-
Notifications
You must be signed in to change notification settings - Fork 2
Added named json string function #5
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
Conversation
…g to allow adding json string examples with a specific name
| // name is auto generated based on the example count | ||
| func (r Response) WithExample(i any) Response { | ||
| return r.WithNamedExample("", i) | ||
| exampleCount := len(r.Content[Json].Examples) + 1 | ||
| return r.WithNamedExample("Example "+strconv.Itoa(exampleCount), i) | ||
| } | ||
|
|
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.
I don't think we want this behavior as the unnamed examples are used to group like services together. 🤔 But this text is so much more readable than the string hash
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.
This should be ok, The current logic is to use the schema.Title for any unnamed examples. See paths.go:210 It would be better to make this change in that section of code rather than this spot.
see paths.go:219 -> 221
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.
I'll move the logic into the AddExample, it will be much easer to read if it's not some big string hash. The Schema name is sometimes ok, if we use an actual go struct. But when it's just a json string the schema becomes that hash.
| func (r Response) WithNamedJsonString(name string, s string) Response { | ||
| var m any | ||
| if s[0] == '[' && s[len(s)-1] == ']' { | ||
| m = make([]any, 0) | ||
| } else { | ||
| m = make(map[string]any) | ||
| } | ||
| err := json.Unmarshal([]byte(s), &m) | ||
| if err != nil { | ||
| // return a response with the error message | ||
| return Response{ | ||
| Status: r.Status, | ||
| Desc: err.Error(), | ||
| Content: Content{"invalid/json": {Examples: map[string]Example{"invalid": {Value: s}}}}, | ||
| } | ||
| } | ||
| return r.WithNamedExample(name, m) | ||
| } |
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.
You may want to add this to the RequestBody as well, there is a bit of duplicate logic in the response and request options.
Also be good to consolidate the code with
func (r Response) WithJsonString(s string) Response {
return r.WithbNamedJsonString("", s)
}
func (r Response) WithNamedJsonString(name string, s string) Response {
// AS listed
} …uple other small issues
paths_test.go
Outdated
| }, | ||
| Examples: map[string]Example{ | ||
| "2c69c864087c4000": { | ||
| "Example 1": { |
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.
We may want to add a couple more examples, some Named and some unnamed. Do we want
- Example 1
- Named
- Example 3
Added named json string function to add specific example names with a json string
fixed the add example without a name to generate a "example #" based on the current number of examples.