Skip to content

Commit 4b5b9fa

Browse files
fix: fixes handling of empty objects during marshaller sync that was impacting docs upgrading with nullable only schemas (#47)
1 parent d38bc46 commit 4b5b9fa

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

marshaller/syncer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ func syncChanges(ctx context.Context, source any, target any, valueNode *yaml.No
249249
}
250250
}
251251

252+
// Ensure we have a valid YAML node even for empty structs
253+
if valueNode == nil {
254+
// Create an empty mapping node for empty structs
255+
valueNode = &yaml.Node{
256+
Kind: yaml.MappingNode,
257+
Tag: "!!map",
258+
Style: yaml.FlowStyle,
259+
}
260+
}
261+
252262
// Populate the RootNode of the target with the result
253263
if coreModel, ok := t.Addr().Interface().(CoreModeler); ok {
254264
coreModel.SetRootNode(valueNode)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"openapi": "3.1.1",
3+
"info": {
4+
"title": "Test API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"TestSchema": {
11+
"type": "object",
12+
"properties": {
13+
"parent": {
14+
"oneOf": [
15+
{},
16+
{
17+
"type": [
18+
"null"
19+
]
20+
}
21+
]
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Test API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"TestSchema": {
11+
"type": "object",
12+
"properties": {
13+
"parent": {
14+
"nullable": true
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}

openapi/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func upgradeNullableSchema(schema *oas3.Schema) {
135135
nullSchema := createNullSchema()
136136
clone := *schema
137137
newSchema := oas3.Schema{}
138-
newSchema.OneOf = []*oas3.JSONSchema[oas3.Referenceable]{nullSchema, oas3.NewJSONSchemaFromSchema[oas3.Referenceable](&clone)}
138+
newSchema.OneOf = []*oas3.JSONSchema[oas3.Referenceable]{oas3.NewJSONSchemaFromSchema[oas3.Referenceable](&clone), nullSchema}
139139
*schema = newSchema
140140
}
141141
}

openapi/upgrade_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ func TestUpgrade_Success(t *testing.T) {
5151
options: []openapi.Option[openapi.UpgradeOptions]{openapi.WithUpgradeSamePatchVersion()},
5252
description: "3.1.0 should upgrade with WithUpgradeSamePatchVersion option",
5353
},
54+
{
55+
name: "upgrade_nullable_schema",
56+
inputFile: "testdata/upgrade/minimal_nullable.json",
57+
expectedFile: "testdata/upgrade/expected_minimal_nullable_upgraded.json",
58+
options: nil,
59+
description: "nullable schema should upgrade to oneOf without panic",
60+
},
5461
}
5562

5663
for _, tt := range tests {

0 commit comments

Comments
 (0)