OpenAPITools / OpenAPITools/openapi-generator
[BUG] [GO] Model generates incorrectly for objects with properties and OneOf types at the same level
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 (example)?
- 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
We have a spec that contains properties at the same level as a OneOf object. The generated model does not match the spec.
openapi-generator version
openapitools/openapi-generator-cli:5.1.1
OpenAPI declaration file content or url
The part of spec in question is below. Full spec is also attached in the gist below
CustomerName:
type: object
required:
- display_name
properties:
display_name:
description: Customer's common or display name
$ref: '#/components/schemas/DisplayNameType'
oneOf:
- type: object
properties:
individual_name:
$ref: '#/components/schemas/IndividualName'
- type: object
properties:
business_name:
$ref: '#/components/schemas/BusinessName'
https://gist.github.com/saikris12/883abef8ad8b7e0544b1d60f0bb3d92f
Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate
-i /local/swagger/customer.yaml
-g go
-o /local/partners/clients/customer
--additional-properties=packageName=customer,isGoSubmodule=true,enumClassPrefix=true,generateInterfaces=true
Steps to reproduce
Generate the go client using the spec above. It should generate the model_create_customer_request.go file with the below model. The display_name which is under the CustomerName object never gets generated since its on the same level as the OneOf.
type CustomerName struct {
CustomerNameOneOf *CustomerNameOneOf
CustomerNameOneOf1 *CustomerNameOneOf1
}
Related issues/PRs
No
Suggest a fix
I tried overriding the mustache template model_oneof.mustache, but I am having issues with the Marshal function. I am not sure how I can marshal the display name along with the OneOf field. I am expecting the name field to be as below :
"name": {
"display_name": "string",
"individual_name": {
"first_name": "string",
"middle_name": "string",
"last_name": "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
Run the provided Docker generation command with the linked OpenAPI spec, then inspect model_oneof.mustache and the generated model_create_customer_request.go file. Confirm how the shared display_name property is omitted and how the generated Marshal behavior handles the OneOf fields. Done means the model includes display_name and produces the expected JSON alongside the selected OneOf value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100