OpenAPITools / OpenAPITools/openapi-generator
[BUG][Go] UnmarshalJson has unused variable err if spec has oneOf without 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?
- 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
In the openAPI spec, if a schema has oneOf but does not have a discriminator and the config useOneOfDiscriminatorLookup is set to True, the unmarshalJson function is incorrectly generated. Such a scenario can arise when the schema looks as follows:
Pet:
description: a pet
oneOf:
- type: 'null'
- $ref: "#/components/schemas/dog"
- $ref: "#/components/schemas/cat"
Considering the fact that there are other schemas in this spec that may have oneOf without type: 'null', the configuration useOneOfDiscriminatorLookup: True is required. As a result, the unmarshal function for the above object is as follows:
func (dst *Pet) UnmarshalJSON(data []byte) error {
var err error
// this object is nullable so check if the payload is null or empty string
if string(data) == "" || string(data) == "{}" {
return nil
}
return nil
}
Building this generates an error because the variable err is declared but not used.
openapi-generator version
Master build with last commit id 023e6dd
Suggest a fix
Allow unmarshalling of data even if nodiscriminator is mentioned whenuseOneOfDiscriminatorLookup: True
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
Use the supplied Pet schema with oneOf, a null branch, no discriminator, and useOneOfDiscriminatorLookup enabled to reproduce the generated Go UnmarshalJSON method. Inspect the generator path responsible for that method and verify the generated code builds without an unused err variable while preserving unmarshalling behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, java, openapi
- Domain
- backend-api-design, devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100