I don't have a runable example handy as I'm basing this off of some results in my work, but I've pasted 2 differing yaml files and how they were generated from Swagger objects in these libraries.
Expected
Results from openapiv3.Swagger.MarshalJSON and converting to yaml in https://editor.swagger.io/#
components:
schemas:
FooBar:
properties:
type_url:
type: string
value:
format: byte
type: string
type: object
info:
title: sample
version: version not set
openapi: 3.0.3
paths: {}
Actual (invalid openapi spec due to the presence of various struct fields in results)
Results from a yaml.Marshal of an openapiv3.Swagger
extensionprops: {}
openapi: 3.0.3
components:
extensionprops: {}
schemas:
FooBar:
ref: ""
value:
extensionprops: {}
type: object
properties:
type_url:
ref: ""
value:
extensionprops: {}
type: string
value:
ref: ""
value:
extensionprops: {}
type: string
format: byte
info:
extensionprops: {}
title: sample
version: version not set
paths: {}
Questions
- Perhaps there's a more appropriate way to use this library and output the resulting yaml?
How to fix
From what I can tell this libary needs to add additional yaml struct tags (inline) to the swagger objects to get this functioning correctly (encoding/json behaves this way by default). See https://godoc.org/gopkg.in/yaml.v2#Marshal
Adding inline to usages of the embedded ExtensionProps struct and to usages of the Schema, SecuritySchema, and Example ref types should cover a lot of this, I'm not sure what the full set is.
Workaround
Something like the following can be done to work around this issue, by leveraging the correct output from UnmarshalJSON to get valid yaml output.
jsonBuf, err := v3Spec.MarshalJSON()
if err != nil {
return err
}
tmp := map[string]interface{}{}
err = json.Unmarshal(jsonBuf, &tmp)
if err != nil {
return err
}
return yaml.NewEncoder(w).Encode(tmp)
I don't have a runable example handy as I'm basing this off of some results in my work, but I've pasted 2 differing yaml files and how they were generated from
Swaggerobjects in these libraries.Expected
Results from
openapiv3.Swagger.MarshalJSONand converting to yaml in https://editor.swagger.io/#Actual (invalid openapi spec due to the presence of various struct fields in results)
extensionpropsvalueResults from a
yaml.Marshalof anopenapiv3.SwaggerQuestions
How to fix
From what I can tell this libary needs to add additional
yamlstruct tags (inline) to the swagger objects to get this functioning correctly (encoding/jsonbehaves this way by default). See https://godoc.org/gopkg.in/yaml.v2#MarshalAdding
inlineto usages of the embeddedExtensionPropsstruct and to usages of theSchema,SecuritySchema, andExampleref types should cover a lot of this, I'm not sure what the full set is.Workaround
Something like the following can be done to work around this issue, by leveraging the correct output from
UnmarshalJSONto get valid yaml output.