[Go-Server] Support for an endpoint returning a file to the client - #15206 (#16748)

* Support for an endpoint returning a file to a client.

* Spaces to tabs conversion

* Add an example endpoint for download a file

* Regenerate after merging main
This commit is contained in:
Ian Cubbon 2023-10-16 20:42:27 -07:00 committed by GitHub
parent 1dd9590064
commit e0738a6e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 251 additions and 6 deletions

7
.gitignore vendored
View File

@ -49,6 +49,8 @@ nb-configuration.xml
/target
/generated-files
test-output/
nbactions.xml
test-output/
# website
website/build/
@ -71,6 +73,7 @@ samples/client/petstore/build
samples/client/petstore/cpp-qt/PetStore/moc_*
samples/client/petstore/cpp-qt/PetStore/*.o
samples/client/petstore/cpp-qt/build-*
samples/client/petstore/cpp-qt/build-*
samples/client/petstore/cpp-qt/PetStore/PetStore
samples/client/petstore/cpp-qt/PetStore/Makefile
samples/client/petstore/cpp-qt/PetStore/PetStore.pro.user
@ -97,7 +100,9 @@ samples/client/petstore/java/jersey2/build/
samples/client/petstore/java/okhttp-gson/.gradle/
samples/client/petstore/java/okhttp-gson/build/
samples/client/petstore/java/feign/build/
samples/client/petstore/java/feign10x/build/
samples/client/petstore/java/feign/project/
samples/client/petstore/java/feign10x/project/
samples/client/petstore/java/retrofit/build/
samples/client/petstore/java/retrofit2/build/
samples/client/petstore/java/retrofit2/hello.txt
@ -105,6 +110,7 @@ samples/client/petstore/java/retrofit2rx/build/
samples/client/petstore/java/default/build/
samples/client/petstore/scala/build/
samples/client/petstore/java/resttemplate/hello.txt
samples/client/petstore/java/retrofit2/hello.txt
samples/client/petstore/java/feign/hello.txt
samples/client/petstore/java/jersey2-java6/project/
samples/client/petstore/java/jersey2-java8/project/
@ -187,7 +193,6 @@ samples/server/petstore/php-slim4/composer.lock
samples/server/petstore/php-symfony/SymfonyBundle-php/composer.lock
samples/server/petstore/php-mezzio-ph/composer.lock
samples/server/petstore/php-mezzio-ph-modern/composer.lock
samples/client/petstore/php-nextgen/OpenAPIClient-php/.phplint.cache/
# ts
samples/client/petstore/typescript-angular2/npm/npm-debug.log

View File

@ -89,18 +89,33 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
func EncodeJSONResponse(i interface{}, status *int,{{#addResponseHeaders}} headers map[string][]string,{{/addResponseHeaders}} w http.ResponseWriter) error {
{{#addResponseHeaders}}
wHeader := w.Header()
{{#addResponseHeaders}}
for key, values := range headers {
for _, value := range values {
wHeader.Add(key, value)
}
}
{{/addResponseHeaders}}
f, ok := i.(*os.File)
if ok {
data, err := io.ReadAll(f)
if err != nil {
return err
}
wHeader.Set("Content-Type", http.DetectContentType(data))
wHeader.Set("Content-Disposition", "attachment; filename="+f.Name())
if status != nil {
w.WriteHeader(*status)
} else {
w.WriteHeader(http.StatusOK)
}
_, err = w.Write(data)
return err
}
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
{{/addResponseHeaders}}
{{^addResponseHeaders}}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
{{/addResponseHeaders}}
if status != nil {
w.WriteHeader(*status)
} else {

View File

@ -332,6 +332,32 @@ paths:
description: file to upload
type: string
format: binary
get:
tags:
- pet
summary: Returns the image for the Pet that has been previously uploaded
description: Returns the image for the Pet that has been previously uploaded
operationId: getPetImageById
parameters:
- name: petId
in: path
description: ID of pet to return
required: true
schema:
type: integer
format: int64
responses:
'200':
description: successful operation
content:
image/jpeg:
schema:
type: string
format: binary
'400':
description: Invalid ID supplied
'404':
description: Pet not found
/store/inventory:
get:
tags:

View File

@ -64,7 +64,25 @@ func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string,
wHeader.Add(key, value)
}
}
f, ok := i.(*os.File)
if ok {
data, err := io.ReadAll(f)
if err != nil {
return err
}
wHeader.Set("Content-Type", http.DetectContentType(data))
wHeader.Set("Content-Disposition", "attachment; filename="+f.Name())
if status != nil {
w.WriteHeader(*status)
} else {
w.WriteHeader(http.StatusOK)
}
_, err = w.Write(data)
return err
}
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
if status != nil {
w.WriteHeader(*status)
} else {

View File

@ -298,6 +298,34 @@ paths:
tags:
- pet
/pet/{petId}/uploadImage:
get:
description: Returns the image for the Pet that has been previously uploaded
operationId: getPetImageById
parameters:
- description: ID of pet to return
explode: false
in: path
name: petId
required: true
schema:
format: int64
type: integer
style: simple
responses:
"200":
content:
image/jpeg:
schema:
format: binary
type: string
description: successful operation
"400":
description: Invalid ID supplied
"404":
description: Pet not found
summary: Returns the image for the Pet that has been previously uploaded
tags:
- pet
post:
description: ""
operationId: uploadFile

View File

@ -28,6 +28,7 @@ type PetAPIRouter interface {
// Deprecated
FindPetsByTags(http.ResponseWriter, *http.Request)
GetPetById(http.ResponseWriter, *http.Request)
GetPetImageById(http.ResponseWriter, *http.Request)
UpdatePet(http.ResponseWriter, *http.Request)
UpdatePetWithForm(http.ResponseWriter, *http.Request)
UploadFile(http.ResponseWriter, *http.Request)
@ -69,6 +70,7 @@ type PetAPIServicer interface {
// Deprecated
FindPetsByTags(context.Context, []string) (ImplResponse, error)
GetPetById(context.Context, int64) (ImplResponse, error)
GetPetImageById(context.Context, int64) (ImplResponse, error)
UpdatePet(context.Context, Pet) (ImplResponse, error)
UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error)
UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error)

View File

@ -80,6 +80,11 @@ func (c *PetAPIController) Routes() Routes {
"/v2/pet/{petId}",
c.GetPetById,
},
"GetPetImageById": Route{
strings.ToUpper("Get"),
"/v2/pet/{petId}/uploadImage",
c.GetPetImageById,
},
"UpdatePet": Route{
strings.ToUpper("Put"),
"/v2/pet",
@ -249,6 +254,27 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) {
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}
// GetPetImageById - Returns the image for the Pet that has been previously uploaded
func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
petIdParam, err := parseNumericParameter[int64](
params["petId"],
WithRequire[int64](parseInt64),
)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.GetPetImageById(r.Context(), petIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}
// UpdatePet - Update an existing pet
func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) {
petParam := Pet{}

View File

@ -112,6 +112,23 @@ func (s *PetAPIService) GetPetById(ctx context.Context, petId int64) (ImplRespon
return Response(http.StatusNotImplemented, nil), errors.New("GetPetById method not implemented")
}
// GetPetImageById - Returns the image for the Pet that has been previously uploaded
func (s *PetAPIService) GetPetImageById(ctx context.Context, petId int64) (ImplResponse, error) {
// TODO - update GetPetImageById with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
// TODO: Uncomment the next line to return response Response(200, *os.File{}) or use other options such as http.Ok ...
// return Response(200, *os.File{}), nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil
return Response(http.StatusNotImplemented, nil), errors.New("GetPetImageById method not implemented")
}
// UpdatePet - Update an existing pet
func (s *PetAPIService) UpdatePet(ctx context.Context, pet Pet) (ImplResponse, error) {
// TODO - update UpdatePet with the required logic for this service method.

View File

@ -68,7 +68,25 @@ func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string,
wHeader.Add(key, value)
}
}
f, ok := i.(*os.File)
if ok {
data, err := io.ReadAll(f)
if err != nil {
return err
}
wHeader.Set("Content-Type", http.DetectContentType(data))
wHeader.Set("Content-Disposition", "attachment; filename="+f.Name())
if status != nil {
w.WriteHeader(*status)
} else {
w.WriteHeader(http.StatusOK)
}
_, err = w.Write(data)
return err
}
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
if status != nil {
w.WriteHeader(*status)
} else {

View File

@ -298,6 +298,34 @@ paths:
tags:
- pet
/pet/{petId}/uploadImage:
get:
description: Returns the image for the Pet that has been previously uploaded
operationId: getPetImageById
parameters:
- description: ID of pet to return
explode: false
in: path
name: petId
required: true
schema:
format: int64
type: integer
style: simple
responses:
"200":
content:
image/jpeg:
schema:
format: binary
type: string
description: successful operation
"400":
description: Invalid ID supplied
"404":
description: Pet not found
summary: Returns the image for the Pet that has been previously uploaded
tags:
- pet
post:
description: ""
operationId: uploadFile

View File

@ -28,6 +28,7 @@ type PetAPIRouter interface {
// Deprecated
FindPetsByTags(http.ResponseWriter, *http.Request)
GetPetById(http.ResponseWriter, *http.Request)
GetPetImageById(http.ResponseWriter, *http.Request)
UpdatePet(http.ResponseWriter, *http.Request)
UpdatePetWithForm(http.ResponseWriter, *http.Request)
UploadFile(http.ResponseWriter, *http.Request)
@ -69,6 +70,7 @@ type PetAPIServicer interface {
// Deprecated
FindPetsByTags(context.Context, []string) (ImplResponse, error)
GetPetById(context.Context, int64) (ImplResponse, error)
GetPetImageById(context.Context, int64) (ImplResponse, error)
UpdatePet(context.Context, Pet) (ImplResponse, error)
UpdatePetWithForm(context.Context, int64, string, string) (ImplResponse, error)
UploadFile(context.Context, int64, string, *os.File) (ImplResponse, error)

View File

@ -80,6 +80,11 @@ func (c *PetAPIController) Routes() Routes {
"/v2/pet/{petId}",
c.GetPetById,
},
"GetPetImageById": Route{
strings.ToUpper("Get"),
"/v2/pet/{petId}/uploadImage",
c.GetPetImageById,
},
"UpdatePet": Route{
strings.ToUpper("Put"),
"/v2/pet",
@ -246,6 +251,26 @@ func (c *PetAPIController) GetPetById(w http.ResponseWriter, r *http.Request) {
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}
// GetPetImageById - Returns the image for the Pet that has been previously uploaded
func (c *PetAPIController) GetPetImageById(w http.ResponseWriter, r *http.Request) {
petIdParam, err := parseNumericParameter[int64](
chi.URLParam(r, "petId"),
WithRequire[int64](parseInt64),
)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.GetPetImageById(r.Context(), petIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
}
// UpdatePet - Update an existing pet
func (c *PetAPIController) UpdatePet(w http.ResponseWriter, r *http.Request) {
petParam := Pet{}

View File

@ -112,6 +112,23 @@ func (s *PetAPIService) GetPetById(ctx context.Context, petId int64) (ImplRespon
return Response(http.StatusNotImplemented, nil), errors.New("GetPetById method not implemented")
}
// GetPetImageById - Returns the image for the Pet that has been previously uploaded
func (s *PetAPIService) GetPetImageById(ctx context.Context, petId int64) (ImplResponse, error) {
// TODO - update GetPetImageById with the required logic for this service method.
// Add api_pet_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.
// TODO: Uncomment the next line to return response Response(200, *os.File{}) or use other options such as http.Ok ...
// return Response(200, *os.File{}), nil
// TODO: Uncomment the next line to return response Response(400, {}) or use other options such as http.Ok ...
// return Response(400, nil),nil
// TODO: Uncomment the next line to return response Response(404, {}) or use other options such as http.Ok ...
// return Response(404, nil),nil
return Response(http.StatusNotImplemented, nil), errors.New("GetPetImageById method not implemented")
}
// UpdatePet - Update an existing pet
func (s *PetAPIService) UpdatePet(ctx context.Context, pet Pet) (ImplResponse, error) {
// TODO - update UpdatePet with the required logic for this service method.

View File

@ -64,7 +64,25 @@ func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string,
wHeader.Add(key, value)
}
}
f, ok := i.(*os.File)
if ok {
data, err := io.ReadAll(f)
if err != nil {
return err
}
wHeader.Set("Content-Type", http.DetectContentType(data))
wHeader.Set("Content-Disposition", "attachment; filename="+f.Name())
if status != nil {
w.WriteHeader(*status)
} else {
w.WriteHeader(http.StatusOK)
}
_, err = w.Write(data)
return err
}
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
if status != nil {
w.WriteHeader(*status)
} else {