go-restful-openapi icon indicating copy to clipboard operation
go-restful-openapi copied to clipboard

[question] Is there a way to return user defined schema type as "file"

Open aryanicosa opened this issue 3 years ago • 5 comments

currently Returns it take code, message interface as parameters. e.g: Returns(http.StatusOK, http.StatusText(http.StatusOK), "file")

it will produce apispec like this: "responses": { "200": { "description": "OK", "schema": { "type": "string", } }

However, I am trying to fine a way to create api spec like below, because in reality the handler is returning a file to user "responses": { "200": { "description": "OK", "schema": { "type": "file", } }

aryanicosa avatar Nov 03 '22 10:11 aryanicosa

I see in this commit https://github.com/emicklei/go-restful-openapi/commit/480c8f365ca44dad15868767c566c790658333ad made change to be able to handle time.Duration type. But, however, file is not a type. is there a better way to handle this case

similar issue in go-swaggo https://github.com/go-swagger/go-swagger/issues/1003

aryanicosa avatar Nov 07 '22 06:11 aryanicosa

thank for reporting this issue. We could introduce a new type:

type SchemaType string

that is check in the Returns to allow a raw type value so that:

Returns(http.StatusOK, http.StatusText(http.StatusOK), SchemaType("file"))

will produce your result.

emicklei avatar Nov 21 '22 20:11 emicklei

@aryanicosa would that work for you?

emicklei avatar Nov 30 '22 21:11 emicklei

I got your point and able to imagine the result, but I am sorry because confuse how to implement it

I realize that schema type is added here https://github.com/emicklei/go-restful-openapi/blob/v2/build_path.go#L287 but, I am not sure where to introduce type SchemaType string and create the checker to return raw type @emicklei

I am still learn and try to figure it out

aryanicosa avatar Dec 06 '22 18:12 aryanicosa

you are on the right track, i think. From the top of my head, we could have a check after line 261.

if st, ok := e.Model.(SchemaType); ok {
    r.Schema.AddType(modelName, string(st))
}

emicklei avatar Dec 07 '22 18:12 emicklei