Skip to content

Commit 8fcb2e0

Browse files
authored
fix: OAS3.1 deprecated property for array properties (#23019)
* test: add failing test for missing @deprecated on array properties in OAS 3.1 The typescript-axios generator drops the @deprecated JSDoc annotation for array-type properties when processing OpenAPI 3.1 specs. This test reproduces the issue by asserting that deprecated scalar, array-of-primitives, and array-of-refs properties all receive the annotation. * fix: preserve deprecated, readOnly, writeOnly when normalizing OAS 3.1 array schemas The OpenAPINormalizer.processNormalize31Spec method converts OAS 3.1 JsonSchema array types into legacy ArraySchema objects, but was not copying the deprecated, readOnly, or writeOnly flags during conversion. This caused @deprecated annotations to be silently dropped for array-type properties in all generators when processing OAS 3.1 specs.
1 parent 5e45db9 commit 8fcb2e0

3 files changed

Lines changed: 155 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,9 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchema
18391839
as.setXml(schema.getXml());
18401840
as.setNullable(schema.getNullable());
18411841
as.setUniqueItems(schema.getUniqueItems());
1842+
as.setDeprecated(schema.getDeprecated());
1843+
as.setReadOnly(schema.getReadOnly());
1844+
as.setWriteOnly(schema.getWriteOnly());
18421845
if (schema.getItems() != null) {
18431846
// `items` is also a json schema
18441847
if (StringUtils.isNotEmpty(schema.getItems().get$ref())) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosClientCodegenTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package org.openapitools.codegen.typescript.axios;
22

3+
import org.openapitools.codegen.ClientOptInput;
34
import org.openapitools.codegen.CodegenConstants;
5+
import org.openapitools.codegen.DefaultGenerator;
46
import org.openapitools.codegen.SupportingFile;
7+
import org.openapitools.codegen.TestUtils;
8+
import org.openapitools.codegen.config.CodegenConfigurator;
59
import org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen;
610
import org.openapitools.codegen.typescript.TypeScriptGroups;
711
import org.testng.annotations.Test;
812

13+
import java.io.File;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
import java.util.List;
18+
919
import static org.assertj.core.api.Assertions.assertThat;
1020
import static org.testng.Assert.assertEquals;
1121

@@ -130,4 +140,42 @@ public void testAppliesCustomAxiosVersion() {
130140

131141
assertEquals(codegen.additionalProperties().get("axiosVersion"), "^1.2.3");
132142
}
143+
144+
@Test(description = "Verify @deprecated annotation is generated for array-type properties")
145+
public void testDeprecatedArrayAttribute() throws Exception {
146+
final File output = Files.createTempDirectory("typescript_axios_deprecated_array_").toFile();
147+
output.deleteOnExit();
148+
149+
final CodegenConfigurator configurator = new CodegenConfigurator()
150+
.setGeneratorName("typescript-axios")
151+
.setInputSpec("src/test/resources/3_1/typescript-axios/deprecated-array-attribute.yaml")
152+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
153+
154+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
155+
final DefaultGenerator generator = new DefaultGenerator();
156+
final List<File> files = generator.opts(clientOptInput).generate();
157+
files.forEach(File::deleteOnExit);
158+
159+
Path file = Paths.get(output + "/api.ts");
160+
String content = Files.readString(file);
161+
162+
// The model has three deprecated properties:
163+
// 'age' (integer), 'tags' (array of strings), 'oldTags' (array of $ref Tag objects)
164+
// and one non-deprecated array property: 'nicknames'
165+
// There should be exactly 3 occurrences of @deprecated in the model file
166+
assertEquals(TestUtils.countOccurrences(content, "@deprecated"), 3,
167+
"Expected @deprecated on 'age' (scalar), 'tags' (array of strings), and 'oldTags' (array of $ref objects)");
168+
169+
// Verify the @deprecated annotation appears in the generated output
170+
TestUtils.assertFileContains(file, "* @deprecated");
171+
172+
// Verify the deprecated array property 'tags' (array of primitives) is present with correct type
173+
TestUtils.assertFileContains(file, "'tags'?: Array<string>");
174+
175+
// Verify the deprecated array property 'oldTags' (array of $ref objects) is present with correct type
176+
TestUtils.assertFileContains(file, "'oldTags'?: Array<Tag>");
177+
178+
// Verify the non-deprecated array property 'nicknames' is also present
179+
TestUtils.assertFileContains(file, "'nicknames'?: Array<string>");
180+
}
133181
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
openapi: 3.1.0
2+
info:
3+
description: Test deprecated annotation on array-type properties
4+
version: 1.0.0
5+
title: Test deprecated array attribute
6+
license:
7+
name: Apache-2.0
8+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
9+
10+
paths:
11+
/pets:
12+
get:
13+
tags:
14+
- default
15+
summary: Finds Pets
16+
operationId: findPets
17+
responses:
18+
'200':
19+
description: successful operation
20+
content:
21+
application/json:
22+
schema:
23+
type: array
24+
items:
25+
$ref: '#/components/schemas/Pet'
26+
27+
patch:
28+
tags:
29+
- default
30+
summary: Update a Pet
31+
operationId: updatePet
32+
requestBody:
33+
required: true
34+
content:
35+
application/json:
36+
schema:
37+
$ref: '#/components/schemas/PetUpdateRequest'
38+
responses:
39+
'200':
40+
description: Pet updated successfully
41+
content:
42+
application/json:
43+
schema:
44+
$ref: '#/components/schemas/Pet'
45+
46+
components:
47+
schemas:
48+
Tag:
49+
type: object
50+
properties:
51+
id:
52+
type: integer
53+
format: int64
54+
name:
55+
type: string
56+
57+
PetUpdateRequest:
58+
type: object
59+
description: Payload for updating a pet
60+
properties:
61+
name:
62+
type: string
63+
description: New name for the pet
64+
age:
65+
type: integer
66+
format: int32
67+
description: Updated age
68+
deprecated: true
69+
tags:
70+
type: array
71+
description: List of tags
72+
deprecated: true
73+
items:
74+
type: string
75+
oldTags:
76+
type: array
77+
description: List of old tag objects
78+
deprecated: true
79+
items:
80+
$ref: '#/components/schemas/Tag'
81+
nicknames:
82+
type: array
83+
description: List of nicknames (not deprecated)
84+
items:
85+
type: string
86+
required:
87+
- name
88+
89+
Pet:
90+
type: object
91+
description: Pet object
92+
properties:
93+
id:
94+
type: integer
95+
format: int64
96+
name:
97+
type: string
98+
age:
99+
type: integer
100+
format: int32
101+
tags:
102+
type: array
103+
items:
104+
type: string

0 commit comments

Comments
 (0)