Skip to content

Commit 4dbb5c9

Browse files
authored
typescript-axios anytype is not defined (#6335)
* Include map for `AnyType` in `typescript` * Exclude `any` from the list of types extracted from `anyOf`, `allOf`, `oneOf` Exclude if there are other meaningful types * Include new scripts and `yaml` to test the new case * Execute the new sample for `typescript-axios` * Filter out only `AnyType` instead of all `any` types * Renamed and modified samples - Included more examples using `oneOf, `allOf`, `anyOf` - Includede examples when types that are translated to `any` are involved (`file`)
1 parent 205514c commit 4dbb5c9

File tree

15 files changed

+846
-20
lines changed

15 files changed

+846
-20
lines changed

bin/typescript-axios-petstore-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
./bin/typescript-axios-petstore-with-complex-headers.sh
77
./bin/typescript-axios-petstore-with-single-request-parameters.sh
88
./bin/typescript-axios-petstore-interfaces.sh
9+
./bin/typescript-axios-petstore-composed-schemas.sh
910
./bin/typescript-axios-petstore.sh
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
ags="generate -i modules/openapi-generator/src/test/resources/3_0/composed-schemas.yaml -g typescript-axios -o samples/client/petstore/typescript-axios/builds/composed-schemas $@"
31+
32+
java $JAVA_OPTS -jar $executable $ags

bin/windows/typescript-axios-petstore-all.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ call bin\windows\typescript-axios-petstore-with-complex-headers.bat
66
call bin\windows\typescript-axios-petstore-with-single-request-parameters.bat
77
call bin\windows\typescript-axios-petstore-with-npm-version.bat
88
call bin\windows\typescript-axios-petstore-interfaces.bat
9+
call bin\windows\typescript-axios-petstore-composed-schemas.bat
910
call bin\windows\typescript-axios-petstore-with-npm-version-and-separate-models-and-api.bat
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@ECHO OFF
2+
3+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
4+
5+
If Not Exist %executable% (
6+
mvn clean package
7+
)
8+
9+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
10+
11+
echo
12+
set ags=generate -i modules\openapi-generator\src\test\resources\3_0\composed-schemas.yaml -g typescript-axios -o samples\client\petstore\typescript-axios\builds\composed-schemas
13+
14+
java %JAVA_OPTS% -jar %executable% %ags%

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public AbstractTypeScriptClientCodegen() {
164164
typeMapping.put("UUID", "string");
165165
typeMapping.put("URI", "string");
166166
typeMapping.put("Error", "Error");
167+
typeMapping.put("AnyType", "any");
167168

168169
cliOptions.add(new CliOption(CodegenConstants.ENUM_NAME_SUFFIX, CodegenConstants.ENUM_NAME_SUFFIX_DESC).defaultValue(this.enumSuffix));
169170
cliOptions.add(new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC).defaultValue(this.enumPropertyNaming.name()));
@@ -808,35 +809,38 @@ public void postProcessFile(File file, String fileType) {
808809

809810
@Override
810811
public String toAnyOfName(List<String> names, ComposedSchema composedSchema) {
811-
List<String> types = composedSchema.getAnyOf().stream().map(schema -> {
812-
String schemaType = getSchemaType(schema);
813-
if (ModelUtils.isArraySchema(schema)) {
814-
ArraySchema ap = (ArraySchema) schema;
815-
Schema inner = ap.getItems();
816-
schemaType = schemaType + "<" + getSchemaType(inner) + ">";
817-
}
818-
return schemaType;
819-
}).distinct().collect(Collectors.toList());
812+
List<String> types = getTypesFromSchemas(composedSchema.getAnyOf());
813+
820814
return String.join(" | ", types);
821815
}
822816

823817
@Override
824818
public String toOneOfName(List<String> names, ComposedSchema composedSchema) {
825-
List<String> types = composedSchema.getOneOf().stream().map(schema -> {
826-
String schemaType = getSchemaType(schema);
827-
if (ModelUtils.isArraySchema(schema)) {
828-
ArraySchema ap = (ArraySchema) schema;
829-
Schema inner = ap.getItems();
830-
schemaType = schemaType + "<" + getSchemaType(inner) + ">";
831-
}
832-
return schemaType;
833-
}).distinct().collect(Collectors.toList());
819+
List<String> types = getTypesFromSchemas(composedSchema.getOneOf());
820+
834821
return String.join(" | ", types);
835822
}
836823

837824
@Override
838825
public String toAllOfName(List<String> names, ComposedSchema composedSchema) {
839-
List<String> types = composedSchema.getAllOf().stream().map(schema -> {
826+
List<String> types = getTypesFromSchemas(composedSchema.getAllOf());
827+
828+
return String.join(" & ", types);
829+
}
830+
831+
/**
832+
* Extracts the list of type names from a list of schemas.
833+
* Excludes `AnyType` if there are other valid types extracted.
834+
*
835+
* @param schemas list of schemas
836+
* @return list of types
837+
*/
838+
protected List<String> getTypesFromSchemas(List<Schema> schemas) {
839+
List<Schema> filteredSchemas = schemas.size() > 1
840+
? schemas.stream().filter(schema -> super.getSchemaType(schema) != "AnyType").collect(Collectors.toList())
841+
: schemas;
842+
843+
return filteredSchemas.stream().map(schema -> {
840844
String schemaType = getSchemaType(schema);
841845
if (ModelUtils.isArraySchema(schema)) {
842846
ArraySchema ap = (ArraySchema) schema;
@@ -845,6 +849,5 @@ public String toAllOfName(List<String> names, ComposedSchema composedSchema) {
845849
}
846850
return schemaType;
847851
}).distinct().collect(Collectors.toList());
848-
return String.join(" & ", types);
849852
}
850853
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
3+
openapi: 3.0.1
4+
info:
5+
version: 1.0.0
6+
title: Example
7+
license:
8+
name: MIT
9+
servers:
10+
- url: http://api.example.xyz/v1
11+
paths:
12+
/pets:
13+
patch:
14+
requestBody:
15+
content:
16+
application/json:
17+
schema:
18+
oneOf:
19+
- $ref: '#/components/schemas/Cat'
20+
- $ref: '#/components/schemas/Dog'
21+
# This field will not match to any type.
22+
- description: Any kind of pet
23+
discriminator:
24+
propertyName: pet_type
25+
responses:
26+
'200':
27+
description: Updated
28+
29+
/pets-filtered:
30+
patch:
31+
requestBody:
32+
content:
33+
application/json:
34+
schema:
35+
anyOf:
36+
- $ref: '#/components/schemas/PetByAge'
37+
- $ref: '#/components/schemas/PetByType'
38+
# This field will not match to any type.
39+
- description: Any kind of filter
40+
responses:
41+
'200':
42+
description: Updated
43+
44+
/file:
45+
post:
46+
requestBody:
47+
content:
48+
application/json:
49+
schema:
50+
type: object
51+
properties:
52+
file:
53+
allOf:
54+
- type: file
55+
# This field will not match to any type.
56+
- description: The file to upload
57+
responses:
58+
'200':
59+
description: File uploaded
60+
61+
components:
62+
schemas:
63+
Pet:
64+
type: object
65+
required:
66+
- pet_type
67+
Dog:
68+
allOf:
69+
# This field will not match to any type.
70+
- description: Dog information
71+
- $ref: '#/components/schemas/Pet'
72+
- type: object
73+
properties:
74+
bark:
75+
type: boolean
76+
breed:
77+
type: string
78+
enum: [Dingo, Husky, Retriever, Shepherd]
79+
Cat:
80+
allOf:
81+
- $ref: '#/components/schemas/Pet'
82+
- type: object
83+
properties:
84+
hunts:
85+
type: boolean
86+
age:
87+
type: integer
88+
PetByAge:
89+
type: object
90+
properties:
91+
age:
92+
type: integer
93+
nickname:
94+
type: string
95+
required:
96+
- age
97+
98+
PetByType:
99+
type: object
100+
properties:
101+
pet_type:
102+
type: string
103+
enum: [Cat, Dog]
104+
hunts:
105+
type: boolean
106+
required:
107+
- pet_type
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
wwwroot/*.js
2+
node_modules
3+
typings
4+
dist
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty npmignore to ensure all required files (e.g., in the dist folder) are published by npm
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5.0.0-SNAPSHOT

0 commit comments

Comments
 (0)