diff --git a/examples/petstore-expanded/petstore-client.gen.go b/examples/petstore-expanded/petstore-client.gen.go index 7dd7eb0ed2..fc2915471e 100644 --- a/examples/petstore-expanded/petstore-client.gen.go +++ b/examples/petstore-expanded/petstore-client.gen.go @@ -440,11 +440,16 @@ type ClientWithResponsesInterface interface { FindPetByIDWithResponse(ctx context.Context, id int64, reqEditors ...RequestEditorFn) (*FindPetByIDResponse, error) } +// FindPetsResponseJSON200 represents a possible response for the FindPets request. +type FindPetsResponseJSON200 []Pet + +// FindPetsResponseJSONDefault represents a possible response for the FindPets request. +type FindPetsResponseJSONDefault Error type FindPetsResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *[]Pet - JSONDefault *Error + JSON200 *FindPetsResponseJSON200 + JSONDefault *FindPetsResponseJSONDefault } // Status returns HTTPResponse.Status @@ -463,11 +468,16 @@ func (r FindPetsResponse) StatusCode() int { return 0 } +// AddPetResponseJSON200 represents a possible response for the AddPet request. +type AddPetResponseJSON200 Pet + +// AddPetResponseJSONDefault represents a possible response for the AddPet request. +type AddPetResponseJSONDefault Error type AddPetResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *Pet - JSONDefault *Error + JSON200 *AddPetResponseJSON200 + JSONDefault *AddPetResponseJSONDefault } // Status returns HTTPResponse.Status @@ -486,10 +496,12 @@ func (r AddPetResponse) StatusCode() int { return 0 } +// DeletePetResponseJSONDefault represents a possible response for the DeletePet request. +type DeletePetResponseJSONDefault Error type DeletePetResponse struct { Body []byte HTTPResponse *http.Response - JSONDefault *Error + JSONDefault *DeletePetResponseJSONDefault } // Status returns HTTPResponse.Status @@ -508,11 +520,16 @@ func (r DeletePetResponse) StatusCode() int { return 0 } +// FindPetByIDResponseJSON200 represents a possible response for the FindPetByID request. +type FindPetByIDResponseJSON200 Pet + +// FindPetByIDResponseJSONDefault represents a possible response for the FindPetByID request. +type FindPetByIDResponseJSONDefault Error type FindPetByIDResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *Pet - JSONDefault *Error + JSON200 *FindPetByIDResponseJSON200 + JSONDefault *FindPetByIDResponseJSONDefault } // Status returns HTTPResponse.Status @@ -590,14 +607,14 @@ func ParseFindPetsResponse(rsp *http.Response) (*FindPetsResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest []Pet + var dest FindPetsResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest Error + var dest FindPetsResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } @@ -623,14 +640,14 @@ func ParseAddPetResponse(rsp *http.Response) (*AddPetResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest Pet + var dest AddPetResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest Error + var dest AddPetResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } @@ -656,7 +673,7 @@ func ParseDeletePetResponse(rsp *http.Response) (*DeletePetResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest Error + var dest DeletePetResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } @@ -682,14 +699,14 @@ func ParseFindPetByIDResponse(rsp *http.Response) (*FindPetByIDResponse, error) switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest Pet + var dest FindPetByIDResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest Error + var dest FindPetByIDResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/internal/test/components/components.gen.go b/internal/test/components/components.gen.go index 3aa31bb73b..e166e49afc 100644 --- a/internal/test/components/components.gen.go +++ b/internal/test/components/components.gen.go @@ -1060,30 +1060,35 @@ type ClientWithResponsesInterface interface { BodyWithAddPropsWithResponse(ctx context.Context, body BodyWithAddPropsJSONRequestBody, reqEditors ...RequestEditorFn) (*BodyWithAddPropsResponse, error) } -type EnsureEverythingIsReferencedResponse struct { - Body []byte - HTTPResponse *http.Response - JSON200 *struct { +// EnsureEverythingIsReferencedResponseJSON200 represents a possible response for the EnsureEverythingIsReferenced request. +type EnsureEverythingIsReferencedResponseJSON200 struct { - // Has additional properties with schema for dictionaries - Five *AdditionalPropertiesObject5 `json:"five,omitempty"` + // Has additional properties with schema for dictionaries + Five *AdditionalPropertiesObject5 `json:"five,omitempty"` - // Has anonymous field which has additional properties - Four *AdditionalPropertiesObject4 `json:"four,omitempty"` - JsonField *ObjectWithJsonField `json:"jsonField,omitempty"` + // Has anonymous field which has additional properties + Four *AdditionalPropertiesObject4 `json:"four,omitempty"` + JsonField *ObjectWithJsonField `json:"jsonField,omitempty"` - // Has additional properties of type int - One *AdditionalPropertiesObject1 `json:"one,omitempty"` + // Has additional properties of type int + One *AdditionalPropertiesObject1 `json:"one,omitempty"` - // Allows any additional property - Three *AdditionalPropertiesObject3 `json:"three,omitempty"` + // Allows any additional property + Three *AdditionalPropertiesObject3 `json:"three,omitempty"` - // Does not allow additional properties - Two *AdditionalPropertiesObject2 `json:"two,omitempty"` - } - JSONDefault *struct { - Field SchemaObject `json:"Field"` - } + // Does not allow additional properties + Two *AdditionalPropertiesObject2 `json:"two,omitempty"` +} + +// EnsureEverythingIsReferencedResponseJSONDefault represents a possible response for the EnsureEverythingIsReferenced request. +type EnsureEverythingIsReferencedResponseJSONDefault struct { + Field SchemaObject `json:"Field"` +} +type EnsureEverythingIsReferencedResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *EnsureEverythingIsReferencedResponseJSON200 + JSONDefault *EnsureEverythingIsReferencedResponseJSONDefault } // Status returns HTTPResponse.Status @@ -1202,33 +1207,14 @@ func ParseEnsureEverythingIsReferencedResponse(rsp *http.Response) (*EnsureEvery switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest struct { - - // Has additional properties with schema for dictionaries - Five *AdditionalPropertiesObject5 `json:"five,omitempty"` - - // Has anonymous field which has additional properties - Four *AdditionalPropertiesObject4 `json:"four,omitempty"` - JsonField *ObjectWithJsonField `json:"jsonField,omitempty"` - - // Has additional properties of type int - One *AdditionalPropertiesObject1 `json:"one,omitempty"` - - // Allows any additional property - Three *AdditionalPropertiesObject3 `json:"three,omitempty"` - - // Does not allow additional properties - Two *AdditionalPropertiesObject2 `json:"two,omitempty"` - } + var dest EnsureEverythingIsReferencedResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest struct { - Field SchemaObject `json:"Field"` - } + var dest EnsureEverythingIsReferencedResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/internal/test/issues/issue-312/issue.gen.go b/internal/test/issues/issue-312/issue.gen.go index e8c0604749..2a499cd6db 100644 --- a/internal/test/issues/issue-312/issue.gen.go +++ b/internal/test/issues/issue-312/issue.gen.go @@ -296,10 +296,12 @@ type ClientWithResponsesInterface interface { ValidatePetsWithResponse(ctx context.Context, body ValidatePetsJSONRequestBody, reqEditors ...RequestEditorFn) (*ValidatePetsResponse, error) } +// GetPetResponseJSON200 represents a possible response for the GetPet request. +type GetPetResponseJSON200 Pet type GetPetResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *Pet + JSON200 *GetPetResponseJSON200 } // Status returns HTTPResponse.Status @@ -318,11 +320,16 @@ func (r GetPetResponse) StatusCode() int { return 0 } +// ValidatePetsResponseJSON200 represents a possible response for the ValidatePets request. +type ValidatePetsResponseJSON200 []Pet + +// ValidatePetsResponseJSONDefault represents a possible response for the ValidatePets request. +type ValidatePetsResponseJSONDefault Error type ValidatePetsResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *[]Pet - JSONDefault *Error + JSON200 *ValidatePetsResponseJSON200 + JSONDefault *ValidatePetsResponseJSONDefault } // Status returns HTTPResponse.Status @@ -382,7 +389,7 @@ func ParseGetPetResponse(rsp *http.Response) (*GetPetResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest Pet + var dest GetPetResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } @@ -408,14 +415,14 @@ func ParseValidatePetsResponse(rsp *http.Response) (*ValidatePetsResponse, error switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest []Pet + var dest ValidatePetsResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest Error + var dest ValidatePetsResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/internal/test/issues/issue-52/issue.gen.go b/internal/test/issues/issue-52/issue.gen.go index 1a400279a3..b8f7bc25a5 100644 --- a/internal/test/issues/issue-52/issue.gen.go +++ b/internal/test/issues/issue-52/issue.gen.go @@ -256,10 +256,12 @@ type ClientWithResponsesInterface interface { ExampleGetWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ExampleGetResponse, error) } +// ExampleGetResponseJSON200 represents a possible response for the ExampleGet request. +type ExampleGetResponseJSON200 Document type ExampleGetResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *Document + JSON200 *ExampleGetResponseJSON200 } // Status returns HTTPResponse.Status @@ -302,7 +304,7 @@ func ParseExampleGetResponse(rsp *http.Response) (*ExampleGetResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest Document + var dest ExampleGetResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/internal/test/issues/issue-grab_import_names/issue.gen.go b/internal/test/issues/issue-grab_import_names/issue.gen.go index dca6350951..6c7be7ed94 100644 --- a/internal/test/issues/issue-grab_import_names/issue.gen.go +++ b/internal/test/issues/issue-grab_import_names/issue.gen.go @@ -216,10 +216,12 @@ type ClientWithResponsesInterface interface { GetFooWithResponse(ctx context.Context, params *GetFooParams, reqEditors ...RequestEditorFn) (*GetFooResponse, error) } +// GetFooResponseJSON200 represents a possible response for the GetFoo request. +type GetFooResponseJSON200 string type GetFooResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *string + JSON200 *GetFooResponseJSON200 } // Status returns HTTPResponse.Status @@ -262,7 +264,7 @@ func ParseGetFooResponse(rsp *http.Response) (*GetFooResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest string + var dest GetFooResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/internal/test/issues/issue-illegal_enum_names/issue.gen.go b/internal/test/issues/issue-illegal_enum_names/issue.gen.go index 93332b522f..c346854853 100644 --- a/internal/test/issues/issue-illegal_enum_names/issue.gen.go +++ b/internal/test/issues/issue-illegal_enum_names/issue.gen.go @@ -207,10 +207,12 @@ type ClientWithResponsesInterface interface { GetFooWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetFooResponse, error) } +// GetFooResponseJSON200 represents a possible response for the GetFoo request. +type GetFooResponseJSON200 []Bar type GetFooResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *[]Bar + JSON200 *GetFooResponseJSON200 } // Status returns HTTPResponse.Status @@ -253,7 +255,7 @@ func ParseGetFooResponse(rsp *http.Response) (*GetFooResponse, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest []Bar + var dest GetFooResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/internal/test/schemas/schemas.gen.go b/internal/test/schemas/schemas.gen.go index 985c1a359e..61709e15dd 100644 --- a/internal/test/schemas/schemas.gen.go +++ b/internal/test/schemas/schemas.gen.go @@ -602,18 +602,20 @@ type ClientWithResponsesInterface interface { Issue9WithResponse(ctx context.Context, params *Issue9Params, body Issue9JSONRequestBody, reqEditors ...RequestEditorFn) (*Issue9Response, error) } +// EnsureEverythingIsReferencedResponseJSON200 represents a possible response for the EnsureEverythingIsReferenced request. +type EnsureEverythingIsReferencedResponseJSON200 struct { + AnyType1 *AnyType1 `json:"anyType1,omitempty"` + + // AnyType2 represents any type. + // + // This should be an interface{} + AnyType2 *AnyType2 `json:"anyType2,omitempty"` + CustomStringType *CustomStringType `foo:"bar" json:"customStringType,omitempty"` +} type EnsureEverythingIsReferencedResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *struct { - AnyType1 *AnyType1 `json:"anyType1,omitempty"` - - // AnyType2 represents any type. - // - // This should be an interface{} - AnyType2 *AnyType2 `json:"anyType2,omitempty"` - CustomStringType *CustomStringType `foo:"bar" json:"customStringType,omitempty"` - } + JSON200 *EnsureEverythingIsReferencedResponseJSON200 } // Status returns HTTPResponse.Status @@ -632,13 +634,24 @@ func (r EnsureEverythingIsReferencedResponse) StatusCode() int { return 0 } +// Issue127ResponseJSON200 represents a possible response for the Issue127 request. +type Issue127ResponseJSON200 GenericObject + +// Issue127ResponseXML200 represents a possible response for the Issue127 request. +type Issue127ResponseXML200 GenericObject + +// Issue127ResponseYAML200 represents a possible response for the Issue127 request. +type Issue127ResponseYAML200 GenericObject + +// Issue127ResponseJSONDefault represents a possible response for the Issue127 request. +type Issue127ResponseJSONDefault GenericObject type Issue127Response struct { Body []byte HTTPResponse *http.Response - JSON200 *GenericObject - XML200 *GenericObject - YAML200 *GenericObject - JSONDefault *GenericObject + JSON200 *Issue127ResponseJSON200 + XML200 *Issue127ResponseXML200 + YAML200 *Issue127ResponseYAML200 + JSONDefault *Issue127ResponseJSONDefault } // Status returns HTTPResponse.Status @@ -856,15 +869,7 @@ func ParseEnsureEverythingIsReferencedResponse(rsp *http.Response) (*EnsureEvery switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest struct { - AnyType1 *AnyType1 `json:"anyType1,omitempty"` - - // AnyType2 represents any type. - // - // This should be an interface{} - AnyType2 *AnyType2 `json:"anyType2,omitempty"` - CustomStringType *CustomStringType `foo:"bar" json:"customStringType,omitempty"` - } + var dest EnsureEverythingIsReferencedResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } @@ -890,28 +895,28 @@ func ParseIssue127Response(rsp *http.Response) (*Issue127Response, error) { switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest GenericObject + var dest Issue127ResponseJSON200 if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: - var dest GenericObject + var dest Issue127ResponseJSONDefault if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSONDefault = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "xml") && rsp.StatusCode == 200: - var dest GenericObject + var dest Issue127ResponseXML200 if err := xml.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.XML200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "yaml") && rsp.StatusCode == 200: - var dest GenericObject + var dest Issue127ResponseYAML200 if err := yaml.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } diff --git a/pkg/codegen/codegen_test.go b/pkg/codegen/codegen_test.go index 2e02d0b59a..22513c7d2e 100644 --- a/pkg/codegen/codegen_test.go +++ b/pkg/codegen/codegen_test.go @@ -146,11 +146,11 @@ func TestExampleOpenAPICodeGeneration(t *testing.T) { type GetTestByNameResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *[]Test - XML200 *[]Test - JSON422 *[]interface{} - XML422 *[]interface{} - JSONDefault *Error + JSON200 *GetTestByNameResponseJSON200 + XML200 *GetTestByNameResponseXML200 + JSON422 *GetTestByNameResponseJSON422 + XML422 *GetTestByNameResponseXML422 + JSONDefault *GetTestByNameResponseJSONDefault }`) // Check that the helper methods are generated correctly: diff --git a/pkg/codegen/template_helpers.go b/pkg/codegen/template_helpers.go index a41b55428d..248347eaf8 100644 --- a/pkg/codegen/template_helpers.go +++ b/pkg/codegen/template_helpers.go @@ -152,7 +152,7 @@ func genResponseUnmarshal(op *OperationDefinition) string { " return nil, err \n"+ "}\n"+ "response.%s = &dest", - typeDefinition.Schema.TypeDecl(), + op.OperationId+"Response"+typeDefinition.TypeName, typeDefinition.TypeName) caseKey, caseClause := buildUnmarshalCase(typeDefinition, caseAction, "json") @@ -168,7 +168,7 @@ func genResponseUnmarshal(op *OperationDefinition) string { " return nil, err \n"+ "}\n"+ "response.%s = &dest", - typeDefinition.Schema.TypeDecl(), + op.OperationId+"Response"+typeDefinition.TypeName, typeDefinition.TypeName) caseKey, caseClause := buildUnmarshalCase(typeDefinition, caseAction, "yaml") handledCaseClauses[caseKey] = caseClause @@ -183,7 +183,7 @@ func genResponseUnmarshal(op *OperationDefinition) string { " return nil, err \n"+ "}\n"+ "response.%s = &dest", - typeDefinition.Schema.TypeDecl(), + op.OperationId+"Response"+typeDefinition.TypeName, typeDefinition.TypeName) caseKey, caseClause := buildUnmarshalCase(typeDefinition, caseAction, "xml") handledCaseClauses[caseKey] = caseClause diff --git a/pkg/codegen/templates/client-with-responses.tmpl b/pkg/codegen/templates/client-with-responses.tmpl index 829fe7775f..58977c3d43 100644 --- a/pkg/codegen/templates/client-with-responses.tmpl +++ b/pkg/codegen/templates/client-with-responses.tmpl @@ -40,11 +40,15 @@ type ClientWithResponsesInterface interface { } {{range .}}{{$opid := .OperationId}}{{$op := .}} +{{- range getResponseTypeDefinitions .}} +// {{$opid}}Response{{.TypeName}} represents a possible response for the {{$opid}} request. +type {{$opid}}Response{{.TypeName}} {{if opts.AliasTypes}}= {{end}}{{.Schema.TypeDecl}} +{{- end}} type {{$opid | ucFirst}}Response struct { Body []byte HTTPResponse *http.Response {{- range getResponseTypeDefinitions .}} - {{.TypeName}} *{{.Schema.TypeDecl}} + {{.TypeName}} *{{$opid}}Response{{.TypeName}} {{- end}} } diff --git a/pkg/codegen/templates/templates.gen.go b/pkg/codegen/templates/templates.gen.go index f84945a9ea..aeb0959f3f 100644 --- a/pkg/codegen/templates/templates.gen.go +++ b/pkg/codegen/templates/templates.gen.go @@ -349,11 +349,15 @@ type ClientWithResponsesInterface interface { } {{range .}}{{$opid := .OperationId}}{{$op := .}} +{{- range getResponseTypeDefinitions .}} +// {{$opid}}Response{{.TypeName}} represents a possible response for the {{$opid}} request. +type {{$opid}}Response{{.TypeName}} {{if opts.AliasTypes}}= {{end}}{{.Schema.TypeDecl}} +{{- end}} type {{$opid | ucFirst}}Response struct { Body []byte HTTPResponse *http.Response {{- range getResponseTypeDefinitions .}} - {{.TypeName}} *{{.Schema.TypeDecl}} + {{.TypeName}} *{{$opid}}Response{{.TypeName}} {{- end}} }