Skip to content

Conversation

@zJeremiah
Copy link
Contributor

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.

…g to allow adding json string examples with a specific name
Comment on lines 182 to 187
// 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)
}

Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

@zJeremiah zJeremiah Jul 15, 2025

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.

Comment on lines 191 to 208
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)
}
Copy link
Member

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 
} 

paths_test.go Outdated
},
Examples: map[string]Example{
"2c69c864087c4000": {
"Example 1": {
Copy link
Member

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

@jbsmith7741 jbsmith7741 merged commit 04181a7 into hydronica:main Jul 15, 2025
2 checks passed
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 this pull request may close these issues.

2 participants