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

Allow empty model objects in response example definitions #40

Open
ncreep opened this issue May 11, 2017 · 7 comments
Open

Allow empty model objects in response example definitions #40

ncreep opened this issue May 11, 2017 · 7 comments

Comments

@ncreep
Copy link
Contributor

ncreep commented May 11, 2017

Hi,

I'm trying to render an example value with the Swagger renderer. My example value contains an empty list and thus JsonToJsonSchema throws:

new IllegalSchemaException("Cannot use an empty list to generate a schema!")

In my case, it would still be useful to actually render the example value, despite not knowing the exact type of the array (I don't care if it's considered to be Object or something).

Could it be possible to not throw the exception and have some fallback rendering of the example value without a schema?

Thanks

@daviddenton
Copy link
Owner

Is there any reason that the example list has to be empty - for instance is it the case that the type of object that goes into the list is dynamic?

@ncreep
Copy link
Contributor Author

ncreep commented May 12, 2017

In the specific use-case I have it's complicated to initialize a valid value (there are a number of nested collection values there, and it just adds noise in the code).
And generally I would like to lower the barrier for creating the example values, so that people not lose their motivation to provide them.

@daviddenton
Copy link
Owner

I took a quick look at defaulting the value instead of throwing the exception but that broke the model definition rendering (it lost the subsequent definitions), so I think this will require a larger change - possibly reworking the renderer algorithm.if you can spot an easy fix then please suggest :)

@ncreep
Copy link
Contributor Author

ncreep commented May 14, 2017

I'll try looking into it when tackling #41, though since I'm new to the library, I'm not sure what I'll come up with...

@daviddenton
Copy link
Owner

@ncreep One more thing that might help with regards to the "generating valid examples" problem - for our internal apps using fintrospect, we use Databob to generate valid random values for objects with no boilerplate. It works out of the box on everything we've tried, and you can provide generators for any other type that you might need: https://github.com/daviddenton/databob

@ncreep
Copy link
Contributor Author

ncreep commented May 14, 2017

Thanks for the suggestion. Although I'm definitely in favor of generating randomized data (be it for tests or for the schema examples), sadly, our codebase is not there yet, it'll take non-trivial effort to get there. And some of the types that we are using are quite messy (as in, deeply nested multiple implementations of a unsealed trait), the problematic one that triggered this issue being one of them.
Also, taking a closer look at Databob, I see that it uses reflection behind the scenes. I'm slightly hesitant to introduce libraries that use reflection, and instinctively lean towards macro/Shapeless-based libraries. Though this point will have to be reevaluated once I actually go forward with the data-generation approach.

@daviddenton daviddenton changed the title Failing schema fail Swagger rendering Allow empty model objects in response example definitions May 18, 2017
@alexpuchau-lendable
Copy link

I've found myself getting the same error. In my case, it's a recursive definition (a job that can have child jobs, that can have child jobs, etc...). So the list needs to be empty at some point. Is that not something that the JsonToJsonSchema could handle?

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

No branches or pull requests

3 participants