Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Can't generate only one specific endpoint of API #17653

Closed
altrof opened this issue Nov 27, 2023 · 11 comments · Fixed by #17859
Closed

[BUG] Can't generate only one specific endpoint of API #17653

altrof opened this issue Nov 27, 2023 · 11 comments · Fixed by #17859
Assignees

Comments

@altrof
Copy link

altrof commented Nov 27, 2023

🐛 Bug Report:

Describe the bug

I decided migrate to OpenAPI Generator from Swagger Codegen.
In Swagger Codegen was possible generate specific endpoint of API by providing operationId with separator '#':
"apis": "ContractSearch#findContractByCustomerId"

So here is my Gradle task and setup for OpenAPI Generator:

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"
}

Expected behavior

Generate only one specified endpoint. But in OpenAPI Generator it doesn't work. Only if whole api "apis": "ContractSearch", then it will generate.

Package System (versions):

  • OpenAPI Generator 6.6.0
  • Java 17
  • Spring Boot 2.7

Also I wrote question on stackoverflow:
https://stackoverflow.com/questions/77557530/openapi-generator-generate-only-one-specific-endpoint-from-api

Maybe it is not a bug, just doesn't have this functionality.

@wing328 wing328 transferred this issue from OpenAPITools/openapi-generator-cli Jan 19, 2024
@wing328
Copy link
Member

wing328 commented Jan 19, 2024

so your goal is to generate the output (client, server, etc) using just one operation (based on operationId) in the openapi doc/spec, right?

@altrof
Copy link
Author

altrof commented Feb 9, 2024

yes, absolutely right. e.g. controller CustomerApi has many endpoints and I want to generate in the another service only one CustomerApi endpoint /create-customer-relation.
In swagger it was possible: CustomerApi#createCustomerRelation, because in CustomerApi it has annotation @ApiOperation with nickname = "createCustomerRelation", which convert into operationId
Hope that you understand my goal. :)

@wing328
Copy link
Member

wing328 commented Feb 13, 2024

we don't offer that at the moment.

may I know if you've time to contribute this enhancement or sponsor this feature financially?

@altrof
Copy link
Author

altrof commented Feb 13, 2024

Yes, I can try to create this feature (contribute).
Will create issue, if the similar is didn't yet create.

@wing328
Copy link
Member

wing328 commented Feb 13, 2024

can you please PM me via Slack to further discuss this when you've time?

https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g

@wing328
Copy link
Member

wing328 commented Feb 14, 2024

i've filed #17859 to add a new rule called FILTER with a list of operationId as the input.

my local tests look good so far.

can you also give it a try when you've time?

@wing328
Copy link
Member

wing328 commented Feb 15, 2024

PR merged. please give it a try with the latest master.

@orolhawion
Copy link
Contributor

orolhawion commented Jul 5, 2024

The same would be very useful to use with tags. I tried FILTER=tags:desired|api but it did not work. The use case is, I have one large openApi.yaml with many endpoints categorized with tags. I only need a client and/or server for endpoints that have specific tags.

@altrof
Copy link
Author

altrof commented Jul 5, 2024

I also thought about it and did it with tricky way in gradle:

gradle closure:

def addApiArgs(String... apisWithEndpoints) {
    def apis = []
    def endpointFilters = []

    apisWithEndpoints.each { apiWithEndpoints ->
        def (api, endpoints) = apiWithEndpoints.split('#')
        apis << api
        endpointFilters.addAll(endpoints.split('\\|').collect { "$it" })
    }

    return ["--global-property", "apis=${apis.join(':')}", "--openapi-normalizer", "FILTER=operationId:${endpointFilters.join('|')}"]
}

gradle tasks:

tasks.register('generateReportingApiClient', JavaExec) {
    configure openApiGeneratorConfig
    args '-i', 'https://reporting-finance-dev.local/api-docs'
    args '-c', "$configFilesRoute/reporting.json"

    args addApiArgs("Reporting#generateReportsForDate|sync|runChangeSystemDate")
}

@orolhawion
Copy link
Contributor

I am afraid I cannot do this with maven.

@altrof
Copy link
Author

altrof commented Jul 5, 2024

ChatGPT:

Add the OpenAPI Generator Plugin to your pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>5.1.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <inputSpec>https://reporting-finance-dev.local/api-docs</inputSpec>
                <configFile>${project.basedir}/config/reporting.json</configFile>
                <globalProperties>
                    <apis>${apis}</apis>
                    <openapiNormalizer>FILTER=operationId:${endpoints}</openapiNormalizer>
                </globalProperties>
            </configuration>
        </plugin>
    </plugins>
</build>

Define Profiles to Configure the API and Endpoints

<profiles>
    <profile>
        <id>generate-reporting-api-client</id>
        <properties>
            <apis>Reporting</apis>
            <endpoints>generateReportsForDate|sync|runChangeSystemDate</endpoints>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-client</id>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <inputSpec>https://reporting-finance-dev.local/api-docs</inputSpec>
                                <configFile>${project.basedir}/config/reporting.json</configFile>
                                <globalProperties>
                                    <apis>${apis}</apis>
                                    <openapiNormalizer>FILTER=operationId:${endpoints}</openapiNormalizer>
                                </globalProperties>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

maybe it will be work :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants