Skip to content

Commit 892836f

Browse files
authored
Adds generator unaliasSchema method, uses it to refactor python-experimental (#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
1 parent b4edfe4 commit 892836f

3 files changed

Lines changed: 286 additions & 545 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,14 +1971,18 @@ public String toOneOfName(List<String> names, ComposedSchema composedSchema) {
19711971
return "oneOf<" + String.join(",", names) + ">";
19721972
}
19731973

1974+
protected Schema unaliasSchema(Schema schema, Map<String, String> usedImportMappings) {
1975+
return ModelUtils.unaliasSchema(this.openAPI, schema, usedImportMappings);
1976+
}
1977+
19741978
/**
19751979
* Return a string representation of the schema type, resolving aliasing and references if necessary.
19761980
*
19771981
* @param schema input
19781982
* @return the string representation of the schema type.
19791983
*/
19801984
protected String getSingleSchemaType(Schema schema) {
1981-
Schema unaliasSchema = ModelUtils.unaliasSchema(this.openAPI, schema, importMapping);
1985+
Schema unaliasSchema = unaliasSchema(schema, importMapping);
19821986

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

22182222
// unalias schema
2219-
schema = ModelUtils.unaliasSchema(this.openAPI, schema, importMapping);
2223+
schema = unaliasSchema(schema, importMapping);
22202224
if (schema == null) {
22212225
LOGGER.warn("Schema {} not found", name);
22222226
return null;
@@ -2330,7 +2334,7 @@ public CodegenModel fromModel(String name, Schema schema) {
23302334
m.interfaces = new ArrayList<String>();
23312335

23322336
for (Schema interfaceSchema : interfaces) {
2333-
interfaceSchema = ModelUtils.unaliasSchema(this.openAPI, interfaceSchema, importMapping);
2337+
interfaceSchema = unaliasSchema(interfaceSchema, importMapping);
23342338

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

29932997
// unalias schema
2994-
p = ModelUtils.unaliasSchema(this.openAPI, p, importMapping);
2998+
p = unaliasSchema(p, importMapping);
29952999

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

@@ -3168,10 +3172,9 @@ public CodegenProperty fromProperty(String name, Schema p) {
31683172
} else if (ModelUtils.isArraySchema(p)) {
31693173
// default to string if inner item is undefined
31703174
ArraySchema arraySchema = (ArraySchema) p;
3171-
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema), importMapping);
3175+
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema), importMapping);
31723176
} else if (ModelUtils.isMapSchema(p)) {
3173-
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getAdditionalProperties(p),
3174-
importMapping);
3177+
Schema innerSchema = unaliasSchema(getAdditionalProperties(p), importMapping);
31753178
if (innerSchema == null) {
31763179
LOGGER.error("Undefined map inner type for `{}`. Default to String.", p.getName());
31773180
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
@@ -3251,7 +3254,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
32513254
itemName = property.name;
32523255
}
32533256
ArraySchema arraySchema = (ArraySchema) p;
3254-
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema), importMapping);
3257+
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema), importMapping);
32553258
CodegenProperty cp = fromProperty(itemName, innerSchema);
32563259
updatePropertyForArray(property, cp);
32573260
} else if (ModelUtils.isMapSchema(p)) {
@@ -3263,8 +3266,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
32633266
property.maxItems = p.getMaxProperties();
32643267

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

35093511
if (responseSchema != null) {
35103512
CodegenProperty cm = fromProperty("response", responseSchema);
@@ -3908,8 +3910,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
39083910
}
39093911
Schema responseSchema;
39103912
if (this.openAPI != null && this.openAPI.getComponents() != null) {
3911-
responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(response),
3912-
importMapping);
3913+
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(response), importMapping);
39133914
} else { // no model/alias defined
39143915
responseSchema = ModelUtils.getSchemaFromResponse(response);
39153916
}
@@ -4150,7 +4151,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
41504151
}
41514152

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

46954696
}
46964697
}
@@ -5815,7 +5816,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
58155816
return codegenParameter;
58165817
}
58175818

5818-
private void addBodyModelSchema(CodegenParameter codegenParameter, String name, Schema schema, Set<String> imports, String bodyParameterName, boolean forceSimpleRef) {
5819+
protected void addBodyModelSchema(CodegenParameter codegenParameter, String name, Schema schema, Set<String> imports, String bodyParameterName, boolean forceSimpleRef) {
58195820
CodegenModel codegenModel = null;
58205821
if (StringUtils.isNotBlank(name)) {
58215822
schema.setName(name);

0 commit comments

Comments
 (0)