Skip to content

error merging schemas for AllOf: merging two schemas with different Nullable #1898

Closed

Description

@boogie-byte

oapi-codegen fails to properly merge allOf elements when one of them has the nullable property unset, and the other one has it set to true.

oapi-codegen --version output:

github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
v2.4.1

OpenAPI spec I'm using:

openapi: "3.0.0"
paths:
  /user:
    get:
      responses:
        200:
          content:
            application/json:
              schema:
                properties:
                  user:
                    allOf:
                      - $ref: "#/components/schemas/user"
                      - nullable: true
components:
  schemas:
    user:
      required:
        - name
        - surname
      properties:
        name:
          type: string
        surname:
          type: string

oapi-codegen config:

---
package: myapi
output: myapi.go
generate:
  models: true
  client: true
output-options:
  nullable-type: true

oapi-codegen output I get:

error generating code: error creating operation definitions: error generating response definitions: error generating request body definition: error generating Go schema for property 'user': error merging schemas: error merging schemas for AllOf: merging two schemas with different Nullable

I've scrolled through the code and it seems that the reason lies within the github.com/getkin/kin-openapi/openapi3 package. The Schema type it provides has the Nullable field as a bool, which makes it impossible to differentiate between the default and unset values, which makes the schema merge code to misbehave.

I'm not really sure what's the best way to address this issue. AFAIK, this OpenAPI idiom is quite popular to mix in some additional properties to shared schemas, and it doesn't really voilate the OpenAPI spec, so theoretically oapi-codegen should be able to deal with it.

Activity

  1. boogie-byte commented on Feb 10, 2025

    @boogie-byte
    Author

    Actually, this seems to be the same issue as #1883.

  2. lukacat10 commented on Feb 15, 2025

    @lukacat10

    @boogie-byte I have commented my solution in #1883, you may find it helpful.

  3. added a commit that references this issue on Jul 16, 2026
    6d3c864
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