Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions marshaller/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ func syncChanges(ctx context.Context, source any, target any, valueNode *yaml.No
}
}

// Ensure we have a valid YAML node even for empty structs
if valueNode == nil {
// Create an empty mapping node for empty structs
valueNode = &yaml.Node{
Kind: yaml.MappingNode,
Tag: "!!map",
Style: yaml.FlowStyle,
}
}

// Populate the RootNode of the target with the result
if coreModel, ok := t.Addr().Interface().(CoreModeler); ok {
coreModel.SetRootNode(valueNode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"openapi": "3.1.1",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {},
"components": {
"schemas": {
"TestSchema": {
"type": "object",
"properties": {
"parent": {
"oneOf": [
{},
{
"type": [
"null"
]
}
]
}
}
}
}
}
}
20 changes: 20 additions & 0 deletions openapi/testdata/upgrade/minimal_nullable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {},
"components": {
"schemas": {
"TestSchema": {
"type": "object",
"properties": {
"parent": {
"nullable": true
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion openapi/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func upgradeNullableSchema(schema *oas3.Schema) {
nullSchema := createNullSchema()
clone := *schema
newSchema := oas3.Schema{}
newSchema.OneOf = []*oas3.JSONSchema[oas3.Referenceable]{nullSchema, oas3.NewJSONSchemaFromSchema[oas3.Referenceable](&clone)}
newSchema.OneOf = []*oas3.JSONSchema[oas3.Referenceable]{oas3.NewJSONSchemaFromSchema[oas3.Referenceable](&clone), nullSchema}
*schema = newSchema
}
}
Expand Down
7 changes: 7 additions & 0 deletions openapi/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func TestUpgrade_Success(t *testing.T) {
options: []openapi.Option[openapi.UpgradeOptions]{openapi.WithUpgradeSamePatchVersion()},
description: "3.1.0 should upgrade with WithUpgradeSamePatchVersion option",
},
{
name: "upgrade_nullable_schema",
inputFile: "testdata/upgrade/minimal_nullable.json",
expectedFile: "testdata/upgrade/expected_minimal_nullable_upgraded.json",
options: nil,
description: "nullable schema should upgrade to oneOf without panic",
},
}

for _, tt := range tests {
Expand Down
Loading