[go-server] Add tests for primitive types in request/response (#20474)

* add test for primitive request/response

* add new files
This commit is contained in:
William Cheng 2025-01-15 16:56:16 +08:00 committed by GitHub
parent dcf472a034
commit 4c73faf737
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 360 additions and 2 deletions

View File

@ -849,11 +849,37 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ApiResponse' $ref: '#/components/schemas/ApiResponse'
/fake/collection/test:
post:
summary: POST a test batch
operationId: fakePostTest
tags:
- fake
requestBody:
$ref: '#/components/requestBodies/TestBody'
responses:
'200':
$ref: '#/components/responses/SuccessfulOp'
externalDocs: externalDocs:
description: Find out more about Swagger description: Find out more about Swagger
url: 'http://swagger.io' url: 'http://swagger.io'
components: components:
responses:
SuccessfulOp:
description: Successful Operation
content:
application/json:
schema:
type: bool
requestBodies: requestBodies:
TestBody:
description: Test body
required: true
content:
text/plain:
schema:
type: string
format: byte
UserArray: UserArray:
content: content:
application/json: application/json:

View File

@ -3,6 +3,8 @@ README.md
api/openapi.yaml api/openapi.yaml
go.mod go.mod
go/api.go go/api.go
go/api_fake.go
go/api_fake_service.go
go/api_pet.go go/api_pet.go
go/api_pet_service.go go/api_pet_service.go
go/api_store.go go/api_store.go

View File

@ -893,8 +893,31 @@ paths:
summary: Get the pets by time summary: Get the pets by time
tags: tags:
- pet - pet
/fake/collection/test:
post:
operationId: fakePostTest
requestBody:
$ref: '#/components/requestBodies/TestBody'
responses:
"200":
content:
application/json:
schema:
type: bool
description: Successful Operation
summary: POST a test batch
tags:
- fake
components: components:
requestBodies: requestBodies:
TestBody:
content:
text/plain:
schema:
format: byte
type: string
description: Test body
required: true
UserArray: UserArray:
content: content:
application/json: application/json:
@ -914,6 +937,13 @@ components:
$ref: '#/components/schemas/Pet' $ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store description: Pet object that needs to be added to the store
required: true required: true
responses:
SuccessfulOp:
content:
application/json:
schema:
type: bool
description: Successful Operation
schemas: schemas:
OrderInfo: OrderInfo:
description: An order info for a pets from the pet store description: An order info for a pets from the pet store

View File

@ -19,6 +19,12 @@ import (
// FakeAPIRouter defines the required methods for binding the api requests to a responses for the FakeAPI
// The FakeAPIRouter implementation should parse necessary information from the http request,
// pass the data to a FakeAPIServicer to perform the required actions, then write the service results to the http response.
type FakeAPIRouter interface {
FakePostTest(http.ResponseWriter, *http.Request)
}
// PetAPIRouter defines the required methods for binding the api requests to a responses for the PetAPI // PetAPIRouter defines the required methods for binding the api requests to a responses for the PetAPI
// The PetAPIRouter implementation should parse necessary information from the http request, // The PetAPIRouter implementation should parse necessary information from the http request,
// pass the data to a PetAPIServicer to perform the required actions, then write the service results to the http response. // pass the data to a PetAPIServicer to perform the required actions, then write the service results to the http response.
@ -63,6 +69,15 @@ type UserAPIRouter interface {
} }
// FakeAPIServicer defines the api actions for the FakeAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type FakeAPIServicer interface {
FakePostTest(context.Context, string) (ImplResponse, error)
}
// PetAPIServicer defines the api actions for the PetAPI service // PetAPIServicer defines the api actions for the PetAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it, // This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file // while the service implementation can be ignored with the .openapi-generator-ignore file

View File

