OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Invalid code generated for java/apache-httpcomponent when using polymorphism
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?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
I'm trying to generate apache-httpclient client from a spec where Store object has a reference to either Pet or Cat which inherit from Pet
The generated code for StorePet.java contains following code in the public String toUrlQueryString(String prefix) method:
// add `huntingSkill` to the URL query string
if (getHuntingSkill() != null) {
try {
joiner.add(String.format("%shuntingSkill%s=%s", prefix, suffix, URLEncoder.encode(String.valueOf(getHuntingSkill()), "UTF-8").replaceAll("\\+", "%20")));
} catch (UnsupportedEncodingException e) {
// Should never happen, UTF-8 is always supported
throw new RuntimeException(e);
}
}
However the method getHuntingSkill() (or the property huntingSkill) is not present in the class.
openapi-generator version
7.15.0. Not a regression, observed in 6.x versions as well.
OpenAPI declaration file content or url
components:
schemas:
Store:
type: object
properties:
pet:
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Dog"
discriminator:
propertyName: petType
mapping:
Cat: "#/components/schemas/Cat"
Dog: "#/components/schemas/Dog"
Pet:
discriminator:
propertyName: petType
required:
- name
- petType # required for inheritance to work
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Pet' # Cat has all properties of a Pet
- properties: # extra properties only for cats
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
Dog:
allOf:
- $ref: '#/components/schemas/Pet' # Dog has all properties of a Pet
- properties: # extra properties only for dogs
packSize:
description: The size of the pack the dog is from
type: integer
openapi: 3.0.3
security: []
servers: []
paths: {}
info:
version: 1.0.0
title: Swagger Petstore
Generation Details
language: java
library: apache-httpclient
Steps to reproduce
openapi-generator generate -i src/main/resources/test.store.openapi.yaml -g java -o tmp/
Related issues/PRs
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 running the listed openapi-generator command with src/main/resources/test.store.openapi.yaml and inspect the generated StorePet.java. Trace how the Store, Pet, Cat, and Dog polymorphism is represented in the generated model and its toUrlQueryString method. Done means the apache-httpclient Java output does not reference properties absent from the generated class.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100