Added mutate command to low level API

This simple method gives the low API a super powerful and simple way to mutate the value of any node, which is then reflected in the root node, can than be serialized again and, voila! now our spec is editable.
This commit is contained in:
Dave Shanley
2022-09-13 09:15:55 -04:00
parent 647541cc77
commit a2b7119af7
5 changed files with 128 additions and 51 deletions

View File

@@ -10,6 +10,8 @@ import (
v3high "github.com/pb33f/libopenapi/datamodel/high/3.0"
v2low "github.com/pb33f/libopenapi/datamodel/low/2.0"
v3low "github.com/pb33f/libopenapi/datamodel/low/3.0"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
type Document struct {
@@ -40,6 +42,18 @@ func (d *Document) GetSpecInfo() *datamodel.SpecInfo {
return d.info
}
func (d *Document) Serialize() ([]byte, error) {
if d.info == nil {
return nil, fmt.Errorf("unable to serialize, document has not yet been initialized")
}
if d.info.SpecFileType == datamodel.YAMLFileType {
return yaml.Marshal(d.info.RootNode)
} else {
yamlData, _ := yaml.Marshal(d.info.RootNode)
return utils.ConvertYAMLtoJSON(yamlData)
}
}
func (d *Document) BuildV2Document() (*DocumentModel[v2high.Swagger], []error) {
var errors []error
if d.info == nil {