mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 04:20:11 +00:00
fix: render empty scalar nodes
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
14
test_specs/nullable-examples.openapi.yaml
Normal file
14
test_specs/nullable-examples.openapi.yaml
Normal 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:
|
||||||
@@ -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,
|
||||||
|
|||||||
Reference in New Issue
Block a user