Skip to content

Commit bdd54da

Browse files
authored
Fix parameter schema mapping (#12893)
* fix parameter schema mapping,refactor unaliasSchema * minor code format change
1 parent a9e63f4 commit bdd54da

14 files changed

+112
-111
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public interface CodegenConfig {
313313

314314
void setRemoveEnumValuePrefix(boolean removeEnumValuePrefix);
315315

316-
Schema unaliasSchema(Schema schema, Map<String, String> schemaMappings);
316+
Schema unaliasSchema(Schema schema);
317317

318318
String defaultTemplatingEngine();
319319

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,8 +2203,8 @@ public String toOneOfName(List<String> names, ComposedSchema composedSchema) {
22032203
}
22042204

22052205
@Override
2206-
public Schema unaliasSchema(Schema schema, Map<String, String> schemaMappings) {
2207-
return ModelUtils.unaliasSchema(this.openAPI, schema, schemaMappings);
2206+
public Schema unaliasSchema(Schema schema) {
2207+
return ModelUtils.unaliasSchema(this.openAPI, schema, schemaMapping);
22082208
}
22092209

22102210
/**
@@ -2214,7 +2214,7 @@ public Schema unaliasSchema(Schema schema, Map<String, String> schemaMappings) {
22142214
* @return the string representation of the schema type.
22152215
*/
22162216
protected String getSingleSchemaType(Schema schema) {
2217-
Schema unaliasSchema = unaliasSchema(schema, schemaMapping);
2217+
Schema unaliasSchema = unaliasSchema(schema);
22182218

22192219
if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema
22202220
// get the schema/model name from $ref
@@ -2546,7 +2546,7 @@ protected void updateModelForComposedSchema(CodegenModel m, Schema schema, Map<S
25462546
m.interfaces = new ArrayList<>();
25472547

25482548
for (Schema interfaceSchema : interfaces) {
2549-
interfaceSchema = unaliasSchema(interfaceSchema, schemaMapping);
2549+
interfaceSchema = unaliasSchema(interfaceSchema);
25502550

25512551
if (StringUtils.isBlank(interfaceSchema.get$ref())) {
25522552
// primitive type
@@ -2775,7 +2775,7 @@ public CodegenModel fromModel(String name, Schema schema) {
27752775
}
27762776

27772777
// unalias schema
2778-
schema = unaliasSchema(schema, schemaMapping);
2778+
schema = unaliasSchema(schema);
27792779
if (schema == null) {
27802780
LOGGER.warn("Schema {} not found", name);
27812781
return null;
@@ -3448,7 +3448,7 @@ protected void updatePropertyForMap(CodegenProperty property, Schema p) {
34483448
property.maxItems = p.getMaxProperties();
34493449

34503450
// handle inner property
3451-
Schema innerSchema = unaliasSchema(getAdditionalProperties(p), schemaMapping);
3451+
Schema innerSchema = unaliasSchema(getAdditionalProperties(p));
34523452
if (innerSchema == null) {
34533453
LOGGER.error("Undefined map inner type for `{}`. Default to String.", p.getName());
34543454
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
@@ -3565,7 +3565,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required) {
35653565
return cpc;
35663566
}
35673567
// unalias schema
3568-
p = unaliasSchema(p, schemaMapping);
3568+
p = unaliasSchema(p);
35693569

35703570
CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
35713571
property.required = required;
@@ -3734,7 +3734,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required) {
37343734
itemName = property.name;
37353735
}
37363736
ArraySchema arraySchema = (ArraySchema) p;
3737-
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema), schemaMapping);
3737+
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema));
37383738
CodegenProperty cp = fromProperty(itemName, innerSchema, false);
37393739
updatePropertyForArray(property, cp);
37403740
} else if (ModelUtils.isTypeObjectSchema(p)) {
@@ -3980,7 +3980,7 @@ protected void handleMethodResponse(Operation operation,
39803980
CodegenOperation op,
39813981
ApiResponse methodResponse,
39823982
Map<String, String> schemaMappings) {
3983-
Schema responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(methodResponse), schemaMapping);
3983+
Schema responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(methodResponse));
39843984

39853985
if (responseSchema != null) {
39863986
CodegenProperty cm = fromProperty("response", responseSchema, false);
@@ -4420,7 +4420,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
44204420

44214421
Schema responseSchema;
44224422
if (this.openAPI != null && this.openAPI.getComponents() != null) {
4423-
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(response), schemaMapping);
4423+
responseSchema = unaliasSchema(ModelUtils.getSchemaFromResponse(response));
44244424
} else { // no model/alias defined
44254425
responseSchema = ModelUtils.getSchemaFromResponse(response);
44264426
}
@@ -4712,7 +4712,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
47124712
String parameterModelName = null;
47134713

47144714
if (parameter.getSchema() != null) {
4715-
parameterSchema = parameter.getSchema();
4715+
parameterSchema = unaliasSchema(parameter.getSchema());
47164716
parameterModelName = getParameterDataType(parameter, parameterSchema);
47174717
CodegenProperty prop;
47184718
if (this instanceof RustServerCodegen) {
@@ -4759,7 +4759,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
47594759
}
47604760

47614761
// TODO need to reivew replacing empty map with schemaMapping instead
4762-
parameterSchema = unaliasSchema(parameterSchema, Collections.emptyMap());
4762+
parameterSchema = unaliasSchema(parameterSchema);
47634763
if (parameterSchema == null) {
47644764
LOGGER.warn("warning! Schema not found for parameter \" {} \"", parameter.getName());
47654765
finishUpdatingParameter(codegenParameter, parameter);
@@ -4964,7 +4964,7 @@ private Schema getReferencedSchemaWhenNotEnum(Schema parameterSchema) {
49644964
* @return data type
49654965
*/
49664966
protected String getParameterDataType(Parameter parameter, Schema schema) {
4967-
Schema unaliasSchema = ModelUtils.unaliasSchema(openAPI, schema);
4967+
Schema unaliasSchema = unaliasSchema(schema);
49684968
if (unaliasSchema.get$ref() != null) {
49694969
return toModelName(ModelUtils.getSimpleRef(unaliasSchema.get$ref()));
49704970
}
@@ -5365,7 +5365,7 @@ protected boolean shouldAddImport(String type) {
53655365
protected Map<String, Schema> unaliasPropertySchema(Map<String, Schema> properties) {
53665366
if (properties != null) {
53675367
for (String key : properties.keySet()) {
5368-
properties.put(key, unaliasSchema(properties.get(key), schemaMapping()));
5368+
properties.put(key, unaliasSchema(properties.get(key)));
53695369

53705370
}
53715371
}
@@ -6491,7 +6491,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
64916491
LOGGER.debug("Debugging fromFormProperty {}: {}", name, propertySchema);
64926492
CodegenProperty codegenProperty = fromProperty(name, propertySchema, false);
64936493

6494-
Schema ps = unaliasSchema(propertySchema, schemaMapping);
6494+
Schema ps = unaliasSchema(propertySchema);
64956495
ModelUtils.syncValidationProperties(ps, codegenParameter);
64966496
codegenParameter.setTypeProperties(ps);
64976497
codegenParameter.setComposedSchemas(getComposedSchemas(ps));
@@ -7002,7 +7002,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
70027002
name = ModelUtils.getSimpleRef(schema.get$ref());
70037003
}
70047004

7005-
Schema unaliasedSchema = unaliasSchema(schema, schemaMapping);
7005+
Schema unaliasedSchema = unaliasSchema(schema);
70067006
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
70077007

70087008
ModelUtils.syncValidationProperties(unaliasedSchema, codegenParameter);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
477477
// generators may choose to make models for use case 2 + 3
478478
Schema refSchema = new Schema();
479479
refSchema.set$ref("#/components/schemas/" + name);
480-
Schema unaliasedSchema = config.unaliasSchema(refSchema, config.schemaMapping());
480+
Schema unaliasedSchema = config.unaliasSchema(refSchema);
481481
if (unaliasedSchema.get$ref() == null) {
482482
LOGGER.info("Model {} not generated since it's a free-form object", name);
483483
continue;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public AbstractDartCodegen() {
109109
apiTestTemplateFiles.put("api_test.mustache", ".dart");
110110

111111
final List<String> reservedWordsList = new ArrayList<>();
112-
try(BufferedReader reader = new BufferedReader(
112+
try (BufferedReader reader = new BufferedReader(
113113
new InputStreamReader(DartClientCodegen.class.getResourceAsStream("/dart/dart-keywords.txt"),
114114
StandardCharsets.UTF_8))) {
115115
while (reader.ready()) {
@@ -445,7 +445,8 @@ public String toModelFilename(String name) {
445445
return underscore(toModelName(name));
446446
}
447447

448-
@Override public String toModelDocFilename(String name) {
448+
@Override
449+
public String toModelDocFilename(String name) {
449450
return toModelName(name);
450451
}
451452

@@ -488,7 +489,7 @@ public String toDefaultValue(Schema schema) {
488489

489490
@Override
490491
public String getTypeDeclaration(Schema p) {
491-
Schema<?> schema = ModelUtils.unaliasSchema(this.openAPI, p, schemaMapping);
492+
Schema<?> schema = unaliasSchema(p);
492493
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
493494
if (ModelUtils.isArraySchema(target)) {
494495
Schema<?> items = getSchemaItems((ArraySchema) schema);
@@ -819,5 +820,7 @@ public void postProcessFile(File file, String fileType) {
819820
}
820821

821822
@Override
822-
public GeneratorLanguage generatorLanguage() { return GeneratorLanguage.DART; }
823+
public GeneratorLanguage generatorLanguage() {
824+
return GeneratorLanguage.DART;
825+
}
823826
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public String getTypeDeclaration(Schema p) {
346346
// specification is aligned with the JSON schema specification.
347347
// When "items" is not specified, the elements of the array may be anything at all.
348348
if (inner != null) {
349-
inner = ModelUtils.unaliasSchema(this.openAPI, inner, schemaMapping);
349+
inner = unaliasSchema(inner);
350350
}
351351
String typDecl;
352352
if (inner != null) {
@@ -360,7 +360,7 @@ public String getTypeDeclaration(Schema p) {
360360
return "[]" + typDecl;
361361
} else if (ModelUtils.isMapSchema(p)) {
362362
Schema inner = getAdditionalProperties(p);
363-
return getSchemaType(p) + "[string]" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner, schemaMapping));
363+
return getSchemaType(p) + "[string]" + getTypeDeclaration(unaliasSchema(inner));
364364
}
365365

366366
//return super.getTypeDeclaration(p);
@@ -792,7 +792,7 @@ public void setGenerateInterfaces(boolean generateInterfaces) {
792792

793793
@Override
794794
public String toDefaultValue(Schema schema) {
795-
schema = ModelUtils.unaliasSchema(this.openAPI, schema, schemaMapping);
795+
schema = unaliasSchema(schema);
796796
if (schema.getDefault() != null) {
797797
return schema.getDefault().toString();
798798
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ public String toModelFilename(String name) {
873873

874874
@Override
875875
public String getTypeDeclaration(Schema p) {
876-
Schema<?> schema = ModelUtils.unaliasSchema(this.openAPI, p, schemaMapping);
876+
Schema<?> schema = unaliasSchema(p);
877877
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
878878
if (ModelUtils.isArraySchema(target)) {
879879
Schema<?> items = getSchemaItems((ArraySchema) schema);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public String getSchemaType(Schema p) {
365365
*/
366366
@Override
367367
public String getTypeDeclaration(Schema p) {
368-
Schema<?> schema = ModelUtils.unaliasSchema(this.openAPI, p, schemaMapping);
368+
Schema<?> schema = unaliasSchema(p);
369369
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
370370
if (ModelUtils.isArraySchema(target)) {
371371
Schema<?> items = getSchemaItems((ArraySchema) schema);

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen {
4747
protected String sourceFolder = "src/main/scala";
4848
protected String appName = "OpenAPI Sample";
4949
protected String appDescription = "A sample openapi server";
50-
protected String infoUrl = "http://org.openapitools" ;
51-
protected String infoEmail = "[email protected]" ;
50+
protected String infoUrl = "http://org.openapitools";
51+
protected String infoEmail = "[email protected]";
5252
protected String licenseInfo = "All rights reserved";
5353
protected String licenseUrl = "http://apache.org/licenses/LICENSE-2.0.html";
54-
protected String apiVersion = "1.0" ;
54+
protected String apiVersion = "1.0";
5555
protected boolean stripPackageName = true;
5656
protected String dateLibrary = DateLibraries.java8.name();
5757

5858
protected enum DateLibraries {
5959
java8("Java 8 native JSR310 (preferred for JDK 1.8+)"),
60-
joda( "Joda (for legacy app)"),
61-
legacy( "Backport to http-client (deprecated)");
60+
joda("Joda (for legacy app)"),
61+
legacy("Backport to http-client (deprecated)");
6262

6363
private final String description;
6464

@@ -187,15 +187,15 @@ public void processOpts() {
187187
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
188188
}
189189

190-
this.appName = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getTitle()).filter(t -> t != null).orElse(this.appName) ;
191-
this.appDescription = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getDescription()).filter(d -> d != null).orElse(this.appDescription) ;
192-
this.infoUrl = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getContact()).filter(c -> c != null).map(c -> c.getUrl()).filter(u -> u != null).orElse(this.infoUrl) ;
193-
this.infoEmail = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getContact()).filter(c -> c != null).map(c -> c.getEmail()).filter(v -> v != null).orElse(this.infoEmail) ;
194-
this.licenseInfo = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getLicense()).filter(l -> l != null).map(l -> l.getName()).filter(n -> n != null).orElse(this.licenseInfo) ;
195-
this.licenseUrl = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getLicense()).filter(l -> l != null).map(l -> l.getUrl()).filter(u -> u != null).orElse(this.licenseUrl) ;
196-
197-
this.apiVersion = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getVersion()).filter(v -> v != null).orElse(this.apiVersion) ;
198-
190+
this.appName = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getTitle()).filter(t -> t != null).orElse(this.appName);
191+
this.appDescription = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getDescription()).filter(d -> d != null).orElse(this.appDescription);
192+
this.infoUrl = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getContact()).filter(c -> c != null).map(c -> c.getUrl()).filter(u -> u != null).orElse(this.infoUrl);
193+
this.infoEmail = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getContact()).filter(c -> c != null).map(c -> c.getEmail()).filter(v -> v != null).orElse(this.infoEmail);
194+
this.licenseInfo = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getLicense()).filter(l -> l != null).map(l -> l.getName()).filter(n -> n != null).orElse(this.licenseInfo);
195+
this.licenseUrl = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getLicense()).filter(l -> l != null).map(l -> l.getUrl()).filter(u -> u != null).orElse(this.licenseUrl);
196+
197+
this.apiVersion = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getVersion()).filter(v -> v != null).orElse(this.apiVersion);
198+
199199
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
200200
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
201201
}
@@ -240,7 +240,7 @@ public void setDateLibrary(String dateLibrary, boolean withLegacy) {
240240
this.dateLibrary = dateLibrary;
241241
return;
242242
}
243-
for ( DateLibraries dateLib : DateLibraries.values()) {
243+
for (DateLibraries dateLib : DateLibraries.values()) {
244244
if (dateLib.name().equals(dateLibrary)) {
245245
this.dateLibrary = dateLibrary;
246246
return;
@@ -357,7 +357,7 @@ public String modelFileFolder() {
357357

358358
@Override
359359
public String getTypeDeclaration(Schema p) {
360-
Schema<?> schema = ModelUtils.unaliasSchema(this.openAPI, p, schemaMapping);
360+
Schema<?> schema = unaliasSchema(p);
361361
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
362362
if (ModelUtils.isArraySchema(target)) {
363363
Schema<?> items = getSchemaItems((ArraySchema) schema);
@@ -395,7 +395,7 @@ public String toInstantiationType(Schema p) {
395395
} else if (ModelUtils.isArraySchema(p)) {
396396
ArraySchema ap = (ArraySchema) p;
397397
String inner = getSchemaType(ap.getItems());
398-
return ( ModelUtils.isSet(ap) ? instantiationTypes.get("set") : instantiationTypes.get("array") ) + "[" + inner + "]";
398+
return (ModelUtils.isSet(ap) ? instantiationTypes.get("set") : instantiationTypes.get("array")) + "[" + inner + "]";
399399
} else {
400400
return null;
401401
}
@@ -433,13 +433,13 @@ public String toDefaultValue(Schema p) {
433433

434434
// test for immutable Monoids with .empty method for idiomatic defaults
435435
if ("List".equals(genericType) ||
436-
"Set".equals(genericType) ||
437-
"Seq".equals(genericType) ||
438-
"Array".equals(genericType) ||
439-
"Vector".equals(genericType) ||
440-
"IndexedSeq".equals(genericType) ||
441-
"Iterable".equals(genericType) ||
442-
"ListSet".equals(genericType)
436+
"Set".equals(genericType) ||
437+
"Seq".equals(genericType) ||
438+
"Array".equals(genericType) ||
439+
"Vector".equals(genericType) ||
440+
"IndexedSeq".equals(genericType) ||
441+
"Iterable".equals(genericType) ||
442+
"ListSet".equals(genericType)
443443
) {
444444
return genericType + ".empty[" + inner + "] ";
445445
}
@@ -611,5 +611,7 @@ public void setInvokerPackage(String invokerPackage) {
611611
}
612612

613613
@Override
614-
public GeneratorLanguage generatorLanguage() { return GeneratorLanguage.SCALA; }
614+
public GeneratorLanguage generatorLanguage() {
615+
return GeneratorLanguage.SCALA;
616+
}
615617
}

0 commit comments

Comments
 (0)