Skip to content

[BUG][GO] JSON marshalling incorrect when setting anyOf properties as required #19963

Open
@perhallgren

Description

@perhallgren

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
    {"obj":"A"} 
    
    But it produces
    {"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
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions