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 @@ -141,6 +141,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
@Getter @Setter
public List<CodegenProperty> parentVars = new ArrayList<>();
public List<CodegenProperty> parentRequiredVars = new ArrayList<>();
public List<CodegenProperty> requiredVarsOptionalInParent = new ArrayList<>();
@Getter @Setter
public List<CodegenProperty> nonNullableVars = new ArrayList<>(); // a list of non-nullable properties
@Getter @Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,13 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
}
if (codegenModel.getParentModel() != null) {
codegenModel.parentRequiredVars = new ArrayList<>(codegenModel.getParentModel().requiredVars);

codegenModel.requiredVarsOptionalInParent = codegenModel.requiredVars.stream()
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.

P2: Potentially incomplete inherited-property detection: only direct parent is checked, which may miss optional properties from higher ancestors in multi-level inheritance.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java, line 792:

<comment>Potentially incomplete inherited-property detection: only direct parent is checked, which may miss optional properties from higher ancestors in multi-level inheritance.</comment>

<file context>
@@ -788,6 +788,13 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
                 if (codegenModel.getParentModel() != null) {
                     codegenModel.parentRequiredVars = new ArrayList<>(codegenModel.getParentModel().requiredVars);
+
+                    codegenModel.requiredVarsOptionalInParent = codegenModel.requiredVars.stream()
+                            .filter(cp ->
+                                codegenModel.getParentModel().getVars().stream()
</file context>

.filter(cp ->
codegenModel.getParentModel().getVars().stream()
.anyMatch(p-> !p.getRequired() && cp.getName().equals(p.getName()))
)
.collect(Collectors.toList());
}
// There must be a better way ...
for (String imp : inheritedImports) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}
{{/openApiNullable}}
{{/required}}
{{/vars}}
{{#requiredVarsOptionalInParent}}
{{name}}({{name}});
{{/requiredVarsOptionalInParent}}
}
{{/hasRequired}}
{{/generatedConstructorWithRequiredArgs}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8132,4 +8132,12 @@ void schemaMappingWithNullableAllOfRendersNullableJavaProperty() throws IOExcept
JavaFileAssert.assertThat(files.get("MyObject.java"))
.assertProperty("optionalRef").withType("JsonNullable<com.example.ExternalModel>");
}

@Test
void issue_23918() throws IOException {
final Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/issue_23918.yaml", SPRING_BOOT,
Map.of(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS, "true", USE_OPTIONAL, "true"));
JavaFileAssert.assertThat(files.get("PartyRoleSpecification.java"))
.fileContains("name(name);", "PartyRoleSpecification(String atType, String name)");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: 3.0.3
info:
title: requiredArg
version: 1.0.0
paths: {}
components:
schemas:
Entity:
type: object
required:
- '@type'
properties:
'@type':
type: string
EntitySpecification:
allOf:
- $ref: "#/components/schemas/Entity"
- type: object
properties:
name:
type: string
description: Name given to the specification
lastUpdate:
type: string
format: date-time
lifecycleStatus:
type: string
isBundle:
type: boolean
version:
type: string
discriminator:
propertyName: "@type"
mapping:
EntitySpecification: "#/components/schemas/EntitySpecification"
PartyRoleSpecification: "#/components/schemas/PartyRoleSpecification"
PartyRoleSpecification:
allOf:
- $ref: "#/components/schemas/EntitySpecification"
- type: object
properties:
status:
type: string
required:
- name
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Bar() {
public Bar(String id, String atType) {
super(atType);
this.id = id;
id(id);
}

public Bar id(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Bar() {
public Bar(String id, String atType) {
super(atType);
this.id = id;
id(id);
}

public Bar id(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Bar() {
public Bar(String id, String atType) {
super(atType);
this.id = id;
id(id);
}

public Bar id(String id) {
Expand Down
Loading