Skip to content

Commit 61f1864

Browse files
github-actions[bot]denvitaharenricardozanini
authored
Added feature to set schema mappings when generation clients. (#846) (#879)
* Added feature to set schema mappings when generation clients. * Updated Restreactive test with schemamapping * Updated the YearMonth schema to be correct format. * Updated the documentation --------- Co-authored-by: Richard Alm <[email protected]> Co-authored-by: Ricardo Zanini <[email protected]>
1 parent 10be4d3 commit 61f1864

File tree

9 files changed

+58
-3
lines changed

9 files changed

+58
-3
lines changed

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public enum ConfigName {
5858
ADDITIONAL_API_TYPE_ANNOTATIONS("additional-api-type-annotations"),
5959
TYPE_MAPPINGS("type-mappings"),
6060
IMPORT_MAPPINGS("import-mappings"),
61+
SCHEMA_MAPPINGS("schema-mappings"),
6162
NORMALIZER("open-api-normalizer"),
6263
RETURN_RESPONSE("return-response"),
6364
ENABLE_SECURITY_GENERATION("enable-security-generation"),

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CommonItemConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public class CommonItemConfig {
3636
@ConfigItem(name = "import-mappings")
3737
public Map<String, String> importMappings;
3838

39+
/**
40+
* Schema Mapping is an OpenAPI Generator configuration specifying which Java types (the values) should be
41+
* imported when a given schema type (the keys of this map) is used
42+
*/
43+
@ConfigItem(name = "schema-mappings")
44+
public Map<String, String> schemaMappings;
45+
3946
/**
4047
* The specified annotations will be added to the generated model files
4148
*/

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ protected void generate(OpenApiGeneratorOptions options) {
300300
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.IMPORT_MAPPINGS, String.class, String.class)
301301
.ifPresent(generator::withImportMappings);
302302

303+
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.SCHEMA_MAPPINGS, String.class, String.class)
304+
.ifPresent(generator::withSchemaMappings);
305+
303306
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.NORMALIZER, String.class, String.class)
304307
.ifPresent(generator::withOpenApiNormalizer);
305308

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ public OpenApiClientGeneratorWrapper withImportMappings(final Map<String, String
188188
return this;
189189
}
190190

191+
public OpenApiClientGeneratorWrapper withSchemaMappings(final Map<String, String> typeMappings) {
192+
typeMappings.forEach(configurator::addSchemaMapping);
193+
return this;
194+
}
195+
191196
public OpenApiClientGeneratorWrapper withOpenApiNormalizer(final Map<String, String> openApiNormalizer) {
192197
configurator.setOpenapiNormalizer(openApiNormalizer);
193198
return this;

client/integration-tests/type-mapping/src/main/openapi/type-mappings-testing.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ components:
3636
UserId:
3737
type: string
3838
format: uuid
39+
YearMonth:
40+
type: object
41+
properties:
42+
year:
43+
format: int32
44+
type: integer
45+
month:
46+
format: int32
47+
type: integer
48+
prolepticMonth:
49+
format: int64
50+
type: integer
51+
monthValue:
52+
format: int32
53+
type: integer
54+
leapYear:
55+
type: boolean
3956

4057
MultipartRequestBody:
4158
type: object
@@ -46,5 +63,7 @@ components:
4663
$ref: '#/components/schemas/SomeDateTime'
4764
binaryStringFile:
4865
$ref: '#/components/schemas/BinaryStringFile'
66+
yearMonth:
67+
$ref: '#/components/schemas/YearMonth'
4968

5069

client/integration-tests/type-mapping/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
quarkus.openapi-generator.codegen.spec.type_mappings_testing_yml.type-mappings.UUID=String
22
quarkus.openapi-generator.codegen.spec.type_mappings_testing_yml.type-mappings.File=InputStream
33
quarkus.openapi-generator.codegen.spec.type_mappings_testing_yml.import-mappings.File=java.io.InputStream
4+
quarkus.openapi-generator.codegen.spec.type_mappings_testing_yml.schema-mappings.YearMonth=java.time.YearMonth
45
quarkus.openapi-generator.codegen.spec.type_mappings_testing_yml.base-package=org.acme.openapi.typemapping
56
quarkus.openapi-generator.codegen.spec.type_mappings_testing_yml.additional-api-type-annotations=@org.eclipse.microprofile.rest.client.annotation.RegisterProvider(io.quarkiverse.openapi.generator.it.type.mapping.OffsetDateTimeParamConverterProvider.class)
67

client/integration-tests/type-mapping/src/test/java/io/quarkiverse/openapi/generator/it/type/mapping/TypeAndImportMappingRestEasyClassicTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.InputStream;
1010
import java.nio.charset.StandardCharsets;
1111
import java.time.OffsetDateTime;
12+
import java.time.YearMonth;
1213
import java.time.ZoneOffset;
1314

1415
import jakarta.inject.Inject;
@@ -41,10 +42,12 @@ class TypeAndImportMappingRestEasyClassicTest {
4142
public void canMapTypesAndImportToDifferentValues() {
4243
final String testUuid = "00112233-4455-6677-8899-aabbccddeeff";
4344
final InputStream testFile = new ByteArrayInputStream("Content of the file".getBytes(StandardCharsets.UTF_8));
45+
final YearMonth testYearMonth = YearMonth.parse("2024-06");
4446

4547
TypeMappingApi.PostTheDataMultipartForm requestBody = new TypeMappingApi.PostTheDataMultipartForm();
4648
requestBody.id = testUuid; // String instead of UUID
4749
requestBody.binaryStringFile = testFile; // InputStream instead of File
50+
requestBody.yearMonth = testYearMonth; // YearMonth instead of String
4851
// dateTime remains OffsetDateTime (as is default)
4952
requestBody.dateTime = OffsetDateTime.of(2000, 2, 13, 4, 5, 6, 0, ZoneOffset.UTC);
5053

@@ -63,6 +66,10 @@ public void canMapTypesAndImportToDifferentValues() {
6366
.withName("binaryStringFile")
6467
.withHeader("Content-Disposition", containing("filename="))
6568
.withHeader(ContentTypeHeader.KEY, equalTo(MediaType.APPLICATION_OCTET_STREAM))
66-
.withBody(equalTo("Content of the file")).build()));
69+
.withBody(equalTo("Content of the file")).build())
70+
.withRequestBodyPart(new MultipartValuePatternBuilder()
71+
.withName("yearMonth")
72+
.withHeader(ContentTypeHeader.KEY, equalTo(MediaType.APPLICATION_JSON))
73+
.withBody(equalTo("\"2024-06\"")).build()));
6774
}
6875
}

