-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly parse format on array fields #973
Conversation
When the format directive is used on an array type the indicated format should go under the items key.
Sorry, I haven't got your point. What on earth are you discussing, |
I'm discussing package main
import "net/http"
// @host example.com
// @BasePath /v2
func main() {
}
type Response struct {
Values []string `json:"values" type:"array,string" format:"uuid"`
}
// @Produce json
// @Success 200 {object} Response "ok"
// @Router /thing [get]
func Get(w http.ResponseWriter, r *http.Request) error {
return nil
} This generates the following YAML. swagger: "2.0"
host: example.com
basePath: /v2
info:
contact: {}
description: Example
title: Example
version: 0.1.0
paths:
/thing:
get:
produces:
- application/json
responses:
"200":
description: ok
schema:
$ref: '#/definitions/main.Response'
definitions:
main.Response:
properties:
values:
format: uuid
items:
type: string
type: array
type: object That works in the Swagger editor, but the The example value it shows is this: {
"values": [
"string"
]
} If the YAML is modified so that the swagger: "2.0"
host: example.com
basePath: /v2
info:
contact: {}
description: Example
title: Example
version: 0.1.0
paths:
/thing:
get:
produces:
- application/json
responses:
"200":
description: ok
schema:
$ref: '#/definitions/main.Response'
definitions:
main.Response:
properties:
values:
items:
type: string
format: uuid
type: array
type: object The example given is instead: {
"values": [
"3fa85f64-5717-4562-b3fc-2c963f66afa6"
]
} The model is also correctly given as This also makes a difference when generating e.g. models from the spec. For the first YAML go-swagger 0.27.0 will generate this code: // MainResponse main response
//
// swagger:model main.Response
type MainResponse struct {
// values
Values []string `json:"values"`
} Where the second YAML would give you: // MainResponse main response
//
// swagger:model main.Response
type MainResponse struct {
// values
Values []strfmt.UUID `json:"values"`
} Hope this made the point of the PR a bit clearer. |
I can't find any examples where I've only found this snippet from the Swagger docs:
I've interpreted that as when If you'd prefer keeping both, I think |
Sorry, maybe you're right. https://swagger.io/specification/v2/#operationObject
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Describe the PR
The format tag used on an array type should place the format in the items object, rather than directly on the type.
Relation issue
Additional context
Examples of this are available on the Swagger website, under Array Examples here.