Skip to content

strict fiber server returns nothing when using schema with oneOf #1872

Closed

Description

@holyfuchs

oapi-codegen --version: v2.4.1
go 1.23.4

Generating code with the following config:

package: main
generate:
  fiber-server: true
  strict-server: true
  models: true

and a openapi.yaml with a oneof:

openapi: 3.0.0
paths:
  /test:
    get:
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Test'
components:
  schemas:
    Test:
      oneOf:
        - $ref: '#/components/schemas/Test1'
        - $ref: '#/components/schemas/Test2'
    Test1:
      type: object
      properties:
        test1:
          type: string
      required:
        - test1
    Test2:
      type: object
      properties:
        test2:
          type: string
      required:
        - test2

generates code:

type Test struct {
	union json.RawMessage
}
type GetTest200JSONResponse Test
func (response GetTest200JSONResponse) VisitGetTestResponse(ctx *fiber.Ctx) error {
	ctx.Response().Header.Set("Content-Type", "application/json")
	ctx.Status(200)

	return ctx.JSON(&response)
}

since union starts with a lower case letter the endpoint won't return anything.
I would expect it to return my Test struct.

changing the generated code to return ctx.JSON(&response.union) fixed the issue

Activity

  1. holyfuchs commented on Jan 1, 2025

    @holyfuchs
    Author

    it seems a workaround is changing the openapi.yaml to the following:

            '200':
              description: Ok
              content:
                application/json:
                  schema:
                    oneOf:
                      - $ref: '#/components/schemas/Test1'
                      - $ref: '#/components/schemas/Test2'
    

    and to make sure the now unused Test is still generated adding the following to the output options

    output-options:
      skip-prune: true
    
  2. mromaszewicz commented on May 1, 2026

    @mromaszewicz
    Member

    The bug was that type GetTest200JSONResponse Test (a defined type, not an alias) did not inherit Test.MarshalJSON(), so ctx.JSON(&response) serialized the struct without exported fields and emitted {}. Commit 5260c72 (2026-04-21) fixed this by adding HasCustomMarshalJSON to schema.go and generating MarshalJSON/UnmarshalJSON delegators in all strict server templates when the response schema is a $ref to a oneOf/anyOf union. The repro spec now generates correct delegators in the current codebase. The fix was landed as 'Fixes #970' but resolves the identical underlying problem reported in #1872.

    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