Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ch4mpy/springdoc-openapi-2494
Reproduce wrong OpenAPI spec for enums with toString
https://github.com/ch4mpy/springdoc-openapi-2494
Last synced: 10 days ago
JSON representation
Reproduce wrong OpenAPI spec for enums with toString
- Host: GitHub
- URL: https://github.com/ch4mpy/springdoc-openapi-2494
- Owner: ch4mpy
- Created: 2024-01-26T00:22:09.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-03-09T00:59:47.000Z (9 months ago)
- Last Synced: 2024-11-02T20:30:23.131Z (18 days ago)
- Language: Java
- Size: 77.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Reproducer for [https://github.com/springdoc/springdoc-openapi/issues/2494](https://github.com/springdoc/springdoc-openapi/issues/2494)
considering the following enums:
```java
public static enum EnumSerializedByName {
A("name a"),
B("name b");String label;
EnumSerializedByName(String label) {
this.label = label;
}@Override
public String toString() {
return label;
}
}
```
```java
public static enum EnumSerializedByToString {
A("str a"),
B("str b");String label;
EnumSerializedByToString(String label) {
this.label = label;
}@Override
@JsonValue // Forces serialization using toString()
public String toString() {
return label;
}
}
```
```java
public static enum BijectiveEnumSerializedByToString {
A("bij a"),
B("bij b");String label;
BijectiveEnumSerializedByToString(String label) {
this.label = label;
}@Override
@JsonValue // Forces serialization using toString()
public String toString() {
return label;
}public static BijectiveEnumSerializedByToString fromString(String str) {
for (final var e : BijectiveEnumSerializedByToString.values()) {
if (Objects.equals(e.toString(), str)) {
return e;
}
}
return null;
}@Component
static class StringEnumSerializedByToStringConverter implements Converter {
@Override
public BijectiveEnumSerializedByToString convert(String source) {
return BijectiveEnumSerializedByToString.fromString(source);
}
}
}
```with Spring default:
- `HttpMessageConverter` beans
- `Converter`
- `Converter`the generated spec is:
```json
"components": {
"schemas": {
"Dto": {
"required": ["bij", "name", "str"],
"type": "object",
"properties": {
"name": { "type": "string", "enum": ["name a", "name b"] },
"str": { "type": "string", "enum": ["str a", "str b"] },
"bij": { "type": "string", "enum": ["bij a", "bij b"] }
}
}
}
}
```This is:
- wrong for `EnumSerializedByName` wich is always serialized using `name()` and deserialised using `valueOf()`
- right for `BijectiveEnumSerializedByToString`
- right for `EnumSerializedByToString` when `HttpMessageConverter` beans are used (`@RequestBody` and `@ResponseBody`), but wrong when the default `Converter` is used (`@RequestParam`)