Skip to content

Commit e133715

Browse files
committed
Merge branch 'master' into bugfix/pageable-scan-resolve-allof-constraints
2 parents 1e4013a + 60ef743 commit e133715

496 files changed

Lines changed: 8261 additions & 5336 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/generators/java-camel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
104104
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
105105
|sourceFolder|source folder for generated code| |src/main/java|
106106
|springApiVersion|Value for 'version' attribute in @RequestMapping (for Spring 7 and above).| |null|
107-
|substituteGenericPagedModel|Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' pagination-metadata property) and replace their generated references with PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata schema are suppressed from code generation. Only applies when library=spring-boot or spring-http-interface.| |false|
107+
|substituteGenericPagedModel|Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' pagination-metadata property) and replace their generated references with PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata schema are suppressed from code generation.| |false|
108108
|testOutput|Set output folder for models and APIs tests| |${project.build.directory}/generated-test-sources/openapi|
109109
|title|server title name or client service name| |OpenAPI Spring|
110110
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).| |false|

docs/generators/kotlin-spring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5858
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
5959
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
6060
|sourceFolder|source folder for generated code| |src/main/kotlin|
61-
|substituteGenericPagedModel|Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' pagination-metadata property) and replace their generated references with PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata schema are suppressed from code generation. Only applies when library=spring-boot or spring-declarative-http-interface.| |false|
61+
|substituteGenericPagedModel|Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' pagination-metadata property) and replace their generated references with PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata schema are suppressed from code generation.| |false|
6262
|title|server title name or client service name| |OpenAPI Kotlin Spring|
6363
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
6464
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|

docs/generators/spring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
9797
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
9898
|sourceFolder|source folder for generated code| |src/main/java|
9999
|springApiVersion|Value for 'version' attribute in @RequestMapping (for Spring 7 and above).| |null|
100-
|substituteGenericPagedModel|Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' pagination-metadata property) and replace their generated references with PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata schema are suppressed from code generation. Only applies when library=spring-boot or spring-http-interface.| |false|
100+
|substituteGenericPagedModel|Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' pagination-metadata property) and replace their generated references with PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata schema are suppressed from code generation.| |false|
101101
|testOutput|Set output folder for models and APIs tests| |${project.build.directory}/generated-test-sources/openapi|
102102
|title|server title name or client service name| |OpenAPI Spring|
103103
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).| |false|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
351351
}
352352

353353
processedModels.add(name);
354-
Map<String, Object> values = new HashMap<>();
354+
Map<String, Object> values = new LinkedHashMap<>();
355355
LOGGER.debug("Resolving model '{}' to example", name);
356356
if (schema.getExample() != null) {
357357
LOGGER.debug("Using example from spec: {}", schema.getExample());

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,10 @@ public void processOpts() {
674674
importMapping.put("LocalDate", "java.time.LocalDate");
675675
typeMapping.put("time-local","LocalTime");
676676
importMapping.put("LocalTime", "java.time.LocalTime");
677+
typeMapping.put("date-time-local", "LocalDateTime");
678+
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
677679
if ("java8-localdatetime".equals(dateLibrary)) {
678680
typeMapping.put("DateTime", "LocalDateTime");
679-
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
680681
} else {
681682
typeMapping.put("DateTime", "OffsetDateTime");
682683
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
@@ -1450,6 +1451,13 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
14501451
}
14511452
}
14521453
return null;
1454+
} else if (ModelUtils.isDateTimeLocalSchema(schema)) {
1455+
if (schema.getDefault() != null) {
1456+
if ("java8".equals(getDateLibrary())) {
1457+
return String.format(Locale.ROOT, "LocalDateTime.parse(\"%s\")", String.valueOf(schema.getDefault()));
1458+
}
1459+
}
1460+
return null;
14531461
} else if (ModelUtils.isStringSchema(schema)) {
14541462
if (schema.getDefault() != null) {
14551463
String _default;
@@ -1536,6 +1544,10 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
15361544
if("java8".equals(getDateLibrary())) {
15371545
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalTime.parse(\"%s\")", value.asText());
15381546
}
1547+
} else if(ModelUtils.isDateTimeLocalSchema(propertySchema)) {
1548+
if("java8".equals(getDateLibrary())) {
1549+
defaultPropertyExpression = String.format(Locale.ROOT, "java.time.LocalDateTime.parse(\"%s\")", value.asText());
1550+
}
15391551
} else if(ModelUtils.isUUIDSchema(propertySchema)) {
15401552
defaultPropertyExpression = "java.util.UUID.fromString(\"" + value.asText() + "\")";
15411553
} else if(ModelUtils.isStringSchema(propertySchema)) {

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

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.openapitools.codegen.languages;
1818

1919
import com.github.curiousoddman.rgxgen.RgxGen;
20+
import io.swagger.v3.core.util.Json;
2021
import io.swagger.v3.oas.models.examples.Example;
2122
import io.swagger.v3.oas.models.media.Schema;
2223
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -635,18 +636,106 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete
635636

636637
if (parameter.getExample() != null) {
637638
codegenParameter.example = parameter.getExample().toString();
639+
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(parameter.getExample()));
638640
} else if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) {
639641
Example example = parameter.getExamples().values().iterator().next();
640642
if (example.getValue() != null) {
641643
codegenParameter.example = example.getValue().toString();
644+
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(example.getValue()));
642645
}
643646
} else if (schema != null && schema.getExample() != null) {
644647
codegenParameter.example = schema.getExample().toString();
648+
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(schema.getExample()));
645649
}
646650

