Search code examples
javaspring-bootgradleopenapiopenapi-generator

Generate only one specific endpoint from API


My current setup for OpenAPI Generator in build.gradle.

def openApiGeneratorConfig = {
    classpath configurations.openApiGenerator
    systemProperties = ["apis": "", "models": ""]
    mainClass = 'org.openapitools.codegen.OpenAPIGenerator'
    args 'generate'
    args '-o', "$rootDir/external"
    args '-t', "$rootDir/external/src/main/resources/codegen/templates/"
    args '--skip-validate-spec'
    args "--import-mappings", "Instant=java.time.Instant,YearMonth=java.time.YearMonth,LocalDateTime=java.time.LocalDateTime"
    args "--type-mappings", "double=BigDecimal"
}

def configFilesRoute = "$rootDir/external/src/main/resources/config/swagger"

task generateExternalApiClient(type: JavaExec) {
    configure openApiGeneratorConfig
    systemProperties = ["apis": "ContractSearch#findContractByCustomerId"]
    args "-i", "http://localhost:9901/v2/api-docs"
    args "-c", "$configFilesRoute/loan.json"
}

In ContractSearch (Api) I have a lot of endpoints (operations) and I need to generate only one specific endpoint. So before this (migrate to OpenAPI Generator), I used Swagger CodeGen and if I specified the operation after api with using symbol # as separator, then it worked fine, and it generated only one specific endpoint. But now after migrating to OpenAPI Generator, it is not work in this behaviour. I googled and also checked documentation, didn't find any answer to my question.

Java 17. Spring Boot 2.7, Gradle 8.2, OpenAPI Generator CLI 6.6.0

I tried adding difference arguments in OpenAPI config, but also didn't help me.


Solution

  • Since OpenApi generator version 7.4 you can use openapiNormalizer of type FILTER specifying which endpoints to generate. One of convenient methods is to filter endpoints by operationId. See documentation for more information.

    java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/java-okhttp/ --openapi-normalizer FILTER="operationId:addPet|getPetById"
    

    To your example you need to add

    args "--openapi-normalizer", "FILTER=operationId:myEndpoint"
    

    For reference maven plugin would be:

      <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>7.4.0</version>
            <executions>
              <execution>
                <id>generate-server</id>
                <goals>
                  <goal>generate</goal>
                </goals>
                <configuration>
                  ...
                  <openapiNormalizer>FILTER=operationId:createNamesContactPerson</openapiNormalizer>
                  <configOptions>
                   ...
                  </configOptions>
                </configuration>
              </execution>
            </executions>
          </plugin>