swagger-api / swagger-api/swagger-codegen-generators
Generate example POJO from `APIModelProperty` annotations
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
I previously asked this question on stackoverflow.
I am writing automated end-to-end tests for an API that makes use of Swagger. I would like to reuse the model classes (and the example property values in particular) in my tests as payload of some HTTP requests to our API. Reusing the annotated model classes allows me to test whether the examples are still up to date and to give a head start to developers writing new tests. However, I found it is unnecessarily complicated to write a generic method to create a model object filled with the example property values from the @APIModelProperty annotations.
This seems like a fairly common use case to me. Therefore, I am considering to contribute to swagger to enable this functionality. Am I welcome to submit a PR for this problem? My suggested approach is as follows:
- Write a separate generic public method in
io.swagger.codegen.examples.ExampleGeneratorsimilar toresolveModelToExample, which returns a POJO that will generate the example JSON when serialized - Refactor duplicated code between the new method and
resolveModelToExample - Write some tests to check edge cases of the new method (such as empty example object, empty properties, nested model objects)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in io.swagger.codegen.examples.ExampleGenerator by reading resolveModelToExample and tracing how annotated model properties become example values. Add the proposed POJO-generation entry point, refactor shared logic, and cover empty examples, empty properties, and nested model objects. Done means the returned POJO serializes to the expected example JSON and the edge-case tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100