Gradle Plugin
The openapi-processor-gradle Gradle plugin can run any of the openapi-processor's.
To use it in a Gradle project, the Gradle file of the project requires a few additional instructions. The following sections describe how to activate and configure openapi-processor-spring in a build.gradle file.
adding the plugin
To activate the plugin, add it to (like any other Gradle plugin) the plugins configuration:
plugins {
....
// add openapi-processor-gradle plugin
id 'io.openapiprocessor.openapi-processor' version '<version>'
}
configuring processor-spring
The plugin will add an openapiProcessor configuration block used to configure the processors. Configuration for a specific processor belongs inside it with the processor name as configuration block name.
openapiProcessor {
spring {
processor 'io.openapiprocessor:openapi-processor-spring:<version>'
apiPath "$projectDir/src/api/openapi.yaml"
targetDir "$projectDir/build/openapi"
mapping "$projectDir/mapping.yaml"
parser "INTERNAL"
}
}
-
processor: (required) the processor dependency. This works in the same way as adding a dependency to a configuration in the gradledependenciesblock. It is given here to avoid unwanted side effects on the build dependencies of the project. -
apiPath: (required) the path to theopenapi.yamlfile and the main input for the processor. If set in the top-level block, it will be used for all configured processors. -
targetDir: (required) the output folder for generating interfaces and models. This is the parent of thepackageNamefolder tree. It is recommended to set this to a subfolder of gradle’s standardbuilddirectory, so it is cleared by thecleantask and does not pollute thesourcesdirectory.See running processor-spring how to include the
targetDirin compilation and packing. -
mapping: (required) provides the processor mapping options. This is a path to the YAML file. See Configuration for a description of the mapping YAML. This replaces thetypeMappingsoption. -
showWarnings: (optional)trueto show warnings from the open api parser orfalse(default) to show no warnings (this option has currently no effect). -
parser: (optional), sets the openapi parser used to read the OpenAPI description. Available values areSWAGGER,OPENAPI4JorINTERNAL(default).-
INTERNAL: internal OpenAPI parser, supports OpenAPI 3.0.x & OpenAPI 3.1.0. -
SWAGGER: Swagger OpenAPI parser, supports OpenAPI 3.0.x -
OPENAPI4J: openapi4j OpenAPI parser, supports OpenAPI 3.0.x. It provides better validation thanSWAGGER, unfortunately it is no longer maintained and is deprecated.-
the parser provides JSON schema validation.
-
-
running processor-spring
The plugin will add a gradle task processSpring to run the processor.
To automatically generate & compile the processor output two additional configurations are necessary.
-
the
sourceSetsare extended to include the processor output (assuming a java project):sourceSets { main { java { // add generated files srcDir 'build/openapi' } } } -
and the
compileJavatask gets a dependency onprocessSpring, so it runs before compilation (again, assuming a java project):// generate api before compiling compileJava.dependsOn ('processSpring')
Adding automatic compilation in this way will also automatically include the generated files into the jar build artifact.