How to name classes created by OpenAPI Generator and get Map in response?
Asked Answered
W

1

5

Using OpenApi generator with Spring Boot

Here is my translation.yml file:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: http://localhost:8088/
    description: Generated server url
paths:
  /api/translation/{module}/{locale}:
    get:
      tags:
        - translations
      summary: Get translations by module, locale and tenantId
      operationId: getTranslations
      parameters:
        - name: module
          in: path
          required: true
          schema:
            type: string
        - name: locale
          in: path
          required: true
          schema:
            type: string
        - name: tenantId
          in: query
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Get the translations
          content:
            application/json:
              schema:
                type: object
                additionalProperties:
                  type: string
              example:
                nav:
                  payments:
                    invoice:
                      details:
                        title: Payments Details
                        all: AllPayments
                      orders: Orders
                      dashboard: Dashboard
                    title: Payments
                  home:
                    title: Home

and here is pom.xml:

           <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/translation.yml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>test.inbound_api</apiPackage>
                            <modelPackage>test.inbound_api.model</modelPackage>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  1. The name of generated interface is ApiApi.java. How to generate something like TranslationApi.java? RESOLVED
  2. The generated interface method returns ResponseEntity<Map<String, String>> how to make it return ResponseEntity<Map<String, Object>>? RESOLVED
  3. How to correctly provide the example with tree structure? In swaggerUi I get now Example Value "string". The example in my code displayed correctly in Swagger Editor, but not present in the generated interface class. That's what I want to achieve: enter image description here
With answered 20/4, 2023 at 22:21 Comment(0)
M
7
  1. extend your config options to
...
<configOptions>
 <useTags>true</useTags>
 <interfaceOnly>true</interfaceOnly>
</configOptions>

Then the tag will be used to generate the name

  1. in your openapi-definition you have currently specified a dictionary that has a string as a value. Use
additionalProperties:
  type: object

to get ResponseEntity<Map<String, Object>>

  1. I'm not entirely sure what you're trying to accomplish here. Maybe the invalid specification in 2. caused this as well?
Musaceous answered 20/4, 2023 at 23:39 Comment(7)
If you copy-paste this translation.yml to Swagger Editor you will see the tree structure in the Example Value Schema textarrea. That is what I'm trying to accomplish. It works in Swagger Editor, but not generated in sources.With
1. and 2. resolved, thanks! But Example still not generated.With
Where do you expect it to be generated? If you want an interface that matches your example, I don't think the generator does that, you'll need to specify a components/schema for thatMusaceous
I just want that it will be displayed in the Swagger UI.With
Looks to me like that's not possible, since the openapi-gernator does not generate the @ExampleObjectMusaceous
@Micro, I was able to get the generated class names changed by using <useTags> and adding the tag to the spec. I do see the method names in the generated classes are still the long form version of the path in the spec. I'm being nit picky, but is there a way to get the class name AND method names to take the tag?Maneating
Looks like the operationId field will define the method namesManeating

© 2022 - 2024 — McMap. All rights reserved.