Skip to content

Marshaling to yaml doesn't inline ExtensionProps #241

@zwiedmann-isp

Description

@zwiedmann-isp

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)

  • extensionprops
  • value

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions