Handle pure nullable types#2437
Conversation
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>
Greptile SummaryFixes a codegen crash introduced by OAI 3.1 schemas whose only type is
Confidence Score: 4/5The 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.
|
| 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
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 ofany. Includes a regression test under internal/test/issues/issue-2430.