Given the following properties
of a Request Body:
...
post:
requestBody:
content:
'multipart/form-data':
schema:
type: object
properties:
foo:
type: string
bar:
type: boolean
baz:
type: integer
...
The OpenAPI Generator for Maven (7.4.0) configuration follows (paying attention to the generatorName
and configOptions:useSpringBoot3
values, spring
and true
, respectively):
...
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger/api.yml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.company.service.web.api</apiPackage>
<modelPackage>com.company.service.api.dto</modelPackage>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<importMappings>
<importMapping>Problem=org.zalando.problem.Problem</importMapping>
<importMapping>OffsetDateTime=java.time.Instant</importMapping>
</importMappings>
<typeMappings>
<typeMapping>OffsetDateTime=java.time.Instant</typeMapping>
</typeMappings>
<skipValidateSpec>false</skipValidateSpec>
<globalProperties>
<skipFormModel>false</skipFormModel>
</globalProperties>
<configOptions>
<delegatePattern>true</delegatePattern>
<title>my-service</title>
<useSpringBoot3>true</useSpringBoot3>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
...
This generates DTOs effectively enough and mostly as expected with accessors and mutators, as well as some nice chain methods, but for one annoyance: there isn’t a way to get all the properties of the DTO as a Map<String, Object>
. This feels like it could be a configuration option in the generator, maybe, but not default.
Currently, I’ve written a private method to my implementation that uses Java Reflection to get the accessors and Map their values with invoke
. But… is there a cleaner way in the use of the generator?