-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix generics used with function scoped types (#1883)
- Loading branch information
1 parent
10030b0
commit 83fe3ca
Showing
6 changed files
with
406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/swaggo/swag/testdata/generics_function_scoped/types" | ||
) | ||
|
||
// @Summary Generic Response | ||
// @Produce json | ||
// @Success 200 {object} types.GenericResponse[api.GetGeneric.User] | ||
// @Success 201 {object} types.GenericResponse[api.GetGeneric.Post] | ||
// @Router / [get] | ||
func GetGeneric(w http.ResponseWriter, r *http.Request) { | ||
type User struct { | ||
Username int `json:"username"` | ||
Email string `json:"email"` | ||
} | ||
type Post struct { | ||
Slug int `json:"slug"` | ||
Title string `json:"title"` | ||
} | ||
|
||
_ = types.GenericResponse[any]{} | ||
} | ||
|
||
// @Summary Generic Response With Custom Type Names | ||
// @Produce json | ||
// @Success 200 {object} types.GenericResponse[api.GetGenericRenamed.User] | ||
// @Success 201 {object} types.GenericResponse[api.GetGenericRenamed.Post] | ||
// @Router /renamed [get] | ||
func GetGenericRenamed(w http.ResponseWriter, r *http.Request) { | ||
type User struct { | ||
Username int `json:"username"` | ||
Email string `json:"email"` | ||
} // @Name RenamedUserData | ||
type Post struct { | ||
Slug int `json:"slug"` | ||
Title string `json:"title"` | ||
} // @Name RenamedPostData | ||
|
||
_ = types.GenericResponse[any]{} | ||
} | ||
|
||
// @Summary Multiple Generic Response | ||
// @Produce json | ||
// @Success 200 {object} types.GenericMultiResponse[api.GetGenericMulti.MyStructA, api.GetGenericMulti.MyStructB] | ||
// @Success 201 {object} types.GenericMultiResponse[api.GetGenericMulti.MyStructB, api.GetGenericMulti.MyStructA] | ||
// @Router /multi [get] | ||
func GetGenericMulti(w http.ResponseWriter, r *http.Request) { | ||
type MyStructA struct { | ||
SomeFieldA string `json:"some_field_a"` | ||
} | ||
type MyStructB struct { | ||
SomeFieldB string `json:"some_field_b"` | ||
} | ||
|
||
_ = types.GenericMultiResponse[any, any]{} | ||
} | ||
|
||
// @Summary Multiple Generic Response With Custom Type Names | ||
// @Produce json | ||
// @Success 200 {object} types.GenericMultiResponse[api.GetGenericMultiRenamed.MyStructA, api.GetGenericMultiRenamed.MyStructB] | ||
// @Success 201 {object} types.GenericMultiResponse[api.GetGenericMultiRenamed.MyStructB, api.GetGenericMultiRenamed.MyStructA] | ||
// @Router /multi-renamed [get] | ||
func GetGenericMultiRenamed(w http.ResponseWriter, r *http.Request) { | ||
type MyStructA struct { | ||
SomeFieldA string `json:"some_field_a"` | ||
} // @Name NameForMyStructA | ||
type MyStructB struct { | ||
SomeFieldB string `json:"some_field_b"` | ||
} // @Name NameForMyStructB | ||
|
||
_ = types.GenericMultiResponse[any, any]{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "This is a sample server.", | ||
"title": "Swagger Example API", | ||
"contact": {}, | ||
"version": "1.0" | ||
}, | ||
"host": "localhost:8080", | ||
"basePath": "/api", | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Generic Response", | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericResponse-api_GetGeneric_User" | ||
} | ||
}, | ||
"201": { | ||
"description": "Created", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericResponse-api_GetGeneric_Post" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/multi": { | ||
"get": { | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Multiple Generic Response", | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericMultiResponse-api_GetGenericMulti_MyStructA-api_GetGenericMulti_MyStructB" | ||
} | ||
}, | ||
"201": { | ||
"description": "Created", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericMultiResponse-api_GetGenericMulti_MyStructB-api_GetGenericMulti_MyStructA" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/multi-renamed": { | ||
"get": { | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Multiple Generic Response With Custom Type Names", | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericMultiResponse-NameForMyStructA-NameForMyStructB" | ||
} | ||
}, | ||
"201": { | ||
"description": "Created", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericMultiResponse-NameForMyStructB-NameForMyStructA" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"/renamed": { | ||
"get": { | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Generic Response With Custom Type Names", | ||
"responses": { | ||
"200": { | ||
"description": "OK", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericResponse-RenamedUserData" | ||
} | ||
}, | ||
"201": { | ||
"description": "Created", | ||
"schema": { | ||
"$ref": "#/definitions/types.GenericResponse-RenamedPostData" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"NameForMyStructA": { | ||
"type": "object", | ||
"properties": { | ||
"some_field_a": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"NameForMyStructB": { | ||
"type": "object", | ||
"properties": { | ||
"some_field_b": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"RenamedPostData": { | ||
"type": "object", | ||
"properties": { | ||
"slug": { | ||
"type": "integer" | ||
}, | ||
"title": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"RenamedUserData": { | ||
"type": "object", | ||
"properties": { | ||
"email": { | ||
"type": "string" | ||
}, | ||
"username": { | ||
"type": "integer" | ||
} | ||
} | ||
}, | ||
"api.GetGeneric.Post": { | ||
"type": "object", | ||
"properties": { | ||
"slug": { | ||
"type": "integer" | ||
}, | ||
"title": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"api.GetGeneric.User": { | ||
"type": "object", | ||
"properties": { | ||
"email": { | ||
"type": "string" | ||
}, | ||
"username": { | ||
"type": "integer" | ||
} | ||
} | ||
}, | ||
"api.GetGenericMulti.MyStructA": { | ||
"type": "object", | ||
"properties": { | ||
"some_field_a": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"api.GetGenericMulti.MyStructB": { | ||
"type": "object", | ||
"properties": { | ||
"some_field_b": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericMultiResponse-NameForMyStructA-NameForMyStructB": { | ||
"type": "object", | ||
"properties": { | ||
"data_t": { | ||
"$ref": "#/definitions/NameForMyStructA" | ||
}, | ||
"data_x": { | ||
"$ref": "#/definitions/NameForMyStructB" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericMultiResponse-NameForMyStructB-NameForMyStructA": { | ||
"type": "object", | ||
"properties": { | ||
"data_t": { | ||
"$ref": "#/definitions/NameForMyStructB" | ||
}, | ||
"data_x": { | ||
"$ref": "#/definitions/NameForMyStructA" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericMultiResponse-api_GetGenericMulti_MyStructA-api_GetGenericMulti_MyStructB": { | ||
"type": "object", | ||
"properties": { | ||
"data_t": { | ||
"$ref": "#/definitions/api.GetGenericMulti.MyStructA" | ||
}, | ||
"data_x": { | ||
"$ref": "#/definitions/api.GetGenericMulti.MyStructB" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericMultiResponse-api_GetGenericMulti_MyStructB-api_GetGenericMulti_MyStructA": { | ||
"type": "object", | ||
"properties": { | ||
"data_t": { | ||
"$ref": "#/definitions/api.GetGenericMulti.MyStructB" | ||
}, | ||
"data_x": { | ||
"$ref": "#/definitions/api.GetGenericMulti.MyStructA" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericResponse-RenamedPostData": { | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"$ref": "#/definitions/RenamedPostData" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericResponse-RenamedUserData": { | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"$ref": "#/definitions/RenamedUserData" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericResponse-api_GetGeneric_Post": { | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"$ref": "#/definitions/api.GetGeneric.Post" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"types.GenericResponse-api_GetGeneric_User": { | ||
"type": "object", | ||
"properties": { | ||
"data": { | ||
"$ref": "#/definitions/api.GetGeneric.User" | ||
}, | ||
"status": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.