OpenAPITools / OpenAPITools/openapi-generator

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

Open
#9,858 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with generate-models.sh, java-config.json, and the provided OpenAPI specification, then reproduce the Java native generation using openapi-generator-cli 5.1.1. Check the generated models and compilation result for the anyOf preference schema; done means the expected AnyOfAWSPreferenceAzurePreference class is generated and the project builds successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.