Skip to content
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

support example tag on slice field. #49

Closed
laushunyu opened this issue Aug 25, 2022 · 4 comments · Fixed by #50
Closed

support example tag on slice field. #49

laushunyu opened this issue Aug 25, 2022 · 4 comments · Fixed by #50

Comments

@laushunyu
Copy link

laushunyu commented Aug 25, 2022

code:

type WantExample struct {
	A string   `json:"a" example:"example of a"`
	B []string `json:"b" example:"example of b‘s item"`
}

func main() {
	reflector := jsonschema.Reflector{}
	schema, err := reflector.Reflect(WantExample{})
	if err != nil {
		log.Fatal(err)
	}

	j, err := json.MarshalIndent(schema, "", " ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(j))
}

output: ( there is no example on field "b")

{
 "properties": {
  "a": {
   "examples": [
    "example of a"
   ],
   "type": "string"
  },
  "b": {
   "items": {
    "type": "string"
   },
   "type": [
    "array",
    "null"
   ]
  }
 },
 "type": "object"
}

wanted:

{
 "properties": {
  "a": {
   "examples": [
    "example of a"
   ],
   "type": "string"
  },
  "b": {
   "items": {
    "type": "string",
    "example": "example of b's item"
   },
   "type": [
    "array",
    "null"
   ]
  }
 },
 "type": "object"
}
@vearutop
Copy link
Member

Interesting, looks like a bug, let me check in details.

@vearutop
Copy link
Member

But also regarding your expectations, example is applied to the property, so in this case it won't be

  "b": {
   "items": {
    "type": "string",
    "example": "example of b's item"
   },
   "type": [
    "array",
    "null"
   ]
  }

but rather something like

  "b": {
  "examples": ["example of b"]
  "items": {
   "type": "string",
  },
  "type": [
   "array",
   "null"
  ]
 }

@laushunyu
Copy link
Author

better, looking forward to a new tag. thx

@vearutop
Copy link
Member

Please check test for an example of example. 🙃

https://github.com/swaggest/jsonschema-go/blob/v0.3.38/reflect_test.go#L1279

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants