OpenAPITools / OpenAPITools/openapi-generator
[BUG] Discriminator mapping not correct when using oneOf with discriminator
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
yes - Have you validated the input using an OpenAPI validator (example)?
yes - What's the version of OpenAPI Generator used?
4.3.1 - Have you search for related issues/PRs?
yes
Description
The generated code leads to the following result:
@JsonInclude(JsonInclude.Include.NON_NULL)
@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-06-25T13:58:21.826225400+02:00[Europe/Berlin]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "parentId", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = TestHolder.class, name = "whatsup"),
@JsonSubTypes.Type(value = WhatHolder.class, name = "tst"),
})
public interface ArrayItem {
public String getParentId();
}
I would expect the following result
@JsonInclude(JsonInclude.Include.NON_NULL)
@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-06-25T13:58:21.826225400+02:00[Europe/Berlin]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "parentId", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = TestHolder.class, name = "whatsup"),
@JsonSubTypes.Type(value = WhatHolder.class, name = "tst"),
})
public interface ArrayItem {
public ParentIdEnum getParentId();
}
It should use the correct type from the child!.
See content of YAML
OpenAPI declaration file content or url
openapi: 3.0.0
info:
version: "0.0.0.1"
title: ""
x-apiType: ""
description: ""
paths:
/test:
post:
tags:
- "test"
description: "tes"
operationId: "test"
requestBody:
description: "Payload for premium calculation."
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/NewRequest"
responses:
"200":
description: "Successful operation"
components:
schemas:
Parent:
required:
- parentId
type: object
properties:
parentId:
type: string
enum:
- whatsup
- tst
discriminator:
propertyName: parentId
mapping:
whatsup: "#/components/schemas/What"
tst: "#/components/schemas/Test"
What:
type: object
allOf:
- $ref: "#/components/schemas/Parent"
properties:
whatever:
type: string
Test:
type: object
allOf:
- $ref: "#/components/schemas/Parent"
properties:
test:
type: string
NewRequest:
type: object
properties:
parents:
type: array
minItems: 1
maxItems: 100
items:
$ref: "#/components/schemas/arrayItem"
arrayItem:
type: object
oneOf:
- $ref: "#/components/schemas/TestHolder"
- $ref: "#/components/schemas/WhatHolder"
discriminator:
propertyName: parentId
mapping:
whatsup: "#/components/schemas/TestHolder"
tst: "#/components/schemas/WhatHolder"
TestHolder:
type: object
allOf:
- $ref: "#/components/schemas/What"
properties:
testHolder:
type: string
WhatHolder:
type: object
allOf:
- $ref: "#/components/schemas/Test"
properties:
whatHolder:
type: string
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the supplied OpenAPI YAML and the Java client generator, then inspect the generated ArrayItem interface and its discriminator handling. Compare the generated parentId type with the expected ParentIdEnum type; done means the discriminator-based oneOf models generate the correct child-derived type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100