Skip to content

Handle pure nullable types#2437

Merged
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2430
Jul 6, 2026
Merged

Handle pure nullable types#2437
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2430

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #2430

OpenAPI 3.1 allows a schema whose only type is "null", which validates exactly the JSON value null. The type dispatch in oapiSchemaToGoType had no branch for it, so codegen failed with "unhandled Schema type: &[null]". Map such schemas to any, mirroring the permissive mapping used for typeless schemas, and skip the optional pointer since nil is already the zero value of any. Includes a regression test under internal/test/issues/issue-2430.

Closes: #2430

OpenAPI 3.1 allows a schema whose only type is "null", which validates
exactly the JSON value null. The type dispatch in oapiSchemaToGoType
had no branch for it, so codegen failed with "unhandled Schema type:
&[null]". Map such schemas to `any`, mirroring the permissive mapping
used for typeless schemas, and skip the optional pointer since nil is
already the zero value of `any`. Includes a regression test under
internal/test/issues/issue-2430.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner July 6, 2026 18:31
@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a codegen crash introduced by OAI 3.1 schemas whose only type is "null" by adding a t.Is("null") branch in oapiSchemaToGoType that maps such schemas to any with the optional pointer suppressed, and adds a full regression test package under internal/test/issues/issue-2430.

  • pkg/codegen/schema.go: new else if t.Is("null") branch sets GoType = "any", SkipOptionalPointer = true, DefineViaAlias = true, resolving the previous "unhandled Schema type: &[null]" error.
  • internal/test/issues/issue-2430/: three-case OAI 3.1 spec (optional property, required property, named top-level component), generated file, and round-trip tests covering unmarshal, marshal-to-null, and alias assignment.

Confidence Score: 4/5

The change is small, self-contained, and only fires for the previously-unhandled pure-null type; existing generated output for all other types is unaffected.

The fix is correct and well-tested. The only observation is that the new branch emits "any" as the Go type string while the equivalent typeless-schema path in GenerateGoSchema emits "interface{}", producing a minor inconsistency in generated output across the two code paths.

pkg/codegen/schema.go — the any vs interface{} string choice relative to the typeless-schema branch is the only thing worth a second look.

Important Files Changed

Filename Overview
pkg/codegen/schema.go Adds a t.Is("null") branch in oapiSchemaToGoType that maps OAI 3.1 pure-null schemas to any with SkipOptionalPointer and DefineViaAlias set; correct fix but uses "any" string while the typeless-schema path uses "interface{}", a minor inconsistency in generated output.
pkg/codegen/schema_test.go Adds TestOapiSchemaToGoType_NullType which correctly validates GoType, SkipOptionalPointer, and DefineViaAlias for the new branch.
internal/test/issues/issue-2430/issue2430.gen.go Generated output matches what the new null-type branch should produce: NullOnly = any alias, RequiredNull.Value any without omitempty, ChallengeOpenJson.Challenger any with omitempty for optional field.
internal/test/issues/issue-2430/issue2430_test.go Regression tests cover unmarshal of a null-typed property, marshal of a required null-typed field to explicit JSON null, and type alias assignment; all assertions are correct.
internal/test/issues/issue-2430/spec.yaml OAI 3.1 spec exercising three null-type cases: optional property, required property, and top-level named component; adequate coverage for the fix.
internal/test/issues/issue-2430/config.yaml Standard codegen config for the issue regression test; no issues.
internal/test/issues/issue-2430/generate.go Standard go:generate directive for the issue test package; no issues.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
pkg/codegen/schema.go:1198-1200
The PR description says this mirrors "the permissive mapping used for typeless schemas," but the typeless-schema branch in `GenerateGoSchema` (line 782) emits `"interface{}"` as the Go type string, while this new branch emits `"any"`. Both are equivalent in Go 1.18+, but they produce different text in generated output and create an inconsistency within the same codebase. Aligning on one spelling avoids surprising downstream teams with mixed generated output.

```suggestion
		outSchema.GoType = "interface{}"
		outSchema.SkipOptionalPointer = true
		outSchema.DefineViaAlias = true
```

Reviews (1): Last reviewed commit: "Handle pure nullable types" | Re-trigger Greptile

@mromaszewicz mromaszewicz added the bug Something isn't working label Jul 6, 2026
@mromaszewicz
mromaszewicz merged commit 53daae1 into main Jul 6, 2026
29 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2430 branch July 6, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"null" type isn't handled for OpenAPI 3.1

1 participant