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

Best method for including example of defined input? #296

Open
badloop opened this issue Feb 3, 2021 · 3 comments
Open

Best method for including example of defined input? #296

badloop opened this issue Feb 3, 2021 · 3 comments

Comments

@badloop
Copy link

badloop commented Feb 3, 2021

I am attempting to allow a user-defined struct to define what the input schema for an API would be. I've found the NewSchemaRefForValue function that does successfully generate the properties block for a given struct, but there doesn't appear to be a way to provide example input using that function. Is there a method for achieving this currently?

@fenollp
Copy link
Collaborator

fenollp commented Feb 4, 2021

@badloop
Copy link
Author

badloop commented Feb 4, 2021

@fenollp thats the code I'm using... openapi3gen.NewSchemaRefForValue seems to do a fine job of generating the Schema object, but there is no way of generating that Schema object with included examples. The examples would need to be added to the object manually after the Schema was created.

So the following works for making the schema object:

type MyStruct struct {
	MyField string `json:"myfield"`
}
s, _, _ := openapi3gen.NewSchemaRefForValue(&MyStruct{})

But I would then need to manipulate that object to add examples for each field. Obviously for this example that is trivial, but for a more complex struct it would quickly become very cumbersome. Ideally I would be able to do something like the following:

type MyStruct struct {
	MyField string `json:"myfield" oapi-example:"this string should look like this"`
}
s, _, _ := openapi3gen.NewSchemaRefForValue(&MyStruct{})

And it would populate the example automatically. I've dug into the code a bit and the fix to allow something like that would be to add fields to the jsoninfo struct for processing. Not sure if this is a direction that the project wants to go though.

Thoughts?

@fenollp
Copy link
Collaborator

fenollp commented Feb 15, 2021

What is stopping you from setting s.Example?

Example interface{} `json:"example,omitempty" yaml:"example,omitempty"`

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

No branches or pull requests

2 participants