openapi-generator
openapi-generator copied to clipboard
[BUG][JAVA] "jaxrs-spec" generator creates invalid nested enums with type number
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
If the OpenAPI definition contains a an enum of type number directly in the properties, an invalid Java enum is generated and the code does not compile. If the exact same enum is references however, the code is correct.
openapi-generator version
jaxrs-spec:7.5.0
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: test
version: 0.0.1
paths:
/test:
get:
responses:
200:
description: test
content:
application/json:
schema:
type: object
properties:
inline:
type: number
enum: [ 0, 1, 2 ]
referenced:
$ref: '#/components/schemas/ReferencedEnum'
components:
schemas:
ReferencedEnum:
type: number
enum: [ 0, 1, 2 ]
Generation Details
I created a standard Maven project and ran it with mvn clean compile
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.5.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/test.yaml</inputSpec>
<generatorName>jaxrs-spec</generatorName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Steps to reproduce
Running the above, ReferencedEnum is valid:
public enum ReferencedEnum {
NUMBER_0(new BigDecimal("0")),
NUMBER_1(new BigDecimal("1")),
NUMBER_2(new BigDecimal("2"));
private BigDecimal value;
// ...
}
However the nested InlineEnum is references a method that does not exist:
public enum InlineEnum {
NUMBER_0(BigDecimal.valueOf(new BigDecimal("0"))),
NUMBER_1(BigDecimal.valueOf(new BigDecimal("1"))),
NUMBER_2(BigDecimal.valueOf(new BigDecimal("2")));
private BigDecimal value;
// ...
}
The method BigDecimal.valurOf(BigDecimal) does not exist, which gives a compilation error.
Related issues/PRs
none
Suggest a fix
Generate the same enum code no matter whether it is a separate Schema or not.