Skip to content

How do I specify three different types of responses #17

@alexandrkamenev

Description

@alexandrkamenev

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions