swaggest / usecase

Clean Architecture Use Case for Go
https://pkg.go.dev/github.com/swaggest/usecase
MIT License
33 stars 2 forks source link

How do I specify three different types of responses #17

Open alexandrkamenev opened 1 year ago

alexandrkamenev commented 1 year ago

Good afternoon. Tell me how to properly issue a usecase.NewInteractor so that output can be of different types? For example, my OpenAPI 3.0 schema describes three different types of responses.

... "/phonenumber/get/" : { "post" : { "operationId" : "getInfo", "requestBody" : { "required" : true, "content" : { "application/json" : { "schema" : { "$ref" : "#/components/schemas/RequestInfo" } } } }, "responses" : { "200" : { "content" : { "application/json" : { "schema" : { "oneOf" : [ { "$ref" : "#/components/schemas/ResponsePhoneExist" }, { "$ref" : "#/components/schemas/ResponsePhoneNoExist" }, { "$ref" : "#/components/schemas/ResponsePhoneInArchive" } ] } } } }, ...

ResponsePhoneExist, ResponsePhoneNoExist and ResponsePhoneInArchive are different types in my go app:

type ResponsePhoneExist struct { Result string json:"result,omitempty" required:"true" enum:"ok" example:"ok" Info string json:"info,omitempty" required:"true" enum:"exist" example:"exist" Phonenumber string json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408" Сount int json:"count,omitempty" required:"true" minimum:"0" maximum:"3" example:"1" Activated string json:"activated,omitempty" required:"true" example:"27.11.2022" }

type ResponsePhoneNoExist struct { Result string json:"result,omitempty" required:"true" enum:"ok" example:"ok" Info string json:"info,omitempty" required:"true" enum:"no exist" example:"no exist" Phonenumber string json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408" }

type ResponsePhoneInArchive struct { Result string json:"result,omitempty" required:"true" enum:"ok" example:"ok" Info string json:"info,omitempty" required:"true" enum:"archive" example:"archive" Phonenumber string json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408" }

Then I define web service in function main():

func main() { service := web.DefaultService() ... service.Docs("/docs", v4emb.New) service.Post("/phonenumber/get", getPhoneInfo()) err := http.ListenAndServe(":3400", service) ... }

func getPhoneInfo() usecase.Interactor { u := usecase.NewInteractor(func(ctx context.Context, request RequestInfo, response ???) error { *response, err = getInfoIn1C(request.PhoneNumber) if err != nil { return status.Wrap(errors.New(err.Error()), status.Internal) } return nil }) u.SetName("getInfo") u.SetExpectedErrors(status.Unauthenticated, status.InvalidArgument, status.Internal) return u }

func getInfoIn1C(phone string) (???, error) { var out ??? var err error ... return out, err }

What type instead "???" should I specify that the automatically generated Open API 3.0 documentation satisfies the fragment given above?

paul-england commented 1 year ago

Very curious about this as well.

paul-england commented 1 year ago

Example in this discussion https://github.com/swaggest/rest/discussions/136