Skip to content

Commit

Permalink
typescript-axios anytype is not defined (#6335)
Browse files Browse the repository at this point in the history
* 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`)
  • Loading branch information
codeserk authored May 25, 2020
1 parent 205514c commit 4dbb5c9
Show file tree
Hide file tree
Showing 15 changed files with 846 additions and 20 deletions.
1 change: 1 addition & 0 deletions bin/typescript-axios-petstore-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
./bin/typescript-axios-petstore-with-complex-headers.sh
./bin/typescript-axios-petstore-with-single-request-parameters.sh
./bin/typescript-axios-petstore-interfaces.sh
./bin/typescript-axios-petstore-composed-schemas.sh
./bin/typescript-axios-petstore.sh
32 changes: 32 additions & 0 deletions bin/typescript-axios-petstore-composed-schemas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn -B clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
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 $@"

java $JAVA_OPTS -jar $executable $ags
1 change: 1 addition & 0 deletions bin/windows/typescript-axios-petstore-all.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ call bin\windows\typescript-axios-petstore-with-complex-headers.bat
call bin\windows\typescript-axios-petstore-with-single-request-parameters.bat
call bin\windows\typescript-axios-petstore-with-npm-version.bat
call bin\windows\typescript-axios-petstore-interfaces.bat
call bin\windows\typescript-axios-petstore-composed-schemas.bat
call bin\windows\typescript-axios-petstore-with-npm-version-and-separate-models-and-api.bat
14 changes: 14 additions & 0 deletions bin/windows/typescript-axios-petstore-composed-schemas.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@ECHO OFF

set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M

echo
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

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public AbstractTypeScriptClientCodegen() {
typeMapping.put("UUID", "string");
typeMapping.put("URI", "string");
typeMapping.put("Error", "Error");
typeMapping.put("AnyType", "any");

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

@Override
public String toAnyOfName(List<String> names, ComposedSchema composedSchema) {
List<String> types = composedSchema.getAnyOf().stream().map(schema -> {
String schemaType = getSchemaType(schema);
if (ModelUtils.isArraySchema(schema)) {
ArraySchema ap = (ArraySchema) schema;
Schema inner = ap.getItems();
schemaType = schemaType + "<" + getSchemaType(inner) + ">";
}
return schemaType;
}).distinct().collect(Collectors.toList());
List<String> types = getTypesFromSchemas(composedSchema.getAnyOf());

return String.join(" | ", types);
}

@Override
public String toOneOfName(List<String> names, ComposedSchema composedSchema) {
List<String> types = composedSchema.getOneOf().stream().map(schema -> {
String schemaType = getSchemaType(schema);
if (ModelUtils.isArraySchema(schema)) {
ArraySchema ap = (ArraySchema) schema;
Schema inner = ap.getItems();
schemaType = schemaType + "<" + getSchemaType(inner) + ">";
}
return schemaType;
}).distinct().collect(Collectors.toList());
List<String> types = getTypesFromSchemas(composedSchema.getOneOf());

return String.join(" | ", types);
}

@Override
public String toAllOfName(List<String> names, ComposedSchema composedSchema) {
List<String> types = composedSchema.getAllOf().stream().map(schema -> {
List<String> types = getTypesFromSchemas(composedSchema.getAllOf());

return String.join(" & ", types);
}

/**
* Extracts the list of type names from a list of schemas.
* Excludes `AnyType` if there are other valid types extracted.
*
* @param schemas list of schemas
* @return list of types
*/
protected List<String> getTypesFromSchemas(List<Schema> schemas) {
List<Schema> filteredSchemas = schemas.size() > 1
? schemas.stream().filter(schema -> super.getSchemaType(schema) != "AnyType").collect(Collectors.toList())
: schemas;

return filteredSchemas.stream().map(schema -> {
String schemaType = getSchemaType(schema);
if (ModelUtils.isArraySchema(schema)) {
ArraySchema ap = (ArraySchema) schema;
Expand All @@ -845,6 +849,5 @@ public String toAllOfName(List<String> names, ComposedSchema composedSchema) {
}
return schemaType;
}).distinct().collect(Collectors.toList());
return String.join(" & ", types);
}
}
107 changes: 107 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/composed-schemas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@


openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
# This field will not match to any type.
- description: Any kind of pet
discriminator:
propertyName: pet_type
responses:
'200':
description: Updated

/pets-filtered:
patch:
requestBody:
content:
application/json:
schema:
anyOf:
- $ref: '#/components/schemas/PetByAge'
- $ref: '#/components/schemas/PetByType'
# This field will not match to any type.
- description: Any kind of filter
responses:
'200':
description: Updated

/file:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
file:
allOf:
- type: file
# This field will not match to any type.
- description: The file to upload
responses:
'200':
description: File uploaded

components:
schemas:
Pet:
type: object
required:
- pet_type
Dog:
allOf:
# This field will not match to any type.
- description: Dog information
- $ref: '#/components/schemas/Pet'
- type: object
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
hunts:
type: boolean
age:
type: integer
PetByAge:
type: object
properties:
age:
type: integer
nickname:
type: string
required:
- age

PetByType:
type: object
properties:
pet_type:
type: string
enum: [Cat, Dog]
hunts:
type: boolean
required:
- pet_type
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wwwroot/*.js
node_modules
typings
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty npmignore to ensure all required files (e.g., in the dist folder) are published by npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0.0-SNAPSHOT
Loading

0 comments on commit 4dbb5c9

Please sign in to comment.