OpenAPI Generator - Lab 2
1. Generate the Client Api
Steps
- Open the
pom.xml
in the project root folder - Go to the
<build> -> <plugins>
section and locate the one within theopenapi-generator-maven-plugin
asartifactId
- After the
<version>
section, add the following code<executions> <execution> <id>generate-client-api-code</id> <goals> <goal>generate</goal> </goals> <phase>generate-sources</phase> <configuration> <inputSpec> ${project.build.directory}/openapiSpecs/credit-api.yaml </inputSpec> <invokerPackage>com.eliasnogueira.credit.invoker</invokerPackage> <apiPackage>com.eliasnogueira.credit.api</apiPackage> <modelPackage>com.eliasnogueira.credit.model</modelPackage> <generatorName>java</generatorName> <generateApiTests>false</generateApiTests> <generateModelTests>false</generateModelTests> <configOptions> <library>rest-assured</library> <serializationLibrary>jackson</serializationLibrary> </configOptions> </configuration> </execution> </executions>
- Execute the following command in the Terminal
Expected results
- The build will be successful
- You will see the following log related to the
openapi-generator-maven-plugin
[INFO] --- openapi-generator-maven-plugin:6.2.1:generate (generate-client-api-code) @ credit-api-tests --- [INFO] Generating with dryRun=false [INFO] Output directory (YOUR_DIR/credit-api-tests/target/generated-sources/openapi) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated. [INFO] OpenAPI Generator: java (client) [INFO] Generator 'java' is considered stable. [INFO] Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac) [INFO] NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI). [INFO] Processing operation OPERATION [INFO] writing file FILE
- The Client Api and Model classes will be created in the following structure
Additional post-step
- Open the
target
folder - Right-click the
generated-sources
folder - Select
Mark directory as -> Generated Sources Root
Why we do have the additional post-step?
The openapi-generator-maven-plugin
is generating a Java project in the target/generated-sources
directory.
We need to tell the IDE to consider that folder as "sources" inferring that we will use the code inside it in the project.