Skip to content

requestBody with oneOf does not generate marshal/unmarshal union data #1620

Closed

Description

@l0n3star

Minimal yaml:

openapi: 3.0.2
info:
  title: test API
servers:
  - url: 'https://api.server.com'
paths:
  '/command':
    get:
      summary: command summary
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/CommandRequest1'
                - $ref: '#/components/schemas/CommandRequest2'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CommandResponse'

components:
  schemas:
    CommandRequest1:
      type: object
      required:
        - id
      properties:
        id:
          type: string
    CommandRequest2:
      type: object
      required:
        - groupID
      properties:
        groupID:
          type: string
    CommandResponse:
      type: object
      required:
        - command
      properties:
        command:
          type: string

This is the generated go code:

package oapi

import (
	"encoding/json"
)

// CommandRequest1 defines model for CommandRequest1.
type CommandRequest1 struct {
	Id string `json:"id"`
}

// CommandRequest2 defines model for CommandRequest2.
type CommandRequest2 struct {
	GroupID string `json:"groupID"`
}

// CommandResponse defines model for CommandResponse.
type CommandResponse struct {
	Command string `json:"command"`
}

// GetCommandJSONBody defines parameters for GetCommand.
type GetCommandJSONBody struct {
	union json.RawMessage
}

// GetCommandJSONRequestBody defines body for GetCommand for application/json ContentType.
type GetCommandJSONRequestBody GetCommandJSONBody

There is GetCommandJSONBody struct with union data but no functions to use it.

Activity

  1. stefanmcshane commented on Nov 13, 2024

    @stefanmcshane

    Also seeing this, which is a hard blocker. Using strict server also

  2. freshteapot commented on Apr 6, 2026

    @freshteapot

    For what its worth,

    I had a similiar problem, but not on all endpoints.
    If you create a higher level object, with a discriminator, it will happily make the union.

    But that would require a discriminator the above example, doesnt seem to have that.

  3. mromaszewicz commented on May 1, 2026

    @mromaszewicz
    Member

    PR #2348 ('Overhaul anonymous schema hoisting', merged 2026-04-29) fixed this by including operation-derived type definitions in the boilerplate passes that emit union accessors. A repro using the exact spec from the issue now generates AsCommandRequest1, FromCommandRequest1, MergeCommandRequest1 (and *2 variants) plus MarshalJSON/UnmarshalJSON on GetCommandJSONBody. The fix is tested by TestBodyRootOneOf_RoundTripCat in internal/test/anonymous_inner_hoisting/client_test.go.

    Closing as fixed. If you can still reproduce on a recent release, please reopen with the exact spec and version used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions