swaggest / rest

Web services with OpenAPI and JSON Schema done quick in Go
https://pkg.go.dev/github.com/swaggest/rest
MIT License
362 stars 17 forks source link

Nullable on lists #120

Closed pboguslawski closed 1 year ago

pboguslawski commented 1 year ago

Describe the bug When declaring struct with list i.e.

    Strings []string `json:"strings"`
    Ints    []int    `json:"ints"`

final openapi.json has nullable: true on these fields.

If it's intentional: how to declare list field that must exist in json and when without items must be like "strings": []?

vearutop commented 1 year ago

This is intentional on the grounds that null (nil) is a valid value to receive in map or slice, it is loosened (no nullable) for omitempty for reason that encoding won't expose null.

If you want custom behavior, you can define it with jsonschema.InterceptNullability hook on (*web.Service).OpenAPICollector.Reflector().DefaultOptions.

Please see https://github.com/swaggest/jsonschema-go/pull/59#issuecomment-1321785609 for details.

pboguslawski commented 1 year ago

Works fine thank you!