OpenAPITools / OpenAPITools/openapi-generator
[BUG][GO] JSON marshalling incorrect when setting anyOf properties as required
Open
Nobody has claimed this yet.
Issue: Bug
- 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?
Expecting marshalling to produce
But it produces{"obj":"A"}{"obj":{"Int32":null,"String":"A"}} - [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When an anyOf model is a required property, JSON marshalling is incorrect.
openapi-generator version
docker run --rm openapitools/openapi-generator-cli:latest version
7.10.0-SNAPSHOT
OpenAPI declaration file content or url
openapi: 3.0.0
info:
version: 1.0.0
title: FooBar Service
paths: {}
components:
schemas:
Bad:
type: object
description: bad
required:
- obj
properties:
obj:
$ref: '#/components/schemas/Obj'
Obj:
anyOf:
- type: string
- type: integer
example: a
description: obj
Generation Details
I'm running
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate \
--input-spec /local/go-json-marshal.yaml \
--generator-name go \
--additional-properties "withGoMod=false" \
--package-name gen \
--output /local/gen
Steps to reproduce
Run this gist
Related issues/PRs
- https://github.com/OpenAPITools/openapi-generator/pull/19962
- https://github.com/OpenAPITools/openapi-generator/issues/14028
Suggest a fix
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
Generate the Go client from the YAML declaration using the Docker command in the report, then run the linked gist to reproduce the marshalling output. Compare the required anyOf property with the expected {"obj":"A"} result and the nested output shown. Done means the generated Go JSON marshalling matches the expected representation for this specification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, go, openapi
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100