diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java index 5b517df02b8b..b04d614e68a5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java @@ -304,6 +304,7 @@ public AbstractJavaCodegen() { typeMapping.put("date", "Date"); typeMapping.put("file", "File"); typeMapping.put("AnyType", "Object"); + typeMapping.put("null", "Object"); typeMapping.put("enum", "Enum"); importMapping.put("BigDecimal", "java.math.BigDecimal"); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java index fa55b6297490..8a4cc1b9c659 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java @@ -1114,4 +1114,13 @@ public void removeAnnotationsTest() { public void testSanitizedDataType() { assertThat(codegen.sanitizeDataType("org.somepkg.DataType")).isEqualTo("orgsomepkgDataType"); } + + @Test(description = "the OAS 3.1 null type maps to Object instead of a never-generated ModelNull (issue 24520)") + public void testNullTypeMapsToObject() { + Schema nullSchema = new Schema<>().type("null"); + + assertThat(codegen.getSchemaType(nullSchema)).isEqualTo("Object"); + assertThat(codegen.getTypeDeclaration(nullSchema)).isEqualTo("Object"); + assertThat(codegen.getTypeDeclaration(new ArraySchema().items(nullSchema))).isEqualTo("List"); + } } diff --git a/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml b/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml index 289489356c19..cd449d9484a8 100644 --- a/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml @@ -1135,3 +1135,6 @@ components: second_property: description: just null type type: null + third_property: + description: null type declared as the OAS 3.1 quoted string + type: 'null' diff --git a/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml b/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml index 82069b6831c1..ea3eaa285ad4 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml +++ b/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml @@ -1215,6 +1215,8 @@ components: $ref: "#/components/schemas/Pet" second_property: description: just null type + third_property: + description: null type declared as the OAS 3.1 quoted string updatePetWithForm_request: properties: name: diff --git a/samples/client/petstore/java/okhttp-gson-3.1/docs/PetWithTypesObjectNullAndRef.md b/samples/client/petstore/java/okhttp-gson-3.1/docs/PetWithTypesObjectNullAndRef.md index f2554b172d33..fa3911092efc 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/docs/PetWithTypesObjectNullAndRef.md +++ b/samples/client/petstore/java/okhttp-gson-3.1/docs/PetWithTypesObjectNullAndRef.md @@ -9,6 +9,7 @@ |------------ | ------------- | ------------- | -------------| |**firstProperty** | [**Pet**](Pet.md) | | [optional] | |**secondProperty** | **Object** | just null type | [optional] | +|**thirdProperty** | **Object** | null type declared as the OAS 3.1 quoted string | [optional] | diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/PetWithTypesObjectNullAndRef.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/PetWithTypesObjectNullAndRef.java index 86dc5f1aca47..9717398161a5 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/PetWithTypesObjectNullAndRef.java +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/PetWithTypesObjectNullAndRef.java @@ -63,6 +63,11 @@ public class PetWithTypesObjectNullAndRef { @javax.annotation.Nullable private Object secondProperty = null; + public static final String SERIALIZED_NAME_THIRD_PROPERTY = "third_property"; + @SerializedName(SERIALIZED_NAME_THIRD_PROPERTY) + @javax.annotation.Nullable + private Object thirdProperty = null; + public PetWithTypesObjectNullAndRef() { } @@ -103,6 +108,25 @@ public void setSecondProperty(@javax.annotation.Nullable Object secondProperty) this.secondProperty = secondProperty; } + + public PetWithTypesObjectNullAndRef thirdProperty(@javax.annotation.Nullable Object thirdProperty) { + this.thirdProperty = thirdProperty; + return this; + } + + /** + * null type declared as the OAS 3.1 quoted string + * @return thirdProperty + */ + @javax.annotation.Nullable + public Object getThirdProperty() { + return thirdProperty; + } + + public void setThirdProperty(@javax.annotation.Nullable Object thirdProperty) { + this.thirdProperty = thirdProperty; + } + /** * A container for additional, undeclared properties. * This is a holder for any undeclared properties as specified with @@ -159,7 +183,8 @@ public boolean equals(Object o) { } PetWithTypesObjectNullAndRef petWithTypesObjectNullAndRef = (PetWithTypesObjectNullAndRef) o; return Objects.equals(this.firstProperty, petWithTypesObjectNullAndRef.firstProperty) && - Objects.equals(this.secondProperty, petWithTypesObjectNullAndRef.secondProperty)&& + Objects.equals(this.secondProperty, petWithTypesObjectNullAndRef.secondProperty) && + Objects.equals(this.thirdProperty, petWithTypesObjectNullAndRef.thirdProperty)&& Objects.equals(this.additionalProperties, petWithTypesObjectNullAndRef.additionalProperties); } @@ -169,7 +194,7 @@ private static boolean equalsNullable(JsonNullable a, JsonNullable b) @Override public int hashCode() { - return Objects.hash(firstProperty, secondProperty, additionalProperties); + return Objects.hash(firstProperty, secondProperty, thirdProperty, additionalProperties); } private static int hashCodeNullable(JsonNullable a) { @@ -185,6 +210,7 @@ public String toString() { sb.append("class PetWithTypesObjectNullAndRef {\n"); sb.append(" firstProperty: ").append(toIndentedString(firstProperty)).append("\n"); sb.append(" secondProperty: ").append(toIndentedString(secondProperty)).append("\n"); + sb.append(" thirdProperty: ").append(toIndentedString(thirdProperty)).append("\n"); sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n"); sb.append("}"); return sb.toString(); @@ -204,7 +230,7 @@ private String toIndentedString(Object o) { static { // a set of all properties/fields (JSON key names) - openapiFields = new HashSet(Arrays.asList("first_property", "second_property")); + openapiFields = new HashSet(Arrays.asList("first_property", "second_property", "third_property")); // a set of required properties/fields (JSON key names) openapiRequiredFields = new HashSet(0);