swagger-api / swagger-api/swagger-codegen-generators

Generate example POJO from `APIModelProperty` annotations

Open
#102 0 comments 0 reactions 0 assignees View on GitHub

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.ExampleGenerator similar to resolveModelToExample, 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.