micronaut-projects / micronaut-projects/micronaut-openapi
OAS files with polymorphic ("one of") fields do not properly serialize / deserialize
- Dominant language
- Java
- Stars
- 114
- Forks
- 121
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 25
Description
### Expected Behavior
I'm able to use an OAS file that produces more than one type for a given variable. (such as this variable called "value":
```yaml
value:
oneOf:
- type: integer
format: int64
- type: number
format: double
- type: string
- type: boolean
- type: array
items:
type: string
```
### Actual Behaviour
if a property has more than one type, the generator's solution does not serialize when tests run, resulting in an error that states that there are missing introspection annotations.
### Steps To Reproduce
use an OAS file which has a polymorphic field, like the example below: (also available at [this example git repo](https://github.com/Jonathan-Zollinger/example-polymorphic-micronaut-openapigenerator-deserialization-problem))
```yaml
openapi: 3.0.3
info:
title: OneOf Example
version: 1.0.0
components:
schemas:
CustomField:
type: object
required:
- id
- value
properties:
id:
type: integer
format: int64
value:
oneOf:
- type: integer
format: int64
- type: number
format: double
- type: string
- type: boolean
- type: array
items:
type: string
```
attempt to run this test:
```groovy
import com.example.model.CustomField
import io.micronaut.serde.ObjectMapper
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification
import spock.lang.Unroll
@MicronautTest
class DemoSpec extends Specification {
@Inject
ObjectMapper objectMapper
@Unroll
def "should deserialize polymorphic custom field type: #expectedClass.simpleName"() {
given: "A raw JSON snippet representing a customized field payload"
def json = """{"id": 42, "value": ${jsonValue}}"""
when:
CustomField result = objectMapper.readValue(json, CustomField)
then:
verifyAll {
result.id == 42
expectedClass.isInstance(result.value)
result.getValue() == expectedValue
}
where:
jsonValue | expectedClass | expectedValue
'"flibberty giblets"' | String | "flibberty giblets"
'true' | Boolean | true
'10023' | Long | 10023L
'45.29' | Float | 45.29f
'["alpha","beta"]' | List | ["alpha", "beta"]
}
}
```
### Environment Information
- OS: windows 11
- Java: 21
- Micronaut: 4.8.3
### Example Application
https://github.com/Jonathan-Zollinger/example-polymorphic-micronaut-openapigenerator-deserialization-problem
### Version
4.8.3
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the OpenAPI schema and generated CustomField model from the linked example application, then run the DemoSpec deserialization test with the listed scalar and array values. Trace how the generator represents the oneOf property and how Micronaut serialization handles it. Done means all shown values deserialize and serialize without missing introspection annotation errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100