Fix alias to map response (#1067)

* fix response reference to map

* update samples

* add null check for openapi
This commit is contained in:
William Cheng
2018-09-20 19:29:33 +08:00
committed by GitHub
parent 340466066c
commit 1b2f3fbfb6
95 changed files with 616 additions and 509 deletions

View File

@@ -841,15 +841,14 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Return the name with escaped characters.
*
* @param name the name to be escaped
* @param charactersToAllow characters that are not escaped
* @param name the name to be escaped
* @param charactersToAllow characters that are not escaped
* @param appdendixToReplacement String to append to replaced characters.
* @return the escaped word
* <p>
* throws Runtime exception as word is not escaped properly.
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
* @see org.openapitools.codegen.utils.StringUtils#escape directly instead
*
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
*/
@Deprecated
public String escapeSpecialCharacters(String name, List<String> charactersToAllow, String appdendixToReplacement) {
@@ -1096,11 +1095,6 @@ public class DefaultCodegen implements CodegenConfig {
public String toInstantiationType(Schema schema) {
if (ModelUtils.isMapSchema(schema)) {
Schema additionalProperties = ModelUtils.getAdditionalProperties(schema);
String type = additionalProperties.getType();
if (null == type) {
LOGGER.error("No Type defined for Additional Property " + additionalProperties + "\n" //
+ "\tIn Property: " + schema);
}
String inner = getSchemaType(additionalProperties);
return instantiationTypes.get("map") + "<String, " + inner + ">";
} else if (ModelUtils.isArraySchema(schema)) {
@@ -2254,7 +2248,7 @@ public class DefaultCodegen implements CodegenConfig {
}
operationId = removeNonNameElementToCamelCase(operationId);
if(path.startsWith("/")) {
if (path.startsWith("/")) {
op.path = path;
} else {
op.path = "/" + path;
@@ -2295,7 +2289,10 @@ public class DefaultCodegen implements CodegenConfig {
op.responses.get(op.responses.size() - 1).hasMore = false;
if (methodResponse != null) {
final Schema responseSchema = ModelUtils.getSchemaFromResponse(methodResponse);
Schema responseSchema = ModelUtils.getSchemaFromResponse(methodResponse);
if (openAPI != null && openAPI.getComponents() != null) { // has models/aliases defined
responseSchema = ModelUtils.unaliasSchema(openAPI.getComponents().getSchemas(), responseSchema);
}
if (responseSchema != null) {
CodegenProperty cm = fromProperty("response", responseSchema);
@@ -2662,12 +2659,12 @@ public class DefaultCodegen implements CodegenConfig {
}
Stream.of(
Pair.of("get", pi.getGet()),
Pair.of("head", pi.getHead()),
Pair.of("put", pi.getPut()),
Pair.of("post", pi.getPost()),
Pair.of("delete", pi.getDelete()),
Pair.of("patch", pi.getPatch()),
Pair.of("get", pi.getGet()),
Pair.of("head", pi.getHead()),
Pair.of("put", pi.getPut()),
Pair.of("post", pi.getPost()),
Pair.of("delete", pi.getDelete()),
Pair.of("patch", pi.getPatch()),
Pair.of("options", pi.getOptions()))
.filter(p -> p.getValue() != null)
.forEach(p -> {
@@ -2676,7 +2673,7 @@ public class DefaultCodegen implements CodegenConfig {
boolean genId = op.getOperationId() == null;
if (genId) {
op.setOperationId(getOrGenerateOperationId(op, c.name+"_"+expression.replaceAll("\\{\\$.*}", ""), method));
op.setOperationId(getOrGenerateOperationId(op, c.name + "_" + expression.replaceAll("\\{\\$.*}", ""), method));
}
if (op.getExtensions() == null) {
@@ -3265,8 +3262,8 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param word The word
* @return The underscored version of the word
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
* @see org.openapitools.codegen.utils.StringUtils#underscore
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
*/
@Deprecated
public static String underscore(String word) {
@@ -3278,8 +3275,8 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param word The word
* @return The dashized version of the word, e.g. "my-name"
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
* @see org.openapitools.codegen.utils.StringUtils#dashize
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
*/
@SuppressWarnings("static-method")
@Deprecated
@@ -3492,8 +3489,8 @@ public class DefaultCodegen implements CodegenConfig {
*
* @param word string to be camelize
* @return camelized string
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
* @see org.openapitools.codegen.utils.StringUtils#camelize(String)
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
*/
@Deprecated
public static String camelize(String word) {
@@ -3506,8 +3503,8 @@ public class DefaultCodegen implements CodegenConfig {
* @param word string to be camelize
* @param lowercaseFirstLetter lower case for first letter if set to true
* @return camelized string
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
* @see org.openapitools.codegen.utils.StringUtils#camelize(String, boolean)
* @deprecated since version 3.2.3, may be removed with the next major release (4.0)
*/
@Deprecated
public static String camelize(String word, boolean lowercaseFirstLetter) {
@@ -4697,10 +4694,10 @@ public class DefaultCodegen implements CodegenConfig {
* Post-process the auto-generated file, e.g. using go-fmt to format the Go code. The file type can be "model-test",
* "model-doc", "model", "api", "api-test", "api-doc", "supporting-mustache", "supporting-common",
* "openapi-generator-ignore", "openapi-generator-version"
*
* <p>
* TODO: store these values in enum instead
*
* @param file file to be processed
* @param file file to be processed
* @param fileType file type
*/
public void postProcessFile(File file, String fileType) {

View File

@@ -308,7 +308,7 @@ public class ModelUtils {
if (schema.getAdditionalProperties() instanceof Schema) {
return true;
}
if (schema.getAdditionalProperties() instanceof Boolean && (Boolean)schema.getAdditionalProperties()) {
if (schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties()) {
return true;
}
return false;
@@ -638,21 +638,42 @@ public class ModelUtils {
* Get the actual schema from aliases. If the provided schema is not an alias, the schema itself will be returned.
*
* @param allSchemas all schemas
* @param schema schema (alias or direct reference)
* @param schema schema (alias or direct reference)
* @return actual schema
*/
public static Schema unaliasSchema(Map<String, Schema> allSchemas, Schema schema) {
if (allSchemas == null || allSchemas.isEmpty()) {
LOGGER.warn("allSchemas cann't be null/empty in unaliasSchema. Returned 'schema'");
return schema;
}
if (schema != null && StringUtils.isNotEmpty(schema.get$ref())) {
Schema ref = allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref()));
if (ref == null) {
LOGGER.warn("{} is not defined", schema.get$ref());
return schema;
} else if (isObjectSchema(ref)) { // model
return schema;
} else if (isStringSchema(ref) && (ref.getEnum() != null && !ref.getEnum().isEmpty())) {
// top-level enum class
return schema;
} else if (isMapSchema(ref) || isArraySchema(ref) || isComposedSchema(ref)) { // map/array def should be created as models
} else if (isArraySchema(ref) || isComposedSchema(ref)) { // array def should be created as models
return schema;
} else if (isMapSchema(ref)) {
if (ref.getProperties() != null && !ref.getProperties().isEmpty()) // has properties
return schema; // treat it as model
else {
// treat it as a typical map
/* TODO unalias the map item if it's an alias
if (ref.getAdditionalProperties() != null) {
Schema innerSchema = (Schema) ref.getAdditionalProperties();
if (StringUtils.isNotEmpty(innerSchema.get$ref())) { // map item is a ref to something else
//Schema unaliasInnerSchema = unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(innerSchema.get$ref())));
//ref.setAdditionalProperties(unaliasInnerSchema);
}
}*/
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
}
} else if (isObjectSchema(ref)) { // model
return schema;
} else {
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
@@ -662,10 +683,10 @@ public class ModelUtils {
}
public static Schema getAdditionalProperties(Schema schema) {
if(schema.getAdditionalProperties() instanceof Schema) {
if (schema.getAdditionalProperties() instanceof Schema) {
return (Schema) schema.getAdditionalProperties();
}
if(schema.getAdditionalProperties() instanceof Boolean && (Boolean)schema.getAdditionalProperties()) {
if (schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties()) {
return new ObjectSchema();
}
return null;