swagger-api / swagger-api/swagger-codegen

Allow language generators to supply a custom ExampleGenerator via CodegenModelFactory

Open
#5,522 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

DefaultCodegen hard codes instantiation of ExampleGenerator which currently generates examples which differ from Swagger UI (i.e. uses "aeiou" for strings instead of "string", etc.)

Swagger-codegen version

2.2.3

Swagger declaration file content or url

http://petstore.swagger.io/v2/swagger.json

Command line used for generation

java -jar swagger-codegen-cli.jar generate -l html -i http://petstore.swagger.io/v2/swagger.json -o /tmp/swagger-codegen

Steps to reproduce

Using http://petstore.swagger.io/v2/swagger.json
generated examples look like:

[ {
  "photoUrls" : [ "aeiou" ],
  "name" : "doggie",
  "id" : 9,
  "category" : {
    "name" : "aeiou",
    "id" : 5
  },
  "tags" : [ {
    "name" : "aeiou",
    "id" : 9
  } ],
  "status" : "available"
} ]
Related issues
Suggest a Fix

In CodegenModelType, add EXAMPLE(ExampleGenerator.class);
and either an abstract interface for it, or let that be a base class.

In DefaultCodegen and others, use CodegenModelFactory.newInstance(EXAMPLE)
rather than hard-code the constructor call.

I also suggest refactoring some of the code in ExampleGenerator so the values (as strings)
can be reused in XmlExampleGenerator

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 by reading CodegenModelFactory, CodegenModelType, DefaultCodegen, and ExampleGenerator to trace where the example generator is instantiated. Run the documented Java CLI generation command against the petstore specification and compare the generated examples with the expected values. Done means language generators can supply a custom ExampleGenerator without hard-coded construction, while preserving reuse with XmlExampleGenerator.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.