@ -0,0 +1,77 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* API version: 1.0.0
*/
package petstoreserver
import (
"encoding/json"
"net/http"
"strings"
)
// FakeAPIController binds http requests to an api service and writes the service results to the http response
type FakeAPIController struct {
service FakeAPIServicer
errorHandler ErrorHandler
}
// FakeAPIOption for how the controller is set up.
type FakeAPIOption func(*FakeAPIController)
// WithFakeAPIErrorHandler inject ErrorHandler into controller
func WithFakeAPIErrorHandler(h ErrorHandler) FakeAPIOption {
return func(c *FakeAPIController) {
c.errorHandler = h
}
}
// NewFakeAPIController creates a default api controller
func NewFakeAPIController(s FakeAPIServicer, opts ...FakeAPIOption) *FakeAPIController {
controller := &FakeAPIController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the FakeAPIController
func (c *FakeAPIController) Routes() Routes {
return Routes{
"FakePostTest": Route{
strings.ToUpper("Post"),
"/v2/fake/collection/test",
c.FakePostTest,
},
}
}
// FakePostTest - POST a test batch
func (c *FakeAPIController) FakePostTest(w http.ResponseWriter, r *http.Request) {
var bodyParam string
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&bodyParam); err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.FakePostTest(r.Context(), bodyParam)
// 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)
}

View File

@ -0,0 +1,39 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* API version: 1.0.0
*/
package petstoreserver
import (
"context"
"net/http"
"errors"
)
// FakeAPIService is a service that implements the logic for the FakeAPIServicer
// This service should implement the business logic for every endpoint for the FakeAPI API.
// Include any external packages or services that will be required by this service.
type FakeAPIService struct {
}
// NewFakeAPIService creates a default api service
func NewFakeAPIService() *FakeAPIService {
return &FakeAPIService{}
}
// FakePostTest - POST a test batch
func (s *FakeAPIService) FakePostTest(ctx context.Context, body string) (ImplResponse, error) {
// TODO - update FakePostTest with the required logic for this service method.
// Add api_fake_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, bool{}) or use other options such as http.Ok ...
// return Response(200, bool{}), nil
return Response(http.StatusNotImplemented, nil), errors.New("FakePostTest method not implemented")
}

View File

@ -20,6 +20,9 @@ import (
func main() { func main() {
log.Printf("Server started") log.Printf("Server started")
FakeAPIService := petstoreserver.NewFakeAPIService()
FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService)
PetAPIService := petstoreserver.NewPetAPIService() PetAPIService := petstoreserver.NewPetAPIService()
PetAPIController := petstoreserver.NewPetAPIController(PetAPIService) PetAPIController := petstoreserver.NewPetAPIController(PetAPIService)
@ -29,7 +32,7 @@ func main() {
UserAPIService := petstoreserver.NewUserAPIService() UserAPIService := petstoreserver.NewUserAPIService()
UserAPIController := petstoreserver.NewUserAPIController(UserAPIService) UserAPIController := petstoreserver.NewUserAPIController(UserAPIService)
router := petstoreserver.NewRouter(PetAPIController, StoreAPIController, UserAPIController) router := petstoreserver.NewRouter(FakeAPIController, PetAPIController, StoreAPIController, UserAPIController)
log.Fatal(http.ListenAndServe(":8080", router)) log.Fatal(http.ListenAndServe(":8080", router))
} }

View File

@ -3,6 +3,8 @@ README.md
api/openapi.yaml api/openapi.yaml
go.mod go.mod
go/api.go go/api.go
go/api_fake.go
go/api_fake_service.go
go/api_pet.go go/api_pet.go
go/api_pet_service.go go/api_pet_service.go
go/api_store.go go/api_store.go

View File

@ -893,8 +893,31 @@ paths:
summary: Get the pets by time summary: Get the pets by time
tags: tags:
- pet - pet
/fake/collection/test:
post:
operationId: fakePostTest
requestBody:
$ref: '#/components/requestBodies/TestBody'
responses:
"200":
content:
application/json:
schema:
type: bool
description: Successful Operation
summary: POST a test batch
tags:
- fake
components: components:
requestBodies: requestBodies:
TestBody:
content:
text/plain:
schema:
format: byte
type: string
description: Test body
required: true
UserArray: UserArray:
content: content:
application/json: application/json:
@ -914,6 +937,13 @@ components:
$ref: '#/components/schemas/Pet' $ref: '#/components/schemas/Pet'
description: Pet object that needs to be added to the store description: Pet object that needs to be added to the store
required: true required: true
responses:
SuccessfulOp:
content:
application/json:
schema:
type: bool
description: Successful Operation
schemas: schemas:
OrderInfo: OrderInfo:
description: An order info for a pets from the pet store description: An order info for a pets from the pet store

View File

@ -19,6 +19,12 @@ import (
// FakeAPIRouter defines the required methods for binding the api requests to a responses for the FakeAPI
// The FakeAPIRouter implementation should parse necessary information from the http request,
// pass the data to a FakeAPIServicer to perform the required actions, then write the service results to the http response.
type FakeAPIRouter interface {
FakePostTest(http.ResponseWriter, *http.Request)
}
// PetAPIRouter defines the required methods for binding the api requests to a responses for the PetAPI // PetAPIRouter defines the required methods for binding the api requests to a responses for the PetAPI
// The PetAPIRouter implementation should parse necessary information from the http request, // The PetAPIRouter implementation should parse necessary information from the http request,
// pass the data to a PetAPIServicer to perform the required actions, then write the service results to the http response. // pass the data to a PetAPIServicer to perform the required actions, then write the service results to the http response.
@ -63,6 +69,15 @@ type UserAPIRouter interface {
} }
// FakeAPIServicer defines the api actions for the FakeAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type FakeAPIServicer interface {
FakePostTest(context.Context, string) (ImplResponse, error)
}
// PetAPIServicer defines the api actions for the PetAPI service // PetAPIServicer defines the api actions for the PetAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it, // This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file // while the service implementation can be ignored with the .openapi-generator-ignore file

