diff --git a/internal/test/array_pointer/array_pointer.gen.go b/internal/test/array_pointer/array_pointer.gen.go new file mode 100644 index 0000000000..599a79950b --- /dev/null +++ b/internal/test/array_pointer/array_pointer.gen.go @@ -0,0 +1,23 @@ +// Package array_pointer provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.0.0-00010101000000-000000000000 DO NOT EDIT. +package array_pointer + +// DateHistogram defines model for DateHistogram. +type DateHistogram struct { + DocCount *int `json:"doc_count,omitempty"` + Key *string `json:"key,omitempty"` + KeyAsString *string `json:"key_as_string"` +} + +// Response defines model for Response. +type Response struct { + Status *int `json:"status,omitempty"` +} + +// ResponsePlaceholder defines model for response.Placeholder. +type ResponsePlaceholder struct { + Hello *string `json:"hello,omitempty"` + Past7DaysHistogram []*DateHistogram `json:"past_7_days_histogram"` + Status *int `json:"status,omitempty"` +} diff --git a/internal/test/array_pointer/cfg.yaml b/internal/test/array_pointer/cfg.yaml new file mode 100644 index 0000000000..5d78facf2b --- /dev/null +++ b/internal/test/array_pointer/cfg.yaml @@ -0,0 +1,5 @@ +# yaml-language-server: $schema=../../configuration-schema.json +package: array_pointer +output: array_pointer.gen.go +generate: + models: true \ No newline at end of file diff --git a/internal/test/array_pointer/doc.go b/internal/test/array_pointer/doc.go new file mode 100644 index 0000000000..b8ada68938 --- /dev/null +++ b/internal/test/array_pointer/doc.go @@ -0,0 +1,3 @@ +package array_pointer + +//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml openapi.yaml diff --git a/internal/test/array_pointer/openapi.yaml b/internal/test/array_pointer/openapi.yaml new file mode 100644 index 0000000000..81dbddb5a7 --- /dev/null +++ b/internal/test/array_pointer/openapi.yaml @@ -0,0 +1,51 @@ +openapi: "3.0.1" +info: + version: 1.0.0 + title: Tests array pointer composition +paths: + /placeholder: + get: + operationId: placeholder + responses: + default: + description: placeholder + content: + application/json: + schema: + $ref: "#/components/schemas/response.Placeholder" +components: + schemas: + response.Placeholder: + x-go-embedding: true + type: object + allOf: + - $ref: "#/components/schemas/Response" + - type: object + properties: + hello: + type: string + past_7_days_histogram: + type: array + x-go-type-skip-optional-pointer: true + nullable: true + items: + allOf: + - $ref: '#/components/schemas/DateHistogram' + + DateHistogram: + type: object + properties: + key: + type: string + key_as_string: + type: string + nullable: true + doc_count: + type: integer + + Response: + x-go-name: Response + type: object + properties: + status: + type: integer \ No newline at end of file diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index 1d03fbdd0e..da8e3133eb 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -586,8 +586,10 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem arrayType.RefType = typeName } + arrayType.AdditionalPropertiesType = &arrayType + arrayType.AdditionalPropertiesType.OAPISchema = schema outSchema.ArrayType = &arrayType - outSchema.GoType = "[]" + arrayType.TypeDecl() + outSchema.GoType = "[]" + additionalPropertiesType(arrayType) outSchema.AdditionalTypes = arrayType.AdditionalTypes outSchema.Properties = arrayType.Properties outSchema.DefineViaAlias = true @@ -795,10 +797,8 @@ func GenFieldsFromProperties(props []Property) []string { } func additionalPropertiesType(schema Schema) string { - addPropsType := schema.AdditionalPropertiesType.GoType - if schema.AdditionalPropertiesType.RefType != "" { - addPropsType = schema.AdditionalPropertiesType.RefType - } + addPropsType := schema.AdditionalPropertiesType.TypeDecl() + if schema.AdditionalPropertiesType.OAPISchema != nil && schema.AdditionalPropertiesType.OAPISchema.Nullable { addPropsType = "*" + addPropsType }