question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[java] Compilation failure when 'discriminator' is introduced

See original GitHub issue
Description

I got the following error when generate code by using openapi-generator-maven-plugin.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project swagger-vnfm: Compilation failure
[ERROR] /swagger-vnfm/target/generated-sources/src/main/java/com/openapi/test/VimConnectionInfo.java:[43,49] incompatible types: java.lang.String cannot be converted to com.openapi.test.VimType
openapi-generator version

<version>3.2.0</version>

OpenAPI declaration file content or url
swagger: '2.0'
info:
  description: This spec is mainly for testing
  version: 1.0.0
  title: Swagger Petstore
  termsOfService: 'http://swagger.io/terms/'
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache-2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: 'petstore.swagger.io:80'
basePath: /v2
paths:
  /test:
    get:
      description: Gets all operation occurrence resources.
      responses:
        '200':
          description: Successful response
          schema:
            type: string
        default:
          description: Error payload
          schema:
            type: string
      tags:
        - test
schemes:
  - http
definitions:
  VimType:
    description: Type of the VIM info structure.
    type: string
    enum:
      - OPENSTACK_V2
      - OPENSTACK_V3
      - VMWARE_VCLOUD
      - OTHER_VIM_INFO
  VimConnectionInfo:
    type: object
    required:
      - id
      - vimType
    discriminator: vimType
    properties:
      id:
        description: Identifier of the VIM
        type: string
      vimType:
        $ref: '#/definitions/VimType'
externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'
Command line used for generation

mvn clean compile

Steps to reproduce

The maven pom.xml is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.vnfm.model</groupId>
    <artifactId>swagger-vnfm</artifactId>
    <packaging>jar</packaging>
    <version>DYNAMIC-SNAPSHOT</version>
    <name>swagger-vnfm</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-core-version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson-version}</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <swagger-core-version>1.5.21</swagger-core-version>
        <gson-version>2.8.1</gson-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>lcm-generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger.yaml</inputSpec>
                            <language>java</language>
                            <generateApis>false</generateApis>
                            <generateModelTests>false</generateModelTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <skipOverwrite>true</skipOverwrite>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                            </configOptions>
                            <output>${project.build.directory}/generated-sources</output>
                            <modelPackage>com.openapi.test</modelPackage>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Related issues/PRs

No

Suggest a fix/enhancement

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
antwebcommented, Feb 4, 2020

Using an enum descriminator is still broken on 4.2.3 and the current master (4.3.0-SNAPSHOT).

I built a simple example based on the OpenAPI docs:

components:
  schemas:
    Pet:
      oneOf:
        - $ref: "#/components/schemas/Cat"
        - $ref: "#/components/schemas/Dog"
        - $ref: "#/components/schemas/Lizard"
      discriminator:
        propertyName: petType
        mapping:
          Cat: "#/components/schemas/Cat"
          Dog: "#/components/schemas/Dog"
          Lizard: "#/components/schemas/Lizard"

    Cat:
      type: object
      properties:
        petType:
          $ref: "#/components/schemas/PetType"
        name:
          type: string

    Dog:
      type: object
      properties:
        petType:
          $ref: "#/components/schemas/PetType"
        bark:
          type: string

    Lizard:
      type: object
      properties:
        petType:
          $ref: "#/components/schemas/PetType"
        lovesRocks:
          type: boolean

    PetType:
      type: string
      enum:
        - Cat
        - Dog
        - Lizard

Generated with

java -jar openapi-generator-cli.jar generate \
  -i api.yaml \
  --api-package 'com.test.api' \
  --model-package 'com.test.model' \
  -g java \
  --library 'retrofit2' \
  -o generated/

The issue seems to be this line in the constructor of the base type (Pet):

this.petType = this.getClass().getSimpleName();

which doesn’t compile unless petType is a String. The fix should be rather simple. Whenever the discriminator is an enum, the constructor should be something like this:

this.petType = PetType.fromValue(this.getClass().getSimpleName());
0reactions
RevealOscarcommented, Dec 9, 2022

Same here as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing with Jackson (fail on unknown properties) does ...
java - Deserializing with Jackson (fail on unknown properties) does not ignore discriminator property (DTO's created with SwaggerCodegen) - ...
Read more >
Common Problems | Machine Learning - Google Developers
Research has suggested that if your discriminator is too good, then generator training can fail due to vanishing gradients.
Read more >
Invalid column index issue with Discriminator - Google Groups
I'm seeing some unusual behavior after converting to Lombok for objects that use the discriminator pattern. Not 100% sure this is Lombok causing...
Read more >
Full Text Bug Listing - Bugs - Eclipse
generateElementValueForNonConstantExpression(EclipseSourceType.java:809) [ERROR] at org.aspectj.ajdt.internal.compiler.lookup.EclipseSourceType.
Read more >
jsoniter-scala
Scala macros for compile-time generation of safe and ultra-fast JSON codecs ... had started from macros that reused jsoniter (json-iterator) for Java reader ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found