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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.openapitools.codegen.languages;

import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.models.media.Schema;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -170,6 +171,17 @@ public String toDefaultValue(Schema p) {
return null;
}

// The Swagger Parser represents an explicit `default: null` (common with
// `nullable: true`, e.g. via allOf composition) as a Jackson NullNode rather than a
// Java null. Treating it as a concrete default produces an invalid Avro union such as
// `["Foo", "null"]` with `"default": null`, because Avro requires a union's default
// value to match its FIRST branch. Treat an explicit null default as "no default" so
// the field falls through to the nullable-union form `["null", "Foo"]` with
// `"default": null`, which is valid.
if (p.getDefault() instanceof JsonNode && ((JsonNode) p.getDefault()).isNull()) {
return null;
}

if (ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p) || ModelUtils.isStringSchema(p)) {
return "\"" + p.getDefault().toString() + "\"";
}
Expand Down
14 changes: 14 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue6268.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ paths:
description: successful operation
components:
schemas:
AvroDefaultNullReference:
type: object
properties:
id:
type: string
SampleModelToTestAvroDefaultValues:
type: object
required:
- tagsRequired
- tagsRequiredWithDefault
properties:
nullableScalarWithNullDefault:
type: string
nullable: true
default: null
nullableModelWithNullDefault:
nullable: true
default: null
allOf:
- $ref: '#/components/schemas/AvroDefaultNullReference'
name:
type: string
default: 'defaultName'
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
AvroDefaultNullReference.avsc
SampleModelToTestAvroDefaultValues.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"namespace": "model",
"type": "record",
"doc": "",
"name": "AvroDefaultNullReference",
"fields": [
{
"name": "id",
"type": ["null", "string"],
"doc": "",
"default": null
}
]

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
"doc": "",
"name": "SampleModelToTestAvroDefaultValues",
"fields": [
{
"name": "nullableScalarWithNullDefault",
"type": ["null", "string"],
"doc": "",
"default": null
},
{
"name": "nullableModelWithNullDefault",
"type": ["null", "model.AvroDefaultNullReference"],
"doc": "",
"default": null
},
{
"name": "name",
"type": ["string", "null"],
Expand Down
Loading