Skip to content
Closed
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 @@ -1093,6 +1093,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 +1871,32 @@ public String toExampleValue(Schema p) {

@Override
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(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)?

return super.getSchemaType(p);
}

// don't apply renaming on types from the typeMapping
if (typeMapping.containsKey(openAPIType)) {
return typeMapping.get(openAPIType);
// Intercept explicit OpenAPI 3.1 type: "null" immediately
if ("null".equalsIgnoreCase(p.getType())) {
return "Object";
}

String openAPIType = super.getSchemaType(p);

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 null;
}

// Intercept if openAPIType resolved to "null"
if ("null".equalsIgnoreCase(openAPIType)) {
return "Object";
}

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

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