fix(codegen): generate pointer types for nullable array items
This PR adds capability to generate pointer types for nullable array items.
When an array's items have nullable: true, the generated Go type now correctly uses pointer types (e.g., []*float32 instead of []float32) to properly represent nullable values.
Ref Issue: #2016
Changes
- Modified
oapiSchemaToGoType()inpkg/codegen/schema.goto check thenullableproperty of array items - Added comprehensive test cases in
internal/test/issues/issue-2016/covering:- Primitive types (number, string, boolean, integer) with nullable items
- Object types with nullable items
- Nested arrays with nullable items
- Nullable arrays with nullable items
Example
Input schema: Sample
type: object
required: [metrics]
properties:
metrics:
type: array
items:
nullable: true
type: number
Before (incorrect)
type Sample struct {
Metrics []float32 `json:"metrics"`
}
After (correct)
type Sample struct {
Metrics []*float32 `json:"metrics"`
}
Would using the Output Option prefer-skip-optional-pointer-on-container-types solve this?
@jamietanna
I don't think prefer-skip-optional-pointer-on-container-types would solve this issue, as they address different aspects
prefer-skip-optional-pointer-on-container-types: Controls pointer to the array itself (*[]stringvs[]string)- This fix: Controls pointer in array items when nullable (
[]*stringvs[]string)
Gotcha, thanks - should we also be correctly using ,omitempty for the JSON tags in this case, too?
@jamietanna I think the omitempty handling is already correct with this fix — it's determined by whether the property is required, which is independent of whether the array items are nullable.
That said, I want to make sure I understand your concern correctly. Were you thinking of a particular case where omitempty should behave differently when the array items are nullable?
Hi @jamietanna , just checking if you had a chance to look at my question above. Let me know what you think!