diff --git a/pkg/codegen/codegen_test.go b/pkg/codegen/codegen_test.go index ff3c86d9ba..4920c72e49 100644 --- a/pkg/codegen/codegen_test.go +++ b/pkg/codegen/codegen_test.go @@ -77,7 +77,14 @@ type GetTestByNameResponse struct { assert.Contains(t, code, "Top *int `form:\"$top,omitempty\" json:\"$top,omitempty\"`") assert.Contains(t, code, "func (c *Client) GetTestByName(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*http.Response, error) {") assert.Contains(t, code, "func (c *ClientWithResponses) GetTestByNameWithResponse(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*GetTestByNameResponse, error) {") - assert.Contains(t, code, "DeadSince *time.Time `json:\"dead_since,omitempty\" tag1:\"value1\" tag2:\"value2\"`") + assert.Contains(t, code, "FavouriteBirds *[]*string `json:\"favourite_birds,omitempty\"`") + assert.Contains(t, code, "DetestedBirds *[]string `json:\"detested_birds,omitempty\"`") + assert.Contains(t, code, "SlicedBirds []string `json:\"sliced_birds\"`") + assert.Contains(t, code, "ForgettableBirds *map[string]*string `json:\"forgettable_birds,omitempty\"`") + assert.Contains(t, code, "MemorableBirds *map[string]string `json:\"memorable_birds,omitempty\"`") + assert.Contains(t, code, "VeryMemorableBirds map[string]string `json:\"very_memorable_birds\"`") + assert.Contains(t, code, "DeadSince *time.Time `json:\"dead_since,omitempty\" tag1:\"value1\" tag2:\"value2\"`") + assert.Contains(t, code, "VeryDeadSince time.Time `json:\"very_dead_since\"`") assert.Contains(t, code, "type EnumTestNumerics int") assert.Contains(t, code, "N2 EnumTestNumerics = 2") assert.Contains(t, code, "type EnumTestEnumNames int") diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index f7b6fd6ac1..25f34de38a 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -602,6 +602,9 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem if err != nil { return fmt.Errorf("error generating type for array: %w", err) } + + var itemPrefix string + if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" { // If we have items which have additional properties or union values, // 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 arrayType.RefType = typeName } + + if arrayType.OAPISchema != nil && arrayType.OAPISchema.Nullable { + itemPrefix = "*" + } + outSchema.ArrayType = &arrayType - outSchema.GoType = "[]" + arrayType.TypeDecl() + outSchema.GoType = "[]" + itemPrefix + arrayType.TypeDecl() outSchema.AdditionalTypes = arrayType.AdditionalTypes outSchema.Properties = arrayType.Properties outSchema.DefineViaAlias = true diff --git a/pkg/codegen/test_spec.yaml b/pkg/codegen/test_spec.yaml index b50f7491c5..f20cbecdca 100644 --- a/pkg/codegen/test_spec.yaml +++ b/pkg/codegen/test_spec.yaml @@ -164,15 +164,45 @@ components: format: date-time CatDead: + required: + - sliced_birds + - very_dead_since + - very_memorable_birds properties: name: type: string + favourite_birds: + type: array + items: + type: string + nullable: true + detested_birds: + type: array + items: + type: string + sliced_birds: + type: array + items: + type: string + forgettable_birds: + additionalProperties: + type: string + nullable: true + memorable_birds: + additionalProperties: + type: string + very_memorable_birds: + additionalProperties: + type: string dead_since: type: string format: date-time x-oapi-codegen-extra-tags: tag1: value1 tag2: value2 + very_dead_since: + type: string + format: date-time cause: type: string enum: [ car, dog, oldage ]