Skip to content

Commit

Permalink
Adds generator unaliasSchema method, uses it to refactor python-exper…
Browse files Browse the repository at this point in the history
…imental (#7274)

* Adds generator specific unaliasSchema method

* Adds unaliasSchema and hasValidation methods in python-experimental generator

* Removes primitive models with no validations, docs, tests, and models

* Uses unaliasSchema in getSchemaType and adds todos

* Deletes handleMethodResponse and fromResponse

* Simplifies fromRequestBody

* Removes unneeded handleMethodResponse

* Updates javadoc

* Updates fromModel

* Adds python-exp java test defaultSettingInPrimitiveModelWithValidations, removes model NumberWithValidationsAndDefault form v3 sample spec

* Deletes getSimpleTypeDeclaration

* Removes straggler file

* Deletes hasValidation and modelWillBeMade

* Uses super in fromFormProperty

* Regenerates samples for python-experimental

* Updates postProcessAllModels
  • Loading branch information
spacether authored Aug 27, 2020
1 parent b4edfe4 commit 892836f
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 545 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1971,14 +1971,18 @@ public String toOneOfName(List<String> names, ComposedSchema composedSchema) {
return "oneOf<" + String.join(",", names) + ">";
}

protected Schema unaliasSchema(Schema schema, Map<String, String> usedImportMappings) {
return ModelUtils.unaliasSchema(this.openAPI, schema, usedImportMappings);
}

/**
* Return a string representation of the schema type, resolving aliasing and references if necessary.
*
* @param schema input
* @return the string representation of the schema type.
*/
protected String getSingleSchemaType(Schema schema) {
Schema unaliasSchema = ModelUtils.unaliasSchema(this.openAPI, schema, importMapping);
Schema unaliasSchema = unaliasSchema(schema, importMapping);

if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema
// get the schema/model name from $ref
Expand Down Expand Up @@ -2216,7 +2220,7 @@ public CodegenModel fromModel(String name, Schema schema) {
}

// unalias schema
schema = ModelUtils.unaliasSchema(this.openAPI, schema, importMapping);
schema = unaliasSchema(schema, importMapping);
if (schema == null) {
LOGGER.warn("Schema {} not found", name);
return null;
Expand Down Expand Up @@ -2330,7 +2334,7 @@ public CodegenModel fromModel(String name, Schema schema) {
m.interfaces = new ArrayList<String>();

for (Schema interfaceSchema : interfaces) {
interfaceSchema = ModelUtils.unaliasSchema(this.openAPI, interfaceSchema, importMapping);
interfaceSchema = unaliasSchema(interfaceSchema, importMapping);

if (StringUtils.isBlank(interfaceSchema.get$ref())) {
// primitive type
Expand Down Expand Up @@ -2991,7 +2995,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
LOGGER.debug("debugging fromProperty for " + name + " : " + p);

// unalias schema
p = ModelUtils.unaliasSchema(this.openAPI, p, importMapping);
p = unaliasSchema(p, importMapping);

CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);

Expand Down Expand Up @@ -3168,10 +3172,9 @@ public CodegenProperty fromProperty(String name, Schema p) {
} else if (ModelUtils.isArraySchema(p)) {
// default to string if inner item is undefined
ArraySchema arraySchema = (ArraySchema) p;
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema), importMapping);
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema), importMapping);
} else if (ModelUtils.isMapSchema(p)) {
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getAdditionalProperties(p),
importMapping);
Schema innerSchema = unaliasSchema(getAdditionalProperties(p), importMapping);
if (innerSchema == null) {
LOGGER.error("Undefined map inner type for `{}`. Default to String.", p.getName());
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
Expand Down Expand Up @@ -3251,7 +3254,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
itemName = property.name;
}
ArraySchema arraySchema = (ArraySchema) p;
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema), importMapping);
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema), importMapping);
CodegenProperty cp = fromProperty(itemName, innerSchema);
updatePropertyForArray(property, cp);
} else if (ModelUtils.isMapSchema(p)) {
Expand All @@ -3263,8 +3266,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
property.maxItems = p.getMaxProperties();

// handle inner property
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getAdditionalProperties(p),
importMapping);
Schema innerSchema = unaliasSchema(getAdditionalProperties(p), importMapping);
if (innerSchema == null) {
LOGGER.error("Undefined map inner type for `{}`. Default to String.", p.getName());
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
Expand Down Expand Up @@ -3504,7 +3506,7 @@ protected void handleMethodResponse(Operation operation,
CodegenOperation op,
ApiResponse methodResponse,
Map<String, String> importMappings) {
Schema responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(methodResponse), importMappings);
Schema responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(methodResponse), importMapping);

if (responseSchema != null) {
CodegenProperty cm = fromProperty("response", responseSchema);
Expand Down Expand Up @@ -3908,8 +3910,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
}
Schema responseSchema;
if (this.openAPI != null && this.openAPI.getComponents() != null) {
responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(response),
importMapping);
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(response), importMapping);
} else { // no model/alias defined
responseSchema = ModelUtils.getSchemaFromResponse(response);
}
Expand Down Expand Up @@ -4150,7 +4151,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
}

if (parameterSchema != null) {
parameterSchema = ModelUtils.unaliasSchema(this.openAPI, parameterSchema);
parameterSchema = unaliasSchema(parameterSchema, Collections.<String, String>emptyMap());
if (parameterSchema == null) {
LOGGER.warn("warning! Schema not found for parameter \"" + parameter.getName() + "\", using String");
parameterSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to missing type definition.");
Expand Down Expand Up @@ -4690,7 +4691,7 @@ protected void addImport(CodegenModel m, String type) {
private Map<String, Schema> unaliasPropertySchema(Map<String, Schema> properties) {
if (properties != null) {
for (String key : properties.keySet()) {
properties.put(key, ModelUtils.unaliasSchema(this.openAPI, properties.get(key), importMapping()));
properties.put(key, unaliasSchema(properties.get(key), importMapping()));

}
}
Expand Down Expand Up @@ -5815,7 +5816,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
return codegenParameter;
}

private void addBodyModelSchema(CodegenParameter codegenParameter, String name, Schema schema, Set<String> imports, String bodyParameterName, boolean forceSimpleRef) {
protected void addBodyModelSchema(CodegenParameter codegenParameter, String name, Schema schema, Set<String> imports, String bodyParameterName, boolean forceSimpleRef) {
CodegenModel codegenModel = null;
if (StringUtils.isNotBlank(name)) {
schema.setName(name);
Expand Down
Loading

0 comments on commit 892836f

Please sign in to comment.