swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.94k stars 6.03k forks source link

[Java] Configuration package names are not sanitized #1089

Closed who closed 9 years ago

who commented 9 years ago

I am working with a pipeline that generates the swagger-codegen config json file. Sometimes, a json file is generated that contains dashes in the apiPackage parameter. Dashes are not valid values in java package names, so it would be nice if swagger-codegen would sanitize this or throw an error.

Here is an example/repro (requires mvn, java) :

1. Make a temporary working directory

cd `mktemp -d /tmp/tmp.XXXXXXX`

2. Download swagger-codegen-cli.jar

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get  -DremoteRepositories=https://oss.sonatype.org/content/repositories/snapshots -Dartifact=io.swagger:swagger-codegen-cli:LATEST -Ddest=swagger-codegen-cli.jar

3. Create a config with invalid values Note that there is a dash in the apiPackage

echo '{"apiPackage": "io.swagger.pet-store"}' > config.json

4. Invoke swagger-codegen-cli.jar

java -jar swagger-codegen-cli.jar generate -l java -c config.json -i http://petstore.swagger.io/v2/swagger.json

5. Compile the generated output

mvn compile;

Result:

...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project swagger-java-client: Compilation failure: Compilation failure:
[ERROR] /tmp/tmp.1CUdUg0/src/main/java/io/swagger/pet-store/UserApi.java:[1,22] error: ';' expected
[ERROR] /tmp/tmp.1CUdUg0/src/main/java/io/swagger/pet-store/StoreApi.java:[1,22] error: ';' expected
[ERROR] /tmp/tmp.1CUdUg0/src/main/java/io/swagger/pet-store/PetApi.java:[1,22] error: ';' expected
...
wing328 commented 9 years ago

@who agree with you that codegen should sanitize or throw an error for invalid value in the JSON.

Personally I prefer sanitization on the input. Let me know if you've cycle to submit a PR or I'll spend some time next week to work on it.

who commented 9 years ago

@wing328 I'd be happy to put a PR together

who commented 9 years ago

PR here: https://github.com/swagger-api/swagger-codegen/pull/1095