Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: path item objects become block scalars [#3557] #3566

Merged
merged 9 commits into from
Sep 9, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions protoc-gen-openapiv2/internal/genopenapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,45 @@ func (po openapiPathsObject) MarshalYAML() (interface{}, error) {
pathObjectNode.Kind = yaml.MappingNode

for _, pathData := range po {
var pathNode, pathItemObjectNode yaml.Node
var pathNode yaml.Node

pathNode.SetString(pathData.Path)
b, err := yaml.Marshal(pathData.PathItemObject)
pathItemObjectNode, err := pathData.PathItemObject.toYAMLNode()
if err != nil {
return nil, err
}
pathItemObjectNode.SetString(string(b))
pathObjectNode.Content = append(pathObjectNode.Content, &pathNode, &pathItemObjectNode)
pathObjectNode.Content = append(pathObjectNode.Content, &pathNode, pathItemObjectNode)
}

return pathObjectNode, nil
}

// We can simplify this implementation once the go-yaml bug is resolved. See: https://github.com/go-yaml/yaml/issues/643.
//
// func (pio *openapiPathItemObject) toYAMLNode() (*yaml.Node, error) {
// var node yaml.Node
// if err := node.Encode(pio); err != nil {
// return nil, err
// }
// return &node, nil
// }
func (pio *openapiPathItemObject) toYAMLNode() (*yaml.Node, error) {
var doc yaml.Node
var buf bytes.Buffer
ec := yaml.NewEncoder(&buf)
ec.SetIndent(2)
if err := ec.Encode(pio); err != nil {
return nil, err
}
if err := yaml.Unmarshal(buf.Bytes(), &doc); err != nil {
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
if len(doc.Content) == 0 {
return nil, errors.New("unexpected number of yaml nodes")
}
return doc.Content[0], nil
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
}

func (so openapiInfoObject) MarshalJSON() ([]byte, error) {
type alias openapiInfoObject
return extensionMarshalJSON(alias(so), so.extensions)
Expand Down
Loading