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
22.04k stars 6.61k forks source link

[BUG][Java][Native] Lack of AnyOf class #9858

Open Planche95 opened 3 years ago

Planche95 commented 3 years ago

Bug Report Checklist

Description

When I'm trying to build code generated from the OpenApi spec file with "anyOf", it fails because of the lack of "AnyOfAWSPreferenceAzurePreference" class.

openapi-generator version

5.1.1

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Preferences Core API
  version: 1.0.0
tags:
  - name: Preferences
    description: Operations for preferences
security:
  - api_key: [ ]
paths:
  /preferences:
    get:
      summary: Returns preferences based on parameters
      tags:
        - Preferences
      parameters:
        - $ref: '#/components/parameters/Accept'
      responses:
        '200':
          $ref: '#/components/responses/GetPreferencesListResponse'

components:
  parameters:
    Accept:
      name: Accept
      in: header
      description: Defines acceptable resource representations
      required: false
      schema:
        type: string
        default: application/json

  responses:
    GetPreferencesListResponse:
      description: Describes the success response for the get preferences operation
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/PreferencesListWithIds'

  securitySchemes:
    api_key:
      type: apiKey
      name: x-api-key
      in: header
  schemas:
    PreferencesType:
      type: string
      enum:
        - AWS
        - AZURE
        - TERRAFORM
        - GITLAB

    AWSPreferenceType:
      type: string
      enum:
        - ACCESS_KEY_ID
        - ACCESS_KEY_SECRET

    AzurePreferenceType:
      type: string
      enum:
        - CLIENT_ID
        - CLIENT_SECRET
        - SUBSCRIPTION_ID
        - TENANT_ID

    PreferencesListWithIds:
      type: array
      items:
        $ref: '#/components/schemas/PreferencesWithId'

    PreferencesWithId:
      allOf:
        - properties:
           id:
            type: string
        - $ref: '#/components/schemas/Preferences'

    Preferences:
      properties:
        programId:
          type: string
        projectId:
          type: string
        labels:
          type: array
          items:
            type: string
        type:
          $ref: '#/components/schemas/PreferencesType'
        preferences:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/AWSPreference'
              - $ref: '#/components/schemas/AzurePreference'
      required:
        - type
        - preferences

    Preference:
      type: object
      properties:
        name:
          type: string
        value:
          type: string
        encrypted:
          type: boolean
      required:
        - encrypted
        - name
        - value

    AWSPreference:
      allOf:
        - $ref: '#/components/schemas/Preference'
        - type: object
          properties:
            name:
              $ref: '#/components/schemas/AWSPreferenceType'

    AzurePreference:
      allOf:
        - $ref: '#/components/schemas/Preference'
        - type: object
          properties:
            name:
              $ref: '#/components/schemas/AzurePreferenceType'
Generation Details
{
  "dateLibrary": "java8",
  "java8": true,
  "library": "native",
  "sourceFolder": "src/main/java",
  "serializationLibrary": "jackson"
}
Steps to reproduce

I'm using this script:

#!/usr/bin/env bash

[ ! -f openapi-generator-cli.jar ] && wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/5.1.1/openapi-generator-cli-5.1.1.jar -O openapi-generator-cli.jar

LAYER=$1
SERVICE_NAME=$2
SPEC_FILE=$3
VERSION=$4
MODULE_PACKAGE=$(echo "$LAYER.$SERVICE_NAME")
MODULE_ARTIFACT=$(echo "$LAYER-$SERVICE_NAME-spec-api")

TMP_DIR=$SERVICE_NAME

rm -fr $TMP_DIR
java -jar openapi-generator-cli.jar config-help -g java > help-java.txt
java -jar openapi-generator-cli.jar generate \
         --global-property models,modelTests=false,apis,supportingFiles \
         --additional-properties=apiPackage=io.$MODULE_PACKAGE.api \
         --additional-properties=artifactDescription=$SPEC_FILE \
         --additional-properties=artifactVersion=$VERSION \
         --additional-properties=artifactId=$MODULE_ARTIFACT \
         --additional-properties=groupId=io.$LAYER \
         --additional-properties=invokerPackage=io.$MODULE_PACKAGE.client \
         --additional-properties=modelPackage=io.$MODULE_PACKAGE.api \
         -i $SPEC_FILE \
         -g java \
         -c java-config.json \
         -o $TMP_DIR
cp -r .mvn $TMP_DIR

call it with:

sh generate-models.sh feature preferences Governance-Core-PreferencesAPI-Target-V1.0.yaml 1.0.0

Related issues/PRs
Suggest a fix
mimkorn commented 3 years ago

I had just tested it on latest 6.0.0 snapshot and 5.2.0 snapshot locally and it is not fixed in none of those.

I have the same issue.

Using this schema

    Error:
      type: object
      example:
        timestamp: '2021-05-19T16:56:04.969+00:00'
        status: 409
        error: Conflict
        message: Application already exits
        path: /public/subscriptions
      properties:
        timestamp:
          type: string
          format: date-time
        status:
          type: integer
        error:
          type: string
        message:
          type: string
        debugMessage:
          type: string
        path:
          type: string
        errors:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/ValidationError'
    ValidationError:
      title: ValidationError
      type: object
      description: ''
      properties:
        object:
          type: string
        field:
          type: string
        rejectedValue:
          type: object
        message:
          type: string

and this configuration of the maven plugin:

<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.0</version>
<executions>
    <execution>
        <id>Java Spring server stub generation</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <inputSpec>${project.basedir}/openapi/subscription.yaml</inputSpec>
            <generatorName>spring</generatorName>

            <generateApis>true</generateApis>
            <generateModels>true</generateModels>
            <generateModelDocumentation>false</generateModelDocumentation>
            <generateModelTests>false</generateModelTests>
            <generateSupportingFiles>true</generateSupportingFiles>

            <configOptions>
                <unhandledException>true</unhandledException>
                <library>spring-cloud</library>
                <openApiNullable>false</openApiNullable>
                <dateLibrary>java8</dateLibrary>
            </configOptions>
            <output>${project.build.directory}/generated-sources/java-server</output>
        </configuration>
    </execution>
</executions>
</plugin>
sfuhrm commented 3 years ago

I'm facing this problem with openapi-generator 5.2.1.