OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.8k stars 6.58k forks source link

[BUG][JAVA] oneOf discriminator type string with enum ignores model prefix and suffix names #16391

Open oleh-hudyma opened 1 year ago

oleh-hudyma commented 1 year ago

Bug Report Checklist

Description

Using version 7.00-beta, when you try to generate oneOf with discriminator property referencing to enum, then everything works fine, but once you add generate model suffix using modelNameSuffix, compilation is failing, because suffix is not added because parent model is using enum name without suffix.

openapi-generator version

7.0.0-beta

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: transactions
  description: transactions
  version: 1.0.0

tags:
  - name: transactions

paths:
  /v1/transactions:
    post:
      tags:
        - transactions
      operationId: requestTransaction
      summary: Create transaction
      requestBody:
        description: transaction
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TransactionCreateRequest"
      responses:
        200:
          description: Created transaction
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/TransactionCreateRequest"

components:
  schemas:
    TransactionCreateRequest:
      type: object
      required:
        - assets
      properties:
        assets:
          $ref: "#/components/schemas/TransactionAssets"

    TransactionAssets:
      type: object
      discriminator:
        propertyName: type
        mapping:
          CASH: "#/components/schemas/TransactionCashAssets"
          ITEMS: "#/components/schemas/TransactionItemsAssets"
      oneOf:
        - $ref: "#/components/schemas/TransactionCashAssets"
        - $ref: "#/components/schemas/TransactionItemsAssets"
      properties:
        type:
          $ref: "#/components/schemas/TransactionAssetsType"

    TransactionCashAssets:
      type: object
      required:
        - type
        - amount
      properties:
        type:
          $ref: "#/components/schemas/TransactionAssetsType"
        amount:
          type: number

    TransactionItemsAssets:
      type: object
      required:
        - type
        - items
      properties:
        type:
          $ref: "#/components/schemas/TransactionAssetsType"
        items:
          type: number

    TransactionAssetsType:
      type: string
      enum:
        - CASH
        - ITEMS
Generation Details
                    <execution>
                        <id>client</id>
                        <phase>none</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/../api.yaml</inputSpec>

                            <generatorName>java-enhanced</generatorName>
                            <library>webclient</library>

                            <modelNameSuffix>Dto</modelNameSuffix>

                            <generateApis>true</generateApis>
                            <generateApiTests>false</generateApiTests>
                            <generateModels>true</generateModels>
                            <generateModelTests>false</generateModelTests>

                            <configOptions>
                                <oas3>true</oas3>
                                <java7>true</java7>

                                <aotHintsEnabled>true</aotHintsEnabled>

                                <dateLibrary>java8-localdatetime</dateLibrary>
                                <interfaceOnly>true</interfaceOnly>

                                <useJakartaEe>true</useJakartaEe>
                                <useSpringBoot3>true</useSpringBoot3>

                                <useTags>true</useTags>

                                <delegatePattern>true</delegatePattern>

                                <!--suppress UnresolvedMavenProperty -->
                                <additionalModelTypeAnnotations>@lombok.Builder @lombok.AllArgsConstructor
                                </additionalModelTypeAnnotations>
                            </configOptions>
                        </configuration>
                    </execution>
Steps to reproduce

mvn clean install

Receive error: [41,12] cannot find symbol [ERROR] symbol: class TransactionAssetsType [ERROR] location: interface com.steam.trading.api.transactions.dto.TransactionAssetsDto

Related issues/PRs

Feature was added by https://github.com/OpenAPITools/openapi-generator/issues/12412

Suggest a fix

Add logic to add suffix and prefix in templates when using enum discriminator

oleh-hudyma commented 1 year ago

@wing328 since you worked at https://github.com/OpenAPITools/openapi-generator/pull/15978, is this bug something you can take a look at?

wing328 commented 1 year ago

Just merged #16684

Please pull the latest master to give it a try.