View File

@ -0,0 +1,77 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* API version: 1.0.0
*/
package petstoreserver
import (
"encoding/json"
"net/http"
"strings"
)
// FakeAPIController binds http requests to an api service and writes the service results to the http response
type FakeAPIController struct {
service FakeAPIServicer
errorHandler ErrorHandler
}
// FakeAPIOption for how the controller is set up.
type FakeAPIOption func(*FakeAPIController)
// WithFakeAPIErrorHandler inject ErrorHandler into controller
func WithFakeAPIErrorHandler(h ErrorHandler) FakeAPIOption {
return func(c *FakeAPIController) {
c.errorHandler = h
}
}
// NewFakeAPIController creates a default api controller
func NewFakeAPIController(s FakeAPIServicer, opts ...FakeAPIOption) *FakeAPIController {
controller := &FakeAPIController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the FakeAPIController
func (c *FakeAPIController) Routes() Routes {
return Routes{
"FakePostTest": Route{
strings.ToUpper("Post"),
"/v2/fake/collection/test",
c.FakePostTest,
},
}
}
// FakePostTest - POST a test batch
func (c *FakeAPIController) FakePostTest(w http.ResponseWriter, r *http.Request) {
var bodyParam string
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&bodyParam); err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.FakePostTest(r.Context(), bodyParam)
// 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)
}

View File

@ -0,0 +1,39 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* API version: 1.0.0
*/
package petstoreserver
import (
"context"
"net/http"
"errors"
)
// FakeAPIService is a service that implements the logic for the FakeAPIServicer
// This service should implement the business logic for every endpoint for the FakeAPI API.
// Include any external packages or services that will be required by this service.
type FakeAPIService struct {
}
// NewFakeAPIService creates a default api service
func NewFakeAPIService() *FakeAPIService {
return &FakeAPIService{}
}
// FakePostTest - POST a test batch
func (s *FakeAPIService) FakePostTest(ctx context.Context, body string) (ImplResponse, error) {
// TODO - update FakePostTest with the required logic for this service method.
// Add api_fake_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, bool{}) or use other options such as http.Ok ...
// return Response(200, bool{}), nil
return Response(http.StatusNotImplemented, nil), errors.New("FakePostTest method not implemented")
}

View File

@ -20,6 +20,9 @@ import (
func main() { func main() {
log.Printf("Server started") log.Printf("Server started")
FakeAPIService := petstoreserver.NewFakeAPIService()
FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService)
PetAPIService := petstoreserver.NewPetAPIService() PetAPIService := petstoreserver.NewPetAPIService()
PetAPIController := petstoreserver.NewPetAPIController(PetAPIService) PetAPIController := petstoreserver.NewPetAPIController(PetAPIService)
@ -29,7 +32,7 @@ func main() {
UserAPIService := petstoreserver.NewUserAPIService() UserAPIService := petstoreserver.NewUserAPIService()
UserAPIController := petstoreserver.NewUserAPIController(UserAPIService) UserAPIController := petstoreserver.NewUserAPIController(UserAPIService)
router := petstoreserver.NewRouter(PetAPIController, StoreAPIController, UserAPIController) router := petstoreserver.NewRouter(FakeAPIController, PetAPIController, StoreAPIController, UserAPIController)
log.Fatal(http.ListenAndServe(":8080", router)) log.Fatal(http.ListenAndServe(":8080", router))
} }