client/integration-tests/type-mapping/src/test/java/io/quarkiverse/openapi/generator/it/type/mapping/TypeAndImportMappingRestEasyReactiveTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.*;
66
import java.nio.charset.StandardCharsets;
77
import java.time.OffsetDateTime;
8+
import java.time.YearMonth;
89
import java.time.ZoneOffset;
910

1011
import jakarta.inject.Inject;
@@ -37,10 +38,12 @@ public class TypeAndImportMappingRestEasyReactiveTest {
3738
public void canMapTypesAndImportToDifferentValues() {
3839
final String testUuid = "00112233-4455-6677-8899-aabbccddeeff";
3940
final InputStream testFile = new ByteArrayInputStream("Content of the file".getBytes(StandardCharsets.UTF_8));
41+
final YearMonth testYearMonth = YearMonth.parse("2024-06");
4042

4143
TypeMappingApi.PostTheDataMultipartForm requestBody = new TypeMappingApi.PostTheDataMultipartForm();
4244
requestBody.id = testUuid; // String instead of UUID
4345
requestBody.binaryStringFile = testFile; // InputStream instead of File
46+
requestBody.yearMonth = testYearMonth; // YearMonth instead of String
4447
// dateTime remains OffsetDateTime (as is default)
4548
requestBody.dateTime = OffsetDateTime.of(2000, 2, 13, 4, 5, 6, 0, ZoneOffset.UTC);
4649

@@ -52,6 +55,12 @@ public void canMapTypesAndImportToDifferentValues() {
5255
.withHeader(ContentTypeHeader.KEY, equalTo(MediaType.TEXT_PLAIN + "; charset=UTF-8"))
5356
.withBody(equalTo(testUuid)).build()));
5457

58+
typeMappingServer.verify(postRequestedFor(urlEqualTo("/type-mapping"))
59+
.withRequestBodyPart(new MultipartValuePatternBuilder()
60+
.withName("yearMonth")
61+
.withHeader(ContentTypeHeader.KEY, equalTo(MediaType.APPLICATION_JSON))
62+
.withBody(equalTo("\"2024-06\"")).build()));
63+
5564
typeMappingServer.verify(postRequestedFor(urlEqualTo("/type-mapping"))
5665
.withRequestBodyPart(new MultipartValuePatternBuilder()
5766
.withName("dateTime")

docs/modules/ROOT/pages/client.adoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ See the module `integration-tests/register-provider` for an example of how to us
115115

116116
Use the property key `quarkus.openapi-generator.codegen.validateSpec=false` to disable validating the input specification file before code generation. By default, invalid specifications will result in an error.
117117

118-
== Type and import mappings
118+
== Type, schema and import mappings
119119

120120
It's possible to remap types in the generated files. For example, instead of a `File` you can configure the code generator to use `InputStream` for all file upload parts of multipart request, or you could change all `UUID` types to `String`. You can configure this in your `application.properties` using the following configuration keys:
121121

@@ -129,6 +129,9 @@ It's possible to remap types in the generated files. For example, instead of a `
129129
|Import Mapping
130130
|`quarkus.openapi-generator.codegen.spec.[filename].import-mappings.[type]`
131131
|`quarkus.openapi-generator.codegen.spec.my_spec_yml.import-mappings.File=java.io.InputStream` will replace the default `import java.io.File` with `import java.io.InputStream`
132+
|Schema Mapping
133+
|`quarkus.openapi-generator.codegen.spec.[filename].schema-mappings.[type]`
134+
|`quarkus.openapi-generator.codegen.spec.my_spec_yml.schema-mappings.YearMonth=java.time.YearMonth` will use `java.time.YearMonth` as type for all schemas of the chosen type in the file.
132135
|===
133136

134137
Note that these configuration properties are maps. For the type-mapping the keys are OAS data types and the values are Java types.
@@ -141,7 +144,7 @@ quarkus.openapi-generator.codegen.spec.my_spec_yml.type-mappings.DateTime=Instan
141144
quarkus.openapi-generator.codegen.spec.my_spec_yml.import-mappings.Instant=java.time.Instant
142145
----
143146

144-
It's also possible to only use a type mapping with a fully qualified name, for instance `quarkus.openapi-generator.codegen.spec.my_spec_yml.type-mappings.File=java.io.InputStream`. For more information and a list of all types see the OpenAPI generator documentation on https://openapi-generator.tech/docs/usage/#type-mappings-and-import-mappings[Type Mappings and Import Mappings].
147+
It's also possible to only use a type mapping with a fully qualified name, for instance `quarkus.openapi-generator.codegen.spec.my_spec_yml.type-mappings.File=java.io.InputStream`. For more information and a list of all types see the OpenAPI generator documentation on https://openapi-generator.tech/docs/usage/#type-mappings-and-import-mappings[Type Mappings and Import Mappings] and https://openapi-generator.tech/docs/customization#schema-mapping[Schema mapping].
145148

146149
See the module https://github.com/quarkiverse/quarkus-openapi-generator/tree/main/integration-tests/type-mapping[type-mapping] for an example of how to use this feature.
147150

0 commit comments

Comments
 (0)