micronaut-projects / micronaut-projects/micronaut-openapi

[generator] Invalid default init value for integer enum properties

Open
#2,723 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
114
Forks
121
Avg merge
2d 17h
Merged PRs (30d)
25

Description

### Expected Behavior

```java
private MaxHours maxHours = MaxHours.NUMBER_48;
```

### Actual Behaviour

Generated Code
MaxHours.java: correct — constants use NUMBER_ prefix
```java
public enum MaxHours {
@JsonProperty("38") NUMBER_38(38),
@JsonProperty("39") NUMBER_39(39),
@JsonProperty("48") NUMBER_48(48),
...
}
```

MyModel.java: broken — default value uses _ prefix

```java
private MaxHours maxHours = MaxHours._48; // ❌ MaxHours._48 does not exist
```

### Steps To Reproduce

When an OpenAPI schema defines an integer enum with a default value, the generated field initializer references a non-existent enum constant. The enum class is generated using the `NUMBER_` prefix (e.g. `NUMBER_48`), but the default value expression uses an `_` prefix (e.g. `_48`), producing code that does not compile.

```yaml
openapi: 3.1.1
info:
title: Integer Enum Default Value Repro
version: 1.0.0
paths: {}
components:
schemas:
MaxHours:
type: integer
enum:
- 38
- 39
- 48
default: 48
MyModel:
type: object
properties:
maxHours:
$ref: "#/components/schemas/MaxHours"
```

```gradle
micronaut {
runtime("netty")
testRuntime("junit5")
openapi {
server(file("repro.openapi.yaml")) {
apiPackageName = "com.repro.api"
modelPackageName = "com.repro.api.model"
}
}
}
```

The same mismatch also occurs when `x-enum-varnames` is used to provide custom names (e.g. H48). The enum class correctly picks up the custom names, but the default value expression still uses the _-prefixed form.

### Environment Information

Gradle plugin 5.0.0

### Example Application

_No response_

### Version

5.0.0

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the provided repro.openapi.yaml and the generated MaxHours.java and MyModel.java output, using the Gradle OpenAPI configuration to reproduce the issue. Compare the enum constants with the generated default value expression, including the x-enum-varnames case. Done means generated code compiles and the default references the existing enum constant.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.