Open alexandrkamenev opened 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" }
json:"result,omitempty" required:"true" enum:"ok" example:"ok"
json:"info,omitempty" required:"true" enum:"exist" example:"exist"
json:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408"
json:"count,omitempty" required:"true" minimum:"0" maximum:"3" example:"1"
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" }
json:"info,omitempty" required:"true" enum:"no exist" example:"no exist"
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" }
json:"info,omitempty" required:"true" enum:"archive" example:"archive"
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?
Very curious about this as well.
Example in this discussion https://github.com/swaggest/rest/discussions/136
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 stringjson:"info,omitempty" required:"true" enum:"exist" example:"exist"
Phonenumber stringjson:"phonenumber,omitempty" required:"true" minLength:"11" maxLength:"11" example:"79146764408"
Сount intjson:"count,omitempty" required:"true" minimum:"0" maximum:"3" example:"1"
Activated stringjson:"activated,omitempty" required:"true" example:"27.11.2022"
}type ResponsePhoneNoExist struct { Result string
json:"result,omitempty" required:"true" enum:"ok" example:"ok"
Info stringjson:"info,omitempty" required:"true" enum:"no exist" example:"no exist"
Phonenumber stringjson:"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 stringjson:"info,omitempty" required:"true" enum:"archive" example:"archive"
Phonenumber stringjson:"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?