Skip to content

Commit b6b71cd

Browse files
authored
[Typescript]: add deprecated tags (#21436)
* Add deprecation markers * Move inputSpec (avoid sharing with other generators) * Generate samples * Refactor unit tests * Add test helper method * Revert "Add test helper method" This reverts commit d3935e8. * Assert number of expected @deprecated
1 parent 6b5fd6e commit b6b71cd

File tree

20 files changed

+1300
-10
lines changed

20 files changed

+1300
-10
lines changed

bin/configs/typescript-echo-api.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
generatorName: typescript
22
outputDir: samples/client/echo_api/typescript/build
3-
inputSpec: modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript/echo_api.yaml
44
templateDir: modules/openapi-generator/src/main/resources/typescript
55
additionalProperties:
66
artifactId: echo-api-typescript

modules/openapi-generator/src/main/resources/typescript/api/api.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {
3232
3333
{{#operation}}
3434
/**
35+
{{#isDeprecated}}
36+
* @deprecated
37+
*
38+
{{/isDeprecated}}
3539
{{#notes}}
3640
* {{&notes}}
3741
{{/notes}}
3842
{{#summary}}
3943
* {{&summary}}
4044
{{/summary}}
4145
{{#allParams}}
42-
* @param {{paramName}} {{description}}
46+
* @param {{paramName}} {{description}}{{#isDeprecated}} (@deprecated){{/isDeprecated}}
4347
{{/allParams}}
4448
*/
4549
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Promise<RequestContext> {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.Collections;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.regex.Matcher;
32+
import java.util.regex.Pattern;
3133

3234
import static org.testng.Assert.*;
3335

@@ -177,6 +179,21 @@ public static void assertFileContains(Path path, String... lines) {
177179
}
178180
}
179181

182+
/**
183+
* Count occurrences of the given text
184+
* @param content content of the file
185+
* @param text text to find
186+
* @return
187+
*/
188+
public static int countOccurrences(String content, String text) {
189+
Matcher matcher = Pattern.compile(text).matcher(content);
190+
int count = 0;
191+
while (matcher.find()) {
192+
count++;
193+
}
194+
return count;
195+
}
196+
180197
public static String linearize(String target) {
181198
return target.replaceAll("\r?\n", "").replaceAll("\\s+", "\\s");
182199
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515

1616
import java.io.File;
1717
import java.nio.file.Files;
18+
import java.nio.file.Path;
1819
import java.nio.file.Paths;
1920
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.Map;
2223

24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
2326
@Test(groups = {TypeScriptGroups.TYPESCRIPT})
2427
public class TypeScriptClientCodegenTest {
2528
@Test
@@ -210,4 +213,58 @@ public void testForAllSanitizedEnum() throws Exception {
210213
"}"
211214
);
212215
}
216+
217+
@Test
218+
public void testDeprecatedOperation() throws Exception {
219+
final File output = Files.createTempDirectory("typescriptnodeclient_").toFile();
220+
output.deleteOnExit();
221+
222+
final CodegenConfigurator configurator = new CodegenConfigurator()
223+
.setGeneratorName("typescript")
224+
.setInputSpec("src/test/resources/3_0/typescript/deprecated-operation.yaml")
225+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
226+
227+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
228+
final DefaultGenerator generator = new DefaultGenerator();
229+
final List<File> files = generator.opts(clientOptInput).generate();
230+
files.forEach(File::deleteOnExit);
231+
232+
// verify operation is deprecated
233+
Path file = Paths.get(output + "/apis/DefaultApi.ts");
234+
TestUtils.assertFileContains(
235+
file,
236+
"* @deprecated"
237+
);
238+
239+
String content = Files.readString(file);
240+
assertEquals(1, TestUtils.countOccurrences(content, "@deprecated"));
241+
242+
}
243+
244+
@Test
245+
public void testDeprecatedParameter() throws Exception {
246+
final File output = Files.createTempDirectory("typescriptnodeclient_").toFile();
247+
output.deleteOnExit();
248+
249+
final CodegenConfigurator configurator = new CodegenConfigurator()
250+
.setGeneratorName("typescript")
251+
.setInputSpec("src/test/resources/3_0/typescript/deprecated-parameter.yaml")
252+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
253+
254+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
255+
final DefaultGenerator generator = new DefaultGenerator();
256+
final List<File> files = generator.opts(clientOptInput).generate();
257+
files.forEach(File::deleteOnExit);
258+
259+
// verify parameter is deprecated parameter
260+
Path file = Paths.get(output + "/apis/DefaultApi.ts");
261+
TestUtils.assertFileContains(
262+
file,
263+
"* @param name name of pet (@deprecated)"
264+
);
265+
266+
String content = Files.readString(file);
267+
assertEquals(1, TestUtils.countOccurrences(content, "@deprecated"));
268+
269+
}
213270
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
openapi: 3.0.0
2+
info:
3+
description: test order parameters
4+
version: 1.0.0
5+
title: Test order parameters
6+
license:
7+
name: Apache-2.0
8+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
9+
paths:
10+
/pets:
11+
get:
12+
tags:
13+
- default
14+
summary: Finds Pets
15+
deprecated: true
16+
description: Find all pets
17+
operationId: findPets
18+
parameters:
19+
- name: type
20+
in: query
21+
description: type of pet
22+
style: form
23+
explode: false
24+
schema:
25+
type: string
26+
default: available
27+
- name: name
28+
in: query
29+
description: name of pet
30+
required: true
31+
schema:
32+
type: string
33+
- name: age
34+
in: query
35+
description: age of pet
36+
schema:
37+
type: number
38+
format: int32
39+
responses:
40+
'200':
41+
description: successful operation
42+
'400':
43+
description: Invalid status value
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
openapi: 3.0.0
2+
info:
3+
description: test order parameters
4+
version: 1.0.0
5+
title: Test order parameters
6+
license:
7+
name: Apache-2.0
8+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
9+
paths:
10+
/pets:
11+
get:
12+
tags:
13+
- default
14+
summary: Finds Pets
15+
description: Find all pets
16+
operationId: findPets
17+
parameters:
18+
- name: type
19+
in: query
20+
description: type of pet
21+
style: form
22+
explode: false
23+
schema:
24+
type: string
25+
default: available
26+
- name: name
27+
in: query
28+
deprecated: true
29+
description: name of pet
30+
required: true
31+
schema:
32+
type: string
33+
- name: age
34+
in: query
35+
description: age of pet
36+
schema:
37+
type: number
38+
format: int32
39+
responses:
40+
'200':
41+
description: successful operation
42+
'400':
43+
description: Invalid status value

0 commit comments

Comments
 (0)