Skip to content

OpenAPI 3.1.0 Support null type#2385

Draft
atye wants to merge 1 commit into
oapi-codegen:feat/kin-openapi-3.1from
atye:feat/kin-openapi-3.1-nulltype
Draft

OpenAPI 3.1.0 Support null type#2385
atye wants to merge 1 commit into
oapi-codegen:feat/kin-openapi-3.1from
atye:feat/kin-openapi-3.1-nulltype

Conversation

@atye
Copy link
Copy Markdown

@atye atye commented May 22, 2026

Per the OpenAPI 3.1.0 Spec:

Data types in the OAS are based on the types supported by the JSON Schema Specification Draft 2020-12. Note that integer as a type is also supported and is defined as a JSON number without a fraction or exponent part. Models are defined using the Schema Object, which is a superset of JSON Schema Specification Draft 2020-12.

The Schema Object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. This object is a superset of the JSON Schema Specification Draft 2020-12.

On the feat/kin-openapi-3.1 branch, parsing the below spec fails.

oneOf:
  - type: object
    title: Player
    properties:
      id:
        type: string
      username:
        type: string
      rating:
        type: integer
      ratingDiff:
        type: integer
    required:
      - id
      - username
      - rating
  - type: object
    title: AI Opponent
    properties:
      id:
        type: "null"
      username:
        type: string
      ai:
        type: integer
        description: AI level, from 1 to 8, where 1 is the weakest and 8 is the strongest.
    required:
      - id
      - username
      - ai
error generating code: error creating operation definitions: error generating response definitions: error generating request body definition: error generating type for additional properties: error generating type for oneOf: error resolving primitive type: unhandled Schema type: &[null]

This seems to be the same behavior described in #2336 (comment).

This PR aims to handle the null type with interface{}. I am not as familiar with the repo so perhaps implementation should be done in #2336.

@atye atye requested a review from a team as a code owner May 22, 2026 19:41
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 22, 2026

Greptile Summary

This PR adds initial OpenAPI 3.1 null type support in code generation. The main changes are:

  • Maps type: "null" to interface{} by default.
  • Adds null to the configurable type-mapping table.
  • Treats null properties and parameters as nil-valued for template checks.
  • Adds unit coverage for null schema conversion and nil-zero helpers.

Confidence Score: 3/5

These issues should be fixed before merging.

  • Valid null enum or const schemas can generate invalid Go constants.
  • Custom null type mappings can make generated model code compare non-nilable fields to nil.
  • The same custom mapping issue can affect generated parameter handling code.

pkg/codegen/schema.go and pkg/codegen/operations.go need follow-up around null nilability and enum handling.

Important Files Changed

Filename Overview
pkg/codegen/schema.go Adds null primitive conversion and model nil-check behavior; null enum and custom-mapping cases need fixes.
pkg/codegen/operations.go Adds null parameter nil-check behavior; custom null mappings can make generated parameter code fail to compile.
pkg/codegen/typemapping.go Adds a default and user-overridable mapping entry for OpenAPI null.

Reviews (1): Last reviewed commit: "handle null type" | Re-trigger Greptile

Comment thread pkg/codegen/schema.go
Comment on lines +1015 to +1018
} else if t.Is("null") {
spec := globalState.typeMapping.Null.Resolve(f)
outSchema.GoType = spec.Type
outSchema.DefineViaAlias = true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Null enums break generation

This branch lets type: "null" schemas reach the enum/const generation path, but null enum values are later stringified as <nil> and emitted as unquoted Go constants. For an OpenAPI 3.1 schema with type: "null" and enum: [null] or const: null, the generated code contains an invalid assignment like a null enum constant set to <nil>, so the output does not compile. This path should either render a valid nil expression for the chosen representation or reject null enums before constants are emitted.

Comment thread pkg/codegen/schema.go
Comment on lines +198 to 200
if t.Is("null") {
return true
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Mapped null may not be nilable

type-mapping.null is configurable, but this helper now treats every null schema as having a nil zero value. If a user maps null to a non-nilable Go type such as string, union and additional-properties marshal templates can emit checks like if t.Field != nil against that non-nilable field. The generated model code then fails to compile. This check needs to be based on the resolved Go type, or null mappings need to be constrained to nilable types.

Comment thread pkg/codegen/operations.go
Comment on lines +70 to 72
if t.Is("null") {
return true
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Mapped null parameters break

The new null branch makes .RequiresNilCheck true for every null parameter, even though type-mapping.null can map null to a non-nilable Go type. With a mapping like null to string, client, webhook, and callback templates can generate if params.X != nil for a string parameter field. That generated code does not compile. Parameter nil checks should follow the resolved Go type instead of the OpenAPI type alone.

@atye atye force-pushed the feat/kin-openapi-3.1-nulltype branch from acd9443 to f8a51bc Compare May 22, 2026 19:45
@atye atye marked this pull request as draft May 22, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant