diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index a09618b5b484..8171678b6128 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -1379,6 +1379,14 @@ public static Schema unaliasSchema(OpenAPI openAPI, return schema; } Schema ref = allSchemas.get(simpleRef); + + // override the description of the referenced component + if(ref != null) { + if(schema.getDescription() != null) { + ref.setDescription(schema.getDescription()); + } + } + if (ref == null) { if (!isRefToSchemaWithProperties(schema.get$ref())) { once(LOGGER).warn("{} is not defined", schema.get$ref()); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java index f9db02812a5c..4a6cc9d045bf 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/TypeScriptClientCodegenTest.java @@ -294,4 +294,33 @@ public void testUseErasableSyntaxConfig() throws IOException { } } } + + @Test + public void testRefDescription() throws Exception { + final File output = Files.createTempDirectory("typescriptnodeclient_").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("typescript") + .setInputSpec("src/test/resources/3_0/typescript/ref-description.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + final DefaultGenerator generator = new DefaultGenerator(); + final List files = generator.opts(clientOptInput).generate(); + files.forEach(File::deleteOnExit); + + Path file = Paths.get(output + "/models/PetUpdateRequest.ts"); + // verify ref description is found + TestUtils.assertFileContains( + file, + "Reference description of Date schema" + ); + // verify component description is not present + TestUtils.assertFileNotContains( + file, + "Component description of Date schema" + ); + } + } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java index 48f1340cf137..a049dd798194 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java @@ -242,6 +242,24 @@ public void testAliasedTypeIsNotUnaliasedIfUsedForImportMapping() { Assert.assertEquals(stringSchema, ModelUtils.unaliasSchema(openAPI, emailSchema, new HashMap<>())); } + @Test + public void testOverrideAliasedSchemaDescription() { + Schema emailSchema = new Schema() + .$ref("#/components/schemas/Email") + .type("string") + .description("description of the reference"); + StringSchema stringSchema = new StringSchema(); + stringSchema.setDescription("description of Email component"); + + HashMap schemaMappings = new HashMap<>(); + schemaMappings.put("Email", "foo.bar.Email"); + + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("Email", stringSchema); + + var unaliasSchema = ModelUtils.unaliasSchema(openAPI, emailSchema, schemaMappings); + Assert.assertEquals(unaliasSchema.getDescription(), "description of the reference"); + } + /** * Issue https://github.com/OpenAPITools/openapi-generator/issues/1624. * ModelUtils.isFreeFormObject() should not throw an NPE when passed an empty diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript/ref-description.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript/ref-description.yaml new file mode 100644 index 000000000000..fbaad022945f --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/typescript/ref-description.yaml @@ -0,0 +1,111 @@ +openapi: 3.1.0 +info: + description: test order parameters + version: 1.0.0 + title: Test order parameters + license: + name: Apache-2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0.html' + +paths: + /pets: + get: + tags: + - default + summary: Finds Pets + deprecated: true + description: Find all pets + operationId: findPets + parameters: + - name: type + in: query + description: type of pet + style: form + explode: false + schema: + type: string + default: available + - name: name + in: query + description: name of pet + required: true + schema: + type: string + - name: age + in: query + description: age of pet + schema: + type: number + format: int32 + responses: + '200': + description: successful operation + '400': + description: Invalid status value + + patch: + tags: + - default + summary: Update a Pet + description: Partially update an existing pet + operationId: updatePet + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/PetUpdateRequest' + responses: + '200': + description: Pet updated successfully + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + '400': + description: Invalid input + '404': + description: Pet not found + +components: + schemas: + PetUpdateRequest: + type: object + description: Payload for updating a pet + properties: + name: + type: string + description: New name for the pet + age: + type: integer + format: int32 + description: Updated age + type: + type: string + description: Type of pet + date: + $ref: "#/components/schemas/Date" + description: Reference description of Date schema + required: + - name + + Pet: + type: object + description: Pet object + properties: + id: + type: integer + format: int64 + name: + type: string + age: + type: integer + format: int32 + type: + type: string + + Date: + type: string + format: date + description: Component description of Date schema +