Skip to content

Commit

Permalink
fix: render empty scalar nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRooney committed Jun 13, 2024
1 parent 5ea4f2b commit c4ab9cb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions datamodel/high/node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
valueNode.Line = line
}
}
if b, bok := value.(*yaml.Node); bok {
if b.Kind == yaml.ScalarNode && b.Tag == "!!null" {
encodeSkip = true
valueNode = utils.CreateEmptyScalarNode()
valueNode.Line = line
}
}
if !encodeSkip {
var rawNode yaml.Node
err := rawNode.Encode(value)
Expand Down
19 changes: 19 additions & 0 deletions datamodel/high/v3/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,25 @@ func TestDocument_MarshalIndention(t *testing.T) {
}
}

func TestDocument_Nullable_Example(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/nullable-examples.openapi.yaml")
info, _ := datamodel.ExtractSpecInfo(data)

lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())

highDoc := NewDocument(lowDoc)
rendered := highDoc.RenderWithIndention(2)

if runtime.GOOS != "windows" {
assert.Equal(t, string(data), strings.TrimSpace(string(rendered)))
}

rendered = highDoc.RenderWithIndention(4)
if runtime.GOOS != "windows" {
assert.NotEqual(t, string(data), strings.TrimSpace(string(rendered)))
}
}

func TestDocument_MarshalIndention_Error(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/single-definition.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
Expand Down
14 changes: 14 additions & 0 deletions test_specs/nullable-examples.openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
openapi: 3.1.0
components:
schemas:
Thing:
type: object
description: A nullable example.
properties:
target:
nullable: true
type: string
enum:
- staging
- production
example:
9 changes: 9 additions & 0 deletions utils/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ func CreateIntNode(str string) *yaml.Node {
return n
}

func CreateEmptyScalarNode() *yaml.Node {
n := &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!null",
Value: "",

Check warning on line 74 in utils/nodes.go

View check run for this annotation

Codecov / codecov/patch

utils/nodes.go#L70-L74

Added lines #L70 - L74 were not covered by tests
}
return n

Check warning on line 76 in utils/nodes.go

View check run for this annotation

Codecov / codecov/patch

utils/nodes.go#L76

Added line #L76 was not covered by tests
}

func CreateFloatNode(str string) *yaml.Node {
n := &yaml.Node{
Kind: yaml.ScalarNode,
Expand Down

0 comments on commit c4ab9cb

Please sign in to comment.