OpenAPITools / OpenAPITools/openapi-generator

[REQ] Generate data model with default value mapping from openapi.examples

Open
#22,334 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Hi team, currently one of the key feature that I find extremely powerful is of course the data model generation, which is extremely helpful and speed up development process. However, part of the process is testing, and reusing that data model to do testing is a norm, and data mapping can be tedious work. I'd like to speed up that testing process.

Describe the solution you'd like

Since OpenAPI have a examples field available, my solution to speed up the process is to leverage that as a part of the data model generation. Of course, this can be optional under new parameter like --include-examples to prevent too much unwanted generation. The implementation that I had in mind would start from reading those examples under object reference and create additional static method/extensions to be matching with existing examples.

For instance

// static method approach
var exampleResponse = DataModel.PrepareDefaultExamples();
var exampleResponse = DataModel.PrepareScenarioAExamples();

Describe alternatives you've considered

N/A

Additional context

I've been taking a look on how mock server can be generated from OpenAPI specs like wiremock, it also leverage the examples from openapi specs as well. Although, implementing this feature in the generator tools can have much more impact and supporting testing more multiple layer like unit test or integration test, and hopefully resulting in a better maintain documentation.

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

The issue names no implementation files, tests, or entry points. Start by locating the data-model generation path and existing option handling in openapi-generator, then trace how OpenAPI examples are parsed. Done should include a defined opt-in behavior for supported examples and generated data-model helpers, with tests covering the selected example cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.