Skip to content

Commit

Permalink
ToRawInfo functions return null-valued nodes instead of nil (#218)
Browse files Browse the repository at this point in the history
This fixes crashes in the yaml.v3 export functions that were
discovered for newly-added specs in public directories.
  • Loading branch information
timburks authored Oct 9, 2020
1 parent 48b4738 commit 349b567
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
9 changes: 9 additions & 0 deletions compiler/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ func InvalidKeysInMap(m *yaml.Node, allowedKeys []string, allowedPatterns []*reg
return invalidKeys
}

// NewNullNode creates a new Null node.
func NewNullNode() *yaml.Node {
node := &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!null",
}
return node
}

// NewMappingNode creates a new Mapping node.
func NewMappingNode() *yaml.Node {
return &yaml.Node{
Expand Down
4 changes: 1 addition & 3 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2147,10 +2147,8 @@ func (m *Any) ToRawInfo() *yaml.Node {
return node.Content[0]
}
return &node
} else {
return nil
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Auth suitable for JSON or YAML export.
Expand Down
6 changes: 2 additions & 4 deletions generate-gnostic/generate-compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,8 @@ func (domain *Domain) generateToRawInfoMethodForType(code *printer.Code, typeNam
code.Print(" return node.Content[0]")
code.Print(" }")
code.Print(" return &node")
code.Print("} else {")
code.Print(" return nil")
code.Print("}")
code.Print("return nil")
code.Print("return compiler.NewNullNode()")
} else if typeName == "StringArray" {
code.Print("return compiler.NewSequenceNodeForStringArray(m.Value)")
} else if typeModel.OneOfWrapper {
Expand All @@ -749,7 +747,7 @@ func (domain *Domain) generateToRawInfoMethodForType(code *printer.Code, typeNam
code.Print("}")
}
}
code.Print("return nil")
code.Print("return compiler.NewNullNode()")
} else {
code.Print("info := compiler.NewMappingNode()")
code.Print("if m == nil {return info}")
Expand Down
18 changes: 8 additions & 10 deletions openapiv2/OpenAPIv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6817,7 +6817,7 @@ func (m *AdditionalPropertiesItem) ToRawInfo() *yaml.Node {
if v1, ok := m.GetOneof().(*AdditionalPropertiesItem_Boolean); ok {
return compiler.NewScalarNodeForBool(v1.Boolean)
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Any suitable for JSON or YAML export.
Expand All @@ -6830,10 +6830,8 @@ func (m *Any) ToRawInfo() *yaml.Node {
return node.Content[0]
}
return &node
} else {
return nil
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of ApiKeySecurity suitable for JSON or YAML export.
Expand Down Expand Up @@ -7716,7 +7714,7 @@ func (m *NonBodyParameter) ToRawInfo() *yaml.Node {
if v3 != nil {
return v3.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Oauth2AccessCodeSecurity suitable for JSON or YAML export.
Expand Down Expand Up @@ -7944,7 +7942,7 @@ func (m *Parameter) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of ParameterDefinitions suitable for JSON or YAML export.
Expand Down Expand Up @@ -7976,7 +7974,7 @@ func (m *ParametersItem) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of PathItem suitable for JSON or YAML export.
Expand Down Expand Up @@ -8425,7 +8423,7 @@ func (m *ResponseValue) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Responses suitable for JSON or YAML export.
Expand Down Expand Up @@ -8618,7 +8616,7 @@ func (m *SchemaItem) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of SecurityDefinitions suitable for JSON or YAML export.
Expand Down Expand Up @@ -8670,7 +8668,7 @@ func (m *SecurityDefinitionsItem) ToRawInfo() *yaml.Node {
if v5 != nil {
return v5.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of SecurityRequirement suitable for JSON or YAML export.
Expand Down
30 changes: 14 additions & 16 deletions openapiv3/OpenAPIv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -6635,7 +6635,7 @@ func (m *AdditionalPropertiesItem) ToRawInfo() *yaml.Node {
if v1, ok := m.GetOneof().(*AdditionalPropertiesItem_Boolean); ok {
return compiler.NewScalarNodeForBool(v1.Boolean)
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Any suitable for JSON or YAML export.
Expand All @@ -6648,10 +6648,8 @@ func (m *Any) ToRawInfo() *yaml.Node {
return node.Content[0]
}
return &node
} else {
return nil
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of AnyOrExpression suitable for JSON or YAML export.
Expand All @@ -6668,7 +6666,7 @@ func (m *AnyOrExpression) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Callback suitable for JSON or YAML export.
Expand Down Expand Up @@ -6706,7 +6704,7 @@ func (m *CallbackOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of CallbacksOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -6818,7 +6816,7 @@ func (m *DefaultType) ToRawInfo() *yaml.Node {
if v2, ok := m.GetOneof().(*DefaultType_String_); ok {
return compiler.NewScalarNodeForString(v2.String_)
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Discriminator suitable for JSON or YAML export.
Expand Down Expand Up @@ -6991,7 +6989,7 @@ func (m *ExampleOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of ExamplesOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -7119,7 +7117,7 @@ func (m *HeaderOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of HeadersOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -7270,7 +7268,7 @@ func (m *LinkOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of LinksOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -7791,7 +7789,7 @@ func (m *ParameterOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of ParametersOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -7987,7 +7985,7 @@ func (m *RequestBodyOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Response suitable for JSON or YAML export.
Expand Down Expand Up @@ -8034,7 +8032,7 @@ func (m *ResponseOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of Responses suitable for JSON or YAML export.
Expand Down Expand Up @@ -8269,7 +8267,7 @@ func (m *SchemaOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of SchemasOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -8362,7 +8360,7 @@ func (m *SecuritySchemeOrReference) ToRawInfo() *yaml.Node {
if v1 != nil {
return v1.ToRawInfo()
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of SecuritySchemesOrReferences suitable for JSON or YAML export.
Expand Down Expand Up @@ -8463,7 +8461,7 @@ func (m *SpecificationExtension) ToRawInfo() *yaml.Node {
if v2, ok := m.GetOneof().(*SpecificationExtension_String_); ok {
return compiler.NewScalarNodeForString(v2.String_)
}
return nil
return compiler.NewNullNode()
}

// ToRawInfo returns a description of StringArray suitable for JSON or YAML export.
Expand Down

0 comments on commit 349b567

Please sign in to comment.