Repository navigation
fix(java): map OpenAPI 3.1 null type to Object to prevent ModelNull generation #23961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"); | ||
|
|
@@ -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)) { | ||
|
|
@@ -1866,16 +1872,29 @@ public String toExampleValue(Schema p) { | |
|
|
||
| @Override | ||
| public String getSchemaType(Schema p) { | ||
| if (p == null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure that we should never check OAS 3.1 |
||
| 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); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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?