mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 20:50:55 +00:00
[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:
parent
dcf472a034
commit
4c73faf737
@ -849,11 +849,37 @@ paths:
|
||||
application/json:
|
||||
schema:
|
||||
$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:
|
||||
description: Find out more about Swagger
|
||||
url: 'http://swagger.io'
|
||||
components:
|
||||
responses:
|
||||
SuccessfulOp:
|
||||
description: Successful Operation
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: bool
|
||||
requestBodies:
|
||||
TestBody:
|
||||
description: Test body
|
||||
required: true
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
format: byte
|
||||
UserArray:
|
||||
content:
|
||||
application/json:
|
||||
|
@ -3,6 +3,8 @@ README.md
|
||||
api/openapi.yaml
|
||||
go.mod
|
||||
go/api.go
|
||||
go/api_fake.go
|
||||
go/api_fake_service.go
|
||||
go/api_pet.go
|
||||
go/api_pet_service.go
|
||||
go/api_store.go
|
||||
|
@ -893,8 +893,31 @@ paths:
|
||||
summary: Get the pets by time
|
||||
tags:
|
||||
- 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:
|
||||
requestBodies:
|
||||
TestBody:
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
format: byte
|
||||
type: string
|
||||
description: Test body
|
||||
required: true
|
||||
UserArray:
|
||||
content:
|
||||
application/json:
|
||||
@ -914,6 +937,13 @@ components:
|
||||
$ref: '#/components/schemas/Pet'
|
||||
description: Pet object that needs to be added to the store
|
||||
required: true
|
||||
responses:
|
||||
SuccessfulOp:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: bool
|
||||
description: Successful Operation
|
||||
schemas:
|
||||
OrderInfo:
|
||||
description: An order info for a pets from the pet store
|
||||
|
@ -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
|
||||
// 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.
|
||||
@ -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
|
||||
// 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
|
||||
|
77
samples/server/petstore/go-api-server/go/api_fake.go
Normal file
77
samples/server/petstore/go-api-server/go/api_fake.go
Normal 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)
|
||||
}
|
39
samples/server/petstore/go-api-server/go/api_fake_service.go
Normal file
39
samples/server/petstore/go-api-server/go/api_fake_service.go
Normal 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")
|
||||
}
|
@ -20,6 +20,9 @@ import (
|
||||
func main() {
|
||||
log.Printf("Server started")
|
||||
|
||||
FakeAPIService := petstoreserver.NewFakeAPIService()
|
||||
FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService)
|
||||
|
||||
PetAPIService := petstoreserver.NewPetAPIService()
|
||||
PetAPIController := petstoreserver.NewPetAPIController(PetAPIService)
|
||||
|
||||
@ -29,7 +32,7 @@ func main() {
|
||||
UserAPIService := petstoreserver.NewUserAPIService()
|
||||
UserAPIController := petstoreserver.NewUserAPIController(UserAPIService)
|
||||
|
||||
router := petstoreserver.NewRouter(PetAPIController, StoreAPIController, UserAPIController)
|
||||
router := petstoreserver.NewRouter(FakeAPIController, PetAPIController, StoreAPIController, UserAPIController)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ README.md
|
||||
api/openapi.yaml
|
||||
go.mod
|
||||
go/api.go
|
||||
go/api_fake.go
|
||||
go/api_fake_service.go
|
||||
go/api_pet.go
|
||||
go/api_pet_service.go
|
||||
go/api_store.go
|
||||
|
@ -893,8 +893,31 @@ paths:
|
||||
summary: Get the pets by time
|
||||
tags:
|
||||
- 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:
|
||||
requestBodies:
|
||||
TestBody:
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
format: byte
|
||||
type: string
|
||||
description: Test body
|
||||
required: true
|
||||
UserArray:
|
||||
content:
|
||||
application/json:
|
||||
@ -914,6 +937,13 @@ components:
|
||||
$ref: '#/components/schemas/Pet'
|
||||
description: Pet object that needs to be added to the store
|
||||
required: true
|
||||
responses:
|
||||
SuccessfulOp:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: bool
|
||||
description: Successful Operation
|
||||
schemas:
|
||||
OrderInfo:
|
||||
description: An order info for a pets from the pet store
|
||||
|
@ -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
|
||||
// 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.
|
||||
@ -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
|
||||
// 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
|
||||
|
77
samples/server/petstore/go-chi-server/go/api_fake.go
Normal file
77
samples/server/petstore/go-chi-server/go/api_fake.go
Normal 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)
|
||||
}
|
39
samples/server/petstore/go-chi-server/go/api_fake_service.go
Normal file
39
samples/server/petstore/go-chi-server/go/api_fake_service.go
Normal 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")
|
||||
}
|
@ -20,6 +20,9 @@ import (
|
||||
func main() {
|
||||
log.Printf("Server started")
|
||||
|
||||
FakeAPIService := petstoreserver.NewFakeAPIService()
|
||||
FakeAPIController := petstoreserver.NewFakeAPIController(FakeAPIService)
|
||||
|
||||
PetAPIService := petstoreserver.NewPetAPIService()
|
||||
PetAPIController := petstoreserver.NewPetAPIController(PetAPIService)
|
||||
|
||||
@ -29,7 +32,7 @@ func main() {
|
||||
UserAPIService := petstoreserver.NewUserAPIService()
|
||||
UserAPIController := petstoreserver.NewUserAPIController(UserAPIService)
|
||||
|
||||
router := petstoreserver.NewRouter(PetAPIController, StoreAPIController, UserAPIController)
|
||||
router := petstoreserver.NewRouter(FakeAPIController, PetAPIController, StoreAPIController, UserAPIController)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user