[Java] Generated API class has wrong dataType and does not compile Issue (#5331)

* add unit test assertion

* add check for composed schema

* add support for x-allOf-name

* change x-allOf-name to x-all-of-name

* Add more troubleshooting information

* Add more troubleshooting information

* Add more troubleshooting information

* Add more troubleshooting information
This commit is contained in:
Sebastien Rosset 2020-03-17 00:37:33 -07:00 committed by GitHub
parent b40257f53a
commit a16079ce7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 5 deletions

View File

@ -7,22 +7,27 @@ SCRIPT="$0"
echo "# START SCRIPT: ${SCRIPT}"
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
logfile="/tmp/generator-fake-petstore-output.log"
for GENERATOR in $(java -jar ${executable} list --short | sed -e 's/,/\'$'\n''/g')
do
if eval java -jar ${executable} generate -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ${GENERATOR} -o /tmp/openapi-generator-test-fake-petstore/2.0/${GENERATOR} > /dev/null 2>&1; then
if eval java -jar ${executable} generate -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ${GENERATOR} -o /tmp/openapi-generator-test-fake-petstore/2.0/${GENERATOR} > ${logfile} 2>&1; then
echo "[OAS 2.0] Executed ${GENERATOR} successfully!"
else
echo "ERROR: Failed to run ${GENERATOR}"
echo "ERROR: Failed to run '${GENERATOR}' generator. The command was:"
echo "java -jar ${executable} generate -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ${GENERATOR} -o /tmp/openapi-generator-test-fake-petstore/2.0/${GENERATOR}"
echo "ERROR: The output of the command was:"
cat ${logfile}
exit 1
fi
if eval java -jar ${executable} generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ${GENERATOR} -o /tmp/openapi-generator-test-fake-petstore/3.0/${GENERATOR} > /dev/null 2>&1; then
if eval java -jar ${executable} generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ${GENERATOR} -o /tmp/openapi-generator-test-fake-petstore/3.0/${GENERATOR} > ${logfile} 2>&1; then
echo "[OAS 3.0] Executed ${GENERATOR} successfully!"
else
echo "ERROR: Failed to run ${GENERATOR}"
echo "ERROR: Failed to run '${GENERATOR}' generator. The command was:"
echo "java -jar ${executable} generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ${GENERATOR} -o /tmp/openapi-generator-test-fake-petstore/3.0/${GENERATOR}"
echo "ERROR: The output of the command was:"
cat ${logfile}
exit 1
fi
done

View File

@ -1788,6 +1788,10 @@ public class DefaultCodegen implements CodegenConfig {
*/
@SuppressWarnings("static-method")
public String toAllOfName(List<String> names, ComposedSchema composedSchema) {
Map<String, Object> exts = composedSchema.getExtensions();
if (exts != null && exts.containsKey("x-all-of-name")) {
return (String) exts.get("x-all-of-name");
}
if (names.size() == 0) {
LOGGER.error("allOf has no member defined: {}. Default to ERROR_ALLOF_SCHEMA", composedSchema);
return "ERROR_ALLOF_SCHEMA";
@ -5272,6 +5276,7 @@ public class DefaultCodegen implements CodegenConfig {
ModelUtils.syncValidationProperties(schema, codegenParameter);
if (ModelUtils.isMapSchema(schema)) {
// Schema with additionalproperties: true (including composed schemas with additionalproperties: true)
Schema inner = ModelUtils.getAdditionalProperties(schema);
if (inner == null) {
LOGGER.error("No inner type supplied for map parameter `{}`. Default to type:string", schema.getName());

View File

@ -739,7 +739,9 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
if (ModelUtils.isArraySchema(p)) {
Schema<?> items = getSchemaItems((ArraySchema) p);
return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items)) + ">";
} else if (ModelUtils.isMapSchema(p)) {
} else if (ModelUtils.isMapSchema(p) && !ModelUtils.isComposedSchema(p)) {
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
// additionalproperties: true
Schema<?> inner = getSchemaAdditionalProperties(p);
return getSchemaType(p) + "<String, " + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)) + ">";
}

View File

@ -172,6 +172,7 @@ public class JavaModelEnumTest {
codegen.setOpenAPI(openAPI);
Schema enumTest = openAPI.getComponents().getSchemas().get("Enum_Test");
Assert.assertNotNull(enumTest);
CodegenModel cm = codegen.fromModel("Enum_Test", enumTest);
Assert.assertEquals(cm.getVars().size(), 8);