Skip to content

Commit

Permalink
Support x-nullable (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
krak3n committed Nov 17, 2022
1 parent 871e029 commit cadbdda
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions openapi2conv/openapi2_conv.go
Expand Up @@ -485,6 +485,16 @@ func ToV3SchemaRef(schema *openapi3.SchemaRef) *openapi3.SchemaRef {
for i, v := range schema.Value.AllOf {
schema.Value.AllOf[i] = ToV3SchemaRef(v)
}
if val, ok := schema.Value.Extensions["x-nullable"]; ok {
var nullable bool

if err := json.Unmarshal(val.(json.RawMessage), &nullable); err == nil {
schema.Value.Nullable = nullable
}

delete(schema.Value.Extensions, "x-nullable")
}

return schema
}

Expand Down Expand Up @@ -824,6 +834,11 @@ func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components
for i, v := range schema.Value.AllOf {
schema.Value.AllOf[i], _ = FromV3SchemaRef(v, components)
}
if schema.Value.Nullable {
schema.Value.Nullable = false
schema.Value.Extensions["x-nullable"] = true
}

return schema, nil
}

Expand Down
6 changes: 4 additions & 2 deletions openapi2conv/openapi2_conv_test.go
Expand Up @@ -89,7 +89,8 @@ const exampleV2 = `
"additionalProperties": true,
"properties": {
"foo": {
"type": "string"
"type": "string",
"x-nullable": true
},
"quux": {
"$ref": "#/definitions/ItemExtension"
Expand Down Expand Up @@ -463,7 +464,8 @@ const exampleV3 = `
"additionalProperties": true,
"properties": {
"foo": {
"type": "string"
"type": "string",
"nullable": true
},
"quux": {
"$ref": "#/components/schemas/ItemExtension"
Expand Down

0 comments on commit cadbdda

Please sign in to comment.