OpenAPITools / OpenAPITools/openapi-generator

[BUG] descriminator cannot be an enum

Open
#20,581 1 comment 1 reaction 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

Description

I was trying to generate objects from a schema that uses a descriminator that has a type of string but is restricted by an enum. I boiled this down using a simple example of a Dog and Cat that are a type of Animal (using allof). The Animal class has a type attribute which is a string. Now if I then have another class that has an attribute of animal with a oneOf Dog, Cat and a descriminator of type then this appears to generate correctly. However if I restrict the type in animal to "Dog, Cat" via an enum, then the generated model contains errors as the Animal class return an enum for the type but the class that references animal still thnks this is a string.

This isn't the exact issue (I was generating openADR 3.0 schema) but I've tried to simplify it.

openapi-generator version

7.11.0

OpenAPI declaration file content or url
openapi: 3.0.0
servers:
  - description: base
    url: url
info:
  title: example
  version: "3.0.1"

components:
  schemas:
    MyPet:
      type: object
      properties:
        type:
          $ref: '#/components/schemas/AnimalTypes'
        animal:
          oneOf:
            - $ref: '#/components/schemas/Dog'
            - $ref: '#/components/schemas/Cat'
          discriminator:
             propertyName: type
    AnimalTypes:
      type: string
      enum: [Dog, Cat]
    Animal:
      type: object
      discriminator:
        propertyName: type
      required:
        - type
      properties:
        type: 
          type: string
          $ref: '#/components/schemas/AnimalTypes'
          
    Dog:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            breed:
              type: string
    Cat:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            color:
              type: string
        
    
Generation Details

using the following pom to build

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.11.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
			${project.basedir}/src/main/resources/api/issue-api.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <cleanupOutput>true</cleanupOutput>
                <configOptions>
                    
		
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <interfaceOnly>true</interfaceOnly>
                    <useJakartaEe>true</useJakartaEe>
		
                </configOptions>
                <apiPackage>org.psm.data.api</apiPackage>
                <modelPackage>org.psm.data.model</modelPackage>
                <generateApis>true</generateApis>
                <generateSupportingFiles>false</generateSupportingFiles>
                <generateApiDocumentation>false</generateApiDocumentation>
                <output>${project.basedir}/generated-sources/</output>
                <!--<templateDirectory>${project.basedir}/src/main/resources/openapi-templates</templateDirectory>-->
	<skipValidateSpec>true</skipValidateSpec>
            </configuration>
        </execution>
    </executions>
</plugin>
Steps to reproduce

Generate code and note that the Cat and Dog class do not compile

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 the supplied OpenAPI YAML and Maven spring-generator configuration, then reproduce the failure described in the issue: generated Cat and Dog classes do not compile. Inspect the generated models to identify the mismatch between the AnimalTypes enum and the discriminator's string handling; done means the generated classes compile correctly for this schema.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.