Skip to content

Commit cadbdda

Browse files
authored
Support x-nullable (#670)
1 parent 871e029 commit cadbdda

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

openapi2conv/openapi2_conv.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,16 @@ func ToV3SchemaRef(schema *openapi3.SchemaRef) *openapi3.SchemaRef {
485485
for i, v := range schema.Value.AllOf {
486486
schema.Value.AllOf[i] = ToV3SchemaRef(v)
487487
}
488+
if val, ok := schema.Value.Extensions["x-nullable"]; ok {
489+
var nullable bool
490+
491+
if err := json.Unmarshal(val.(json.RawMessage), &nullable); err == nil {
492+
schema.Value.Nullable = nullable
493+
}
494+
495+
delete(schema.Value.Extensions, "x-nullable")
496+
}
497+
488498
return schema
489499
}
490500

@@ -824,6 +834,11 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
824834
for i, v := range schema.Value.AllOf {
825835
schema.Value.AllOf[i], _ = FromV3SchemaRef(v, components)
826836
}
837+
if schema.Value.Nullable {
838+
schema.Value.Nullable = false
839+
schema.Value.Extensions["x-nullable"] = true
840+
}
841+
827842
return schema, nil
828843
}
829844

openapi2conv/openapi2_conv_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ const exampleV2 = `
8989
"additionalProperties": true,
9090
"properties": {
9191
"foo": {
92-
"type": "string"
92+
"type": "string",
93+
"x-nullable": true
9394
},
9495
"quux": {
9596
"$ref": "#/definitions/ItemExtension"
@@ -463,7 +464,8 @@ const exampleV3 = `
463464
"additionalProperties": true,
464465
"properties": {
465466
"foo": {
466-
"type": "string"
467+
"type": "string",
468+
"nullable": true
467469
},
468470
"quux": {
469471
"$ref": "#/components/schemas/ItemExtension"

0 commit comments

Comments
 (0)