[BUG][JavaSpring] Enum default value is only set if defined inline in schema
See original GitHub issueBug 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;
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Choosing the default value of an Enum type without having to ...
The answer is nothing. C# has no problem with MyEnum e having a value of zero even though the enumeration doesn't define it....
Read more >CA1008: Enums should have zero value (code analysis) - .NET
The default value of an uninitialized enumeration, just like other value types, is zero. A non-flags-attributed enumeration should define a ...
Read more >Guide - Enum TypeScript & JavaScript Tutorial
The first act is the creation of an object called itemCategory in line 1. This will have the value undefined. It will be...
Read more >C++ enum : Learn about defining, declaring, an ... - CodesDope
Values of the Members of Enum ... All the elements of an enum have a value. By default, the value of the first...
Read more >ENUM statement - Progress Documentation
Generally, you do not need to worry about the numerical values, but you can specify one or more of the values explicitly if...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The issue is also on Java again since 5.1.0
This issue is reproducible in C# again.