swaggest / jsonschema-go

JSON Schema mapping for Go
https://pkg.go.dev/github.com/swaggest/jsonschema-go
MIT License
102 stars 13 forks source link

default tag does not work for arrays #70

Closed tpoxa closed 1 year ago

tpoxa commented 1 year ago

Describe the bug Default tag does not work for array fields of a struct

To Reproduce

type Recipient struct {
    Routes []string `json:"routes,omitempty" title:"Routes" default:"A,B"`
}

func main() {
    r := jsonschema.Reflector{}
    sh, _ := r.Reflect(Recipient{})

    j, _ := json.MarshalIndent(sh, "", " ")
    fmt.Println(string(j))
}

Expected behavior

{
 "properties": {
  "routes": {
   "title": "Routes",
   "items": {
    "type": "string"
   },
   "type": "array",
   "default":["A","B"]  <-- Default value
  }
 },
 "type": "object"
}

Actual behaviour

{
 "properties": {
  "routes": {
   "title": "Routes",
   "items": {
    "type": "string"
   },
   "type": "array"
  }
 },
 "type": "object"
}

Additional context I checked with jsonschemavalidator.net that schema where default is set for an array is valid for Draft-7 I guess its quite a challenge to do for an object, but at-least basic type support would be great.