Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this serve any purpose? Isn't the idea that null should always be handled before any typeMapping?


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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fallback necessary for the fix? To me it seems like a rather large change in behavior, and I fail to see why we would want to have a fallback with an error rather than a complete ceasing of processing? Surely something is very wrong if we get here (and this isn't the proper way to handle that)?

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really log an error if this is an expected processing branch now?

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())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that we should never check OAS 3.1 types here? Or is it expected that the openAPIType or normalization has handled that (but if so, why even check 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