Skip to content

Commit f4ac5df

Browse files
Support nullable slice elements and map values (#2185)
1 parent 59fb0e8 commit f4ac5df

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

pkg/codegen/codegen_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ type GetTestByNameResponse struct {
7777
assert.Contains(t, code, "Top *int `form:\"$top,omitempty\" json:\"$top,omitempty\"`")
7878
assert.Contains(t, code, "func (c *Client) GetTestByName(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*http.Response, error) {")
7979
assert.Contains(t, code, "func (c *ClientWithResponses) GetTestByNameWithResponse(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*GetTestByNameResponse, error) {")
80-
assert.Contains(t, code, "DeadSince *time.Time `json:\"dead_since,omitempty\" tag1:\"value1\" tag2:\"value2\"`")
80+
assert.Contains(t, code, "FavouriteBirds *[]*string `json:\"favourite_birds,omitempty\"`")
81+
assert.Contains(t, code, "DetestedBirds *[]string `json:\"detested_birds,omitempty\"`")
82+
assert.Contains(t, code, "SlicedBirds []string `json:\"sliced_birds\"`")
83+
assert.Contains(t, code, "ForgettableBirds *map[string]*string `json:\"forgettable_birds,omitempty\"`")
84+
assert.Contains(t, code, "MemorableBirds *map[string]string `json:\"memorable_birds,omitempty\"`")
85+
assert.Contains(t, code, "VeryMemorableBirds map[string]string `json:\"very_memorable_birds\"`")
86+
assert.Contains(t, code, "DeadSince *time.Time `json:\"dead_since,omitempty\" tag1:\"value1\" tag2:\"value2\"`")
87+
assert.Contains(t, code, "VeryDeadSince time.Time `json:\"very_dead_since\"`")
8188
assert.Contains(t, code, "type EnumTestNumerics int")
8289
assert.Contains(t, code, "N2 EnumTestNumerics = 2")
8390
assert.Contains(t, code, "type EnumTestEnumNames int")

pkg/codegen/schema.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
602602
if err != nil {
603603
return fmt.Errorf("error generating type for array: %w", err)
604604
}
605+
606+
var itemPrefix string
607+
605608
if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" {
606609
// If we have items which have additional properties or union values,
607610
// but are not a pre-defined type, we need to define a type
@@ -618,8 +621,13 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
618621

619622
arrayType.RefType = typeName
620623
}
624+
625+
if arrayType.OAPISchema != nil && arrayType.OAPISchema.Nullable {
626+
itemPrefix = "*"
627+
}
628+
621629
outSchema.ArrayType = &arrayType
622-
outSchema.GoType = "[]" + arrayType.TypeDecl()
630+
outSchema.GoType = "[]" + itemPrefix + arrayType.TypeDecl()
623631
outSchema.AdditionalTypes = arrayType.AdditionalTypes
624632
outSchema.Properties = arrayType.Properties
625633
outSchema.DefineViaAlias = true

pkg/codegen/test_spec.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,45 @@ components:
164164
format: date-time
165165

166166
CatDead:
167+
required:
168+
- sliced_birds
169+
- very_dead_since
170+
- very_memorable_birds
167171
properties:
168172
name:
169173
type: string
174+
favourite_birds:
175+
type: array
176+
items:
177+
type: string
178+
nullable: true
179+
detested_birds:
180+
type: array
181+
items:
182+
type: string
183+
sliced_birds:
184+
type: array
185+
items:
186+
type: string
187+
forgettable_birds:
188+
additionalProperties:
189+
type: string
190+
nullable: true
191+
memorable_birds:
192+
additionalProperties:
193+
type: string
194+
very_memorable_birds:
195+
additionalProperties:
196+
type: string
170197
dead_since:
171198
type: string
172199
format: date-time
173200
x-oapi-codegen-extra-tags:
174201
tag1: value1
175202
tag2: value2
203+
very_dead_since:
204+
type: string
205+
format: date-time
176206
cause:
177207
type: string
178208
enum: [ car, dog, oldage ]

0 commit comments

Comments
 (0)