Skip to content

Commit 04a29d1

Browse files
fix inverted logic cubic-dev-ai has found
1 parent 05208f7 commit 04a29d1

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,6 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
27942794
addImport(composed, refSchema, m, modelName);
27952795

27962796
if (allDefinitions != null && refSchema != null) {
2797-
// Inline properties from oneOf wrapper schemas (cannot be used as parent classes)
27982797
if (ModelUtils.isOneOfWrapperSchema(refSchema)) {
27992798
Map<String, Schema> newProperties = new LinkedHashMap<>();
28002799
addProperties(newProperties, required, refSchema, new HashSet<>());
@@ -3162,10 +3161,23 @@ public CodegenModel fromModel(String name, Schema schema) {
31623161
// remove duplicated properties
31633162
m.removeAllDuplicatedProperty();
31643163

3165-
// Mark inherited readonly properties for template to use setter instead of direct field access
31663164
if (m.parent != null && m.readOnlyVars != null) {
3165+
Schema parentSchema = null;
3166+
if (allDefinitions != null && m.parentSchema != null) {
3167+
parentSchema = allDefinitions.get(m.parentSchema);
3168+
}
3169+
3170+
Set<String> parentReadOnlyNames = new HashSet<>();
3171+
if (parentSchema != null && parentSchema.getProperties() != null) {
3172+
for (Map.Entry<String, Schema> entry : ((Map<String, Schema>) parentSchema.getProperties()).entrySet()) {
3173+
if (Boolean.TRUE.equals(entry.getValue().getReadOnly())) {
3174+
parentReadOnlyNames.add(entry.getKey());
3175+
}
3176+
}
3177+
}
3178+
31673179
for (CodegenProperty roVar : m.readOnlyVars) {
3168-
if (!Boolean.TRUE.equals(roVar.isOverridden)) {
3180+
if (Boolean.TRUE.equals(roVar.isOverridden) || parentReadOnlyNames.contains(roVar.baseName)) {
31693181
roVar.vendorExtensions.put("x-is-inherited-readonly", Boolean.TRUE);
31703182
}
31713183
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5075,11 +5075,6 @@ private List<String> getNames(List<CodegenProperty> props) {
50755075
return props.stream().map(v -> v.name).collect(Collectors.toList());
50765076
}
50775077

5078-
/**
5079-
* Test that oneOf wrapper schemas are not used as parent classes.
5080-
* Schemas with oneOf + properties generate AbstractOpenApiSchema subclasses
5081-
* which cannot be extended. Properties should be inlined instead.
5082-
*/
50835078
@Test
50845079
public void testOneOfWrapperSchemaShouldNotBeUsedAsParent() {
50855080
final DefaultCodegen codegen = new DefaultCodegen();
@@ -5119,10 +5114,6 @@ public void testIsOneOfWrapperSchema() {
51195114
"ScrollEvent should not be detected as oneOf wrapper");
51205115
}
51215116

5122-
/**
5123-
* Test that inherited readOnly properties use protected setters in @JsonCreator.
5124-
* Child classes must use setters for inherited readOnly properties since they're private in parent.
5125-
*/
51265117
@Test
51275118
public void testInheritedReadOnlyPropertiesHaveProtectedSetters() {
51285119
final DefaultCodegen codegen = new DefaultCodegen();
@@ -5142,12 +5133,10 @@ public void testInheritedReadOnlyPropertiesHaveProtectedSetters() {
51425133

51435134
boolean foundInheritedReadOnly = false;
51445135
for (CodegenProperty roVar : extendedModel.readOnlyVars) {
5145-
if (!Boolean.TRUE.equals(roVar.isOverridden)) {
5136+
if (Boolean.TRUE.equals(roVar.vendorExtensions.get("x-is-inherited-readonly"))) {
51465137
foundInheritedReadOnly = true;
5147-
assertTrue(Boolean.TRUE.equals(roVar.vendorExtensions.get("x-is-inherited-readonly")),
5148-
"Inherited readOnly '" + roVar.name + "' should have x-is-inherited-readonly");
51495138
}
51505139
}
5151-
assertTrue(foundInheritedReadOnly, "ExtendedModel should have inherited readOnly properties");
5140+
assertTrue(foundInheritedReadOnly, "ExtendedModel should have inherited readOnly properties with x-is-inherited-readonly vendor extension");
51525141
}
51535142
}

0 commit comments

Comments
 (0)