647651
setParameterExampleValue(codegenParameter);
648652
}
649653

654+
protected String toPythonExample(CodegenProperty cp) {
655+
if (cp == null) {
656+
return null;
657+
}
658+
659+
Object example = getSchemaExample(cp.jsonSchema);
660+
if (example != null) {
661+
return toPythonLiteral(example);
662+
}
663+
664+
return null;
665+
}
666+
667+
protected String toPythonExample(CodegenParameter cp) {
668+
if (cp == null) {
669+
return null;
670+
}
671+
672+
Object example = getSchemaExample(cp.jsonSchema);
673+
if (example != null) {
674+
return toPythonLiteral(example);
675+
}
676+
677+
return null;
678+
}
679+
680+
private Object getSchemaExample(String jsonSchema) {
681+
if (StringUtils.isEmpty(jsonSchema)) {
682+
return null;
683+
}
684+
try {
685+
Map<String, Object> schema = Json.mapper().readValue(jsonSchema, Map.class);
686+
Object example = schema.get("example");
687+
if (example != null) {
688+
return example;
689+
}
690+
Object examples = schema.get("examples");
691+
if (examples instanceof List && !((List<?>) examples).isEmpty()) {
692+
return ((List<?>) examples).get(0);
693+
}
694+
return null;
695+
} catch (Exception e) {
696+
return null;
697+
}
698+
}
699+
700+
protected String toPythonLiteral(Object value) {
701+
if (value == null) {
702+
return "None";
703+
}
704+
if (value instanceof String) {
705+
return toPythonStringLiteral((String) value);
706+
}
707+
if (value instanceof Boolean) {
708+
return (Boolean) value ? "True" : "False";
709+
}
710+
if (value instanceof Number) {
711+
return value.toString();
712+
}
713+
if (value instanceof Map) {
714+
List<String> entries = new ArrayList<>();
715+
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
716+
entries.add(toPythonStringLiteral(String.valueOf(entry.getKey())) + ": " + toPythonLiteral(entry.getValue()));
717+
}
718+
return "{" + StringUtils.join(entries, ", ") + "}";
719+
}
720+
if (value instanceof Iterable) {
721+
List<String> items = new ArrayList<>();
722+
for (Object item : (Iterable<?>) value) {
723+
items.add(toPythonLiteral(item));
724+
}
725+
return "[" + StringUtils.join(items, ", ") + "]";
726+
}
727+
728+
return toPythonStringLiteral(String.valueOf(value));
729+
}
730+
731+
protected String toPythonStringLiteral(String value) {
732+
try {
733+
return Json.mapper().writeValueAsString(value);
734+
} catch (Exception e) {
735+
return "\"" + escapeUnsafeCharacters(value) + "\"";
736+
}
737+
}
738+
650739
@Override
651740
public String sanitizeTag(String tag) {
652741
return sanitizeName(tag);
@@ -2171,10 +2260,10 @@ private String finalizeType(CodegenProperty cp, PythonType pt) {
21712260
pt.annotate("alias", cp.baseName);
21722261
}
21732262

2174-
/* TODO review as example may break the build
2175-
if (!StringUtils.isEmpty(cp.getExample())) { // has example
2176-
fields.add(String.format(Locale.ROOT, "example=%s", cp.getExample()));
2177-
}*/
2263+
String example = toPythonExample(cp);
2264+
if (example != null) {
2265+
pt.annotate("json_schema_extra", "{\"examples\": [" + example + "]}", false);
2266+
}
21782267

21792268
//String defaultValue = null;
21802269
if (!cp.required) { //optional
@@ -2247,11 +2336,6 @@ private String finalizeType(CodegenParameter cp, PythonType pt) {
22472336
pt.annotate("description", cp.description);
22482337
}
22492338

2250-
/* TODO support example
2251-
if (!StringUtils.isEmpty(cp.getExample())) { // has example
2252-
fields.add(String.format(Locale.ROOT, "example=%s", cp.getExample()));
2253-
}*/
2254-
22552339
//return pt.asTypeConstraint(moduleImports);
22562340
return pt.asTypeConstraintWithAnnotations(moduleImports);
22572341
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
373373
CodegenDiscriminator discriminator = model.parentModel.getDiscriminator();
374374
if (discriminator != null) {
375375
for (CodegenDiscriminator.MappedModel mappedModel : discriminator.getMappedModels()) {
376-
if (mappedModel.getModelName().equals(model.name)) {
376+
if (mappedModel.getSchemaName().equals(model.schemaName)) {
377377
model.getVendorExtensions().put("x-discriminator-value", mappedModel.getMappingName());
378378
break;
379379
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public KotlinSpringServerCodegen() {
308308
"Detect schemas that represent paginated responses (an object with a 'content' array property and a 'page' "
309309
+ "pagination-metadata property) and replace their generated references with "
310310
+ "PagedModel<T>. By default this uses a generated type in the config package (default 'org.openapitools.configuration'), but `importMappings.PagedModel` can override it to a custom/FQCN-mapped type. The detected page schemas and the pagination metadata "
311-
+ "schema are suppressed from code generation. Only applies when library=spring-boot or spring-declarative-http-interface.",
311+
+ "schema are suppressed from code generation.",
312312
substituteGenericPagedModel);
313313
addSwitch(COMPANION_OBJECT, "Whether to generate companion objects in data classes, enabling companion extensions.", companionObject);
314314
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
@@ -750,7 +750,7 @@ public void processOpts() {
750750
this.setGeneratePageableConstraintValidation(convertPropertyToBoolean(GENERATE_PAGEABLE_CONSTRAINT_VALIDATION));
751751
}
752752
writePropertyBack(GENERATE_PAGEABLE_CONSTRAINT_VALIDATION, generatePageableConstraintValidation);
753-
if (additionalProperties.containsKey(SUBSTITUTE_GENERIC_PAGED_MODEL) && (library.equals(SPRING_BOOT) || library.equals(SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY))) {
753+
if (additionalProperties.containsKey(SUBSTITUTE_GENERIC_PAGED_MODEL)) {
754754
this.setSubstituteGenericPagedModel(convertPropertyToBoolean(SUBSTITUTE_GENERIC_PAGED_MODEL));
755755
}
756756
writePropertyBack(SUBSTITUTE_GENERIC_PAGED_MODEL, substituteGenericPagedModel);
@@ -1213,7 +1213,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
12131213
}
12141214
}
12151215

1216-
if ((SPRING_BOOT.equals(library) || SPRING_DECLARATIVE_HTTP_INTERFACE_LIBRARY.equals(library)) && substituteGenericPagedModel) {
1216+
if (substituteGenericPagedModel) {
12171217
pagedModelRegistry = PagedModelScanUtils.scanPagedModels(openAPI);
12181218
if (!pagedModelRegistry.isEmpty()) {
12191219
boolean customMapping = importMapping.containsKey("PagedModel");

0 commit comments

Comments
 (0)