Skip to content
Open
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 @@ -303,6 +303,7 @@ public AbstractJavaCodegen() {
typeMapping.put("date", "Date");
typeMapping.put("file", "File");
typeMapping.put("AnyType", "Object");
typeMapping.put("null", "Object");

importMapping.put("BigDecimal", "java.math.BigDecimal");
importMapping.put("UUID", "java.util.UUID");
Expand Down Expand Up @@ -1093,6 +1094,11 @@ public String toModelFilename(String name) {

@Override
public String getTypeDeclaration(Schema p) {
// Intercept "null" explicitly to prevent complex object resolution
if (p != null && "null".equalsIgnoreCase(p.getType())) {
return "Object";
}

Schema<?> schema = unaliasSchema(p);
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
if (ModelUtils.isArraySchema(target)) {
Expand Down Expand Up @@ -1866,16 +1872,29 @@ public String toExampleValue(Schema p) {

@Override
public String getSchemaType(Schema p) {
if (p == null) {
LOGGER.error("Schema is null");
return "Object";
}

// 1. First, call the parent method to extract the schema type string
String openAPIType = super.getSchemaType(p);

// don't apply renaming on types from the typeMapping
if (null == openAPIType) {
LOGGER.error("No Type defined for Schema {}", p);
return "Object"; // Safe fallback to prevent NullPointerException later
}

// 2. Intercept the "null" type string immediately before it routes to typeMapping or toModelName
if ("null".equalsIgnoreCase(openAPIType) || "null".equalsIgnoreCase(p.getType())) {
return "Object";
}

// 3. Don't apply renaming on types from the typeMapping
if (typeMapping.containsKey(openAPIType)) {
return typeMapping.get(openAPIType);
}

if (null == openAPIType) {
LOGGER.error("No Type defined for Schema {}", p);
}
return toModelName(openAPIType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4556,6 +4556,15 @@ public void testSwagger2TagImportRestAssured() throws IOException {
assertThat(content)
.contains("import io.swagger.v3.oas.annotations.tags.*;")
.contains("@Tag(");

}

@Test
public void testNullTypeMapsToObject() {
final JavaClientCodegen codegen = new JavaClientCodegen();
Schema nullSchema = new Schema().type("null");
Assert.assertEquals(codegen.getSchemaType(nullSchema), "Object");
Assert.assertEquals(codegen.getTypeDeclaration(nullSchema), "Object");
}

}
Loading