From 31b695f86092eb5510654d9467d2ee09c82ee01a Mon Sep 17 00:00:00 2001 From: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com> Date: Sun, 21 Jun 2026 15:17:16 +0900 Subject: [PATCH] [core] Restore sibling example for allOf with a single $ref (#23335) When a property is declared as `allOf: [ $ref ]` with a sibling `example`, fromProperty() reassigns the working schema to the inner $ref schema before computing the example, so toExampleValue() runs against a schema that has no example and returns the literal string "null". The subsequent "restore original schema" block re-applies the outer schema's nullable, description, min/max, title, etc. but not the example. Restore the example from the original (outer) schema in that block, mirroring the existing handling of the other sibling attributes. Regression from 6.x. Fixes #23335 --- .../org/openapitools/codegen/DefaultCodegen.java | 7 +++++++ .../openapitools/codegen/DefaultCodegenTest.java | 16 ++++++++++++++++ .../src/test/resources/3_0/property-title.yaml | 2 ++ 3 files changed, 25 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index d000cde76814..43a1e9bb0631 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -4307,6 +4307,13 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo if (original.getTitle() != null) { property.setTitle(original.getTitle()); } + // the example was computed above against the inner (allOf/$ref) schema, which does + // not carry the example declared as a sibling of the allOf/$ref. Restore it here so + // that e.g. `allOf: [ $ref ]` with a sibling `example` keeps the declared example + // instead of falling back to the literal "null". + if (original.getExample() != null) { + property.example = toExampleValue(original); + } } // override defaultValue if it's not set and defaultToEmptyContainer is set diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index afd75b3f16f6..f46f712a3890 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -2105,6 +2105,22 @@ public void testTitleProperty() { assertEquals("Ref-Property-Title", codegen.fromProperty("refProperty", (Schema) testProperties.get("refProperty")).title); } + @Test + public void testAllOfSingleRefSiblingExample() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/property-title.yaml"); + new InlineModelResolver().flatten(openAPI); + final DefaultCodegen codegen = new DefaultCodegen(); + codegen.setOpenAPI(openAPI); + + final Map testProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("ModelWithTitledProperties").getProperties()); + + // a plain property keeps its example + assertEquals("Simple-Property-Example", codegen.fromProperty("simpleProperty", (Schema) testProperties.get("simpleProperty")).example); + // an `allOf: [ $ref ]` property must keep the example declared as a sibling of the allOf, + // instead of falling back to the literal "null" computed against the inner $ref schema + assertEquals("Ref-Property-Example", codegen.fromProperty("refProperty", (Schema) testProperties.get("refProperty")).example); + } + @Test public void testDeprecatedRef() { final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/model-deprecated.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_0/property-title.yaml b/modules/openapi-generator/src/test/resources/3_0/property-title.yaml index a7474f08afe7..2bf3e8b5d5f9 100644 --- a/modules/openapi-generator/src/test/resources/3_0/property-title.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/property-title.yaml @@ -19,9 +19,11 @@ components: simpleProperty: type: string title: Simple-Property-Title + example: Simple-Property-Example refProperty: type: string title: Ref-Property-Title + example: Ref-Property-Example allOf: - $ref: '#/components/schemas/RefObject' type: object