Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][JavaSpring] Enum default value is only set if defined inline in schema #1981

Closed
5 of 6 tasks
ajdergute opened this issue Jan 25, 2019 · 9 comments
Closed
5 of 6 tasks

Comments

@ajdergute
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used? (3.3.4)
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Depending if an enum is specified inline or not a the specified default value is assigned (or not). If defined inline (first case) a default value is assigned, as expected. If the enum is referenced via $ref, then 'null' is assigned, which should be changed.

openapi-generator version

As far as i know it's not a regression and I use OpenAPI Generator Version 3.2.3 and 3.3.4.

OpenAPI declaration file content or url

in both cases:

paths:
  /testenum:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TestObject'

works:

components:
  schemas:
    TestObject:
      type: object
      properties:
        myEnum:
          type: string
          default: FIRSTVALUE
          enum:
            - FIRSTVALUE
            - SECONDVALUE

and produces:

public class TestObjectDto {
  @JsonProperty("myEnum")
  private MyEnumEnum myEnum = MyEnumEnum.FIRSTVALUE;
}

doesn't work:

components:
  schemas:
    TestObject:
      type: object
      properties:
        myEnum:
          $ref: '#/components/schemas/MyEnum'
...
    MyEnum:
      type: string
      default: FIRSTVALUE
      enum:
      - FIRSTVALUE
      - SECONDVALUE

and produces:

public class TestObjectDto {
  @JsonProperty("myEnum")
  private MyEnumDto myEnum = null;
}
Command line used for generation

I used the maven generator plugin as follows:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>3.3.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <inputSpec>src/main/resources/swagger/api.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <invokerPackage>${project.groupId}.service</invokerPackage>
                            <apiPackage>${project.groupId}.service.api</apiPackage>
                            <modelPackage>${project.groupId}.service.model</modelPackage>
                            <modelNameSuffix>Dto</modelNameSuffix>
                            <configOptions>
                                <sourceFolder>src/main/java</sourceFolder>
                                <library>spring-boot</library>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <useTags>true</useTags>
                                <interfaceOnly>true</interfaceOnly>
                                <delegatePattern>true</delegatePattern>
                                <swaggerDocketConfig>false</swaggerDocketConfig>
                                <configPackage>${project.groupId}.config</configPackage>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

Create an enum as described and see if default value is assigned.

Related issues/PRs
Suggest a fix

The expected behaviour in second case should be an default value assigment of the enum class like so:
private MyEnumDto myEnum = MyEnumDto.FIRSTVALUE;

@jmini
Copy link
Member

jmini commented Jan 25, 2019

Thank you for opening this issue!

I think this is the same issue as in #1927, toDefaultValue in AbstractJavaCodegen needs to unalias the schema.

@jmini
Copy link
Member

jmini commented Jan 28, 2019

I have pushed #2001 to solve this issue.

@jmini
Copy link
Member

jmini commented Jan 31, 2019

PR #2001 was merged, can you try with 4.0.0-SNAPSHOT and let us a comment here?

I am closing this issue. please reopen if you need anything.

@jmini jmini closed this as completed Jan 31, 2019
@ajdergute
Copy link
Author

Thank's a lot for this superfast bug fix. I tried it today with 4.0.0-beta2 and it works.

@jmini
Copy link
Member

jmini commented Feb 12, 2019

Some follow up is requested: see #2127

@tek08
Copy link
Contributor

tek08 commented Jan 22, 2021

I think this issue has re-emerged, at least on python generated code? Fwiw, I'm usually using 4.3.1, but have also tried 5.0.0, for the sake of debugging, and found the same issue occurring.

@IvanShymanouski
Copy link

I think this issue has re-emerged, at least on python generated code? Fwiw, I'm usually using 4.3.1, but have also tried 5.0.0, for the sake of debugging, and found the same issue occurring.

I believe it's also reproducible for C# as well. I tried inline definition like described in the bug and it works (but the enum, in the case, is a part of the class where the property defined. And it's not what we want to have), but regular usage with separate Enum type defined doesn't work

@moikot
Copy link

moikot commented Apr 8, 2021

This issue is reproducible in C# again.

@adrien-marsoulaud
Copy link

adrien-marsoulaud commented Sep 22, 2021

The issue is also on Java again since 5.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants