fix: render empty scalar nodes

This commit is contained in:
Thomas Rooney
2024-06-13 12:10:54 +01:00
committed by quobix
parent cbe1201cbd
commit 626bca5ee4
4 changed files with 49 additions and 0 deletions

View File

@@ -513,6 +513,13 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
valueNode.Line = line 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 { if !encodeSkip {
var rawNode yaml.Node var rawNode yaml.Node
err := rawNode.Encode(value) err := rawNode.Encode(value)

View File

@@ -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) { func TestDocument_MarshalIndention_Error(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/single-definition.yaml") data, _ := os.ReadFile("../../../test_specs/single-definition.yaml")
info, _ := datamodel.ExtractSpecInfo(data) info, _ := datamodel.ExtractSpecInfo(data)

View File

@@ -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:

View File

@@ -67,6 +67,15 @@ func CreateIntNode(str string) *yaml.Node {
return n return n
} }
func CreateEmptyScalarNode() *yaml.Node {
n := &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!null",
Value: "",
}
return n
}
func CreateFloatNode(str string) *yaml.Node { func CreateFloatNode(str string) *yaml.Node {
n := &yaml.Node{ n := &yaml.Node{
Kind: yaml.ScalarNode, Kind: yaml.ScalarNode,