OpenAPI: REST Controllers Generator

Generates implementations based on JDL models and OpenAPI definitions SpringMVC generated OpenAPI interfaces.

After you have generated SpringMVC interfaces and DTOs with OpenAPI generator, you can use this command to generate implementations (skeletons) and mappers for those interfaces and dtos:

jbang zw -p io.zenwave360.sdk.plugins.OpenAPIControllersPlugin
openapiFile=src/main/resources/model/openapi.yml \
zdlFile=src/main/resources/model/orders-model.jdl \
basePackage=io.zenwave360.example \
openApiApiPackage=io.zenwave360.example.adapters.web \
openApiModelPackage=io.zenwave360.example.adapters.web.model \
openApiModelNameSuffix=DTO \
targetFolder=.

Configuring OpenAPI Generator for Compatibility

Default options for https://openapi-generator.tech/docs/generators/spring/ have changes over time and different versions. Here is a sample configuration that works with this version of ZenWave360 SDK.

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.8.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/public/apis/openapi.yml</inputSpec>
<skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
<generatorName>spring</generatorName>
<apiPackage>${openApiApiPackage}</apiPackage>
<modelPackage>${openApiModelPackage}</modelPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<addCompileSourceRoot>true</addCompileSourceRoot>
<generateSupportingFiles>false</generateSupportingFiles>
<typeMappings>
<typeMapping>Double=java.math.BigDecimal</typeMapping>
</typeMappings>
<configOptions>
<useSpringBoot3>true</useSpringBoot3>
<documentationProvider>none</documentationProvider>
<openApiNullable>false</openApiNullable>
<useOptional>false</useOptional>
<useTags>true</useTags>
<interfaceOnly>true</interfaceOnly>
<skipDefaultInterface>true</skipDefaultInterface>
<delegatePattern>false</delegatePattern>
<sortParamsByRequiredFlag>false</sortParamsByRequiredFlag><!-- this is important -->
</configOptions>
</configuration>
</execution>
</executions>
</plugin>

Also, make sure you set required: true for requestBody in your OpenAPI definition for post, put, patch...

requestBody:
required: true # this is important
content:
application/json:
schema:
$ref: "#/components/schemas/Customer"

Plugin Options

OptionDescriptionTypeDefaultValues
openapiFileOpenAPI Specification FileString
zdlFileZDL file to parseString
targetFolderTarget folder to generate code to. If left empty, it will print to stdout.File
styleProgramming StyleProgrammingStyleimperativeimperative, reactive
operationIdsOpenAPI operationIds to generate code forList[]
statusCodesStatus codes to generate code forList[200, 201, 202, 400]
openApiApiPackageThe package to used by OpenAPI-Generator for generated api objects/classesString
openApiModelPackageThe package to used by OpenAPI-Generator for generated model objects/classesString{{openApiApiPackage}}
openApiModelNamePrefixSets the prefix for model enums and classes used by OpenAPI-GeneratorString
openApiModelNameSuffixSets the suffix for model enums and classes used by OpenAPI-GeneratorString
basePackageApplications base packageString
controllersPackageThe package to generate REST ControllersString{{basePackage}}.adapters.web
entitiesPackagePackage where your domain entities areString{{basePackage}}.core.domain
inboundDtosPackagePackage where your inbound dtos areString{{basePackage}}.core.inbound.dtos
servicesPackagePackage where your domain services/usecases interfaces areString{{basePackage}}.core.inbound
inputDTOSuffixShould use same value configured in BackendApplicationDefaultPlugin. Whether to use an input DTO for entities used as command parameter.String
includeControllerTestsInclude Controller Unit tests (using ServicesInMemoryConfig)booleantrue
paginatedDtoItemsJsonPathJSONPath list to search for response DTO schemas for list or paginated results. User '$.items' for lists or $.properties.<content property>.items for paginated results.List[$.items, $.properties.content.items]
formatterCode formatter implementationFormattersspringgoogle, palantir, spring, eclipse
skipFormattingSkip java sources output formattingbooleanfalse
haltOnFailFormattingHalt on formatting errorsbooleantrue
continueOnZdlErrorContinue even when ZDL contains fatal errorsbooleantrue

Getting Help

jbang zw -p io.zenwave360.sdk.plugins.OpenAPIControllersPlugin --help