swagger-api / swagger-api/swagger-codegen
Allow the user to specify a preferred Media Type
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
This is a feature request to allow the user to specify their preferred Media Type when generating a client from a Swagger spec. Currently, if the Swagger spec defines multiple media types for consumes or produces in a given path, the user has no way to specify which type should be used at runtime. For example, if the spec states that a path supports both application/xml and application/json, there's no way for the user to indicate which type they want to use.
In theory, it shouldn't matter which type is used, since the service supports both. In practice, many companies have corporate standards that require a specific media type to be used.
My proposal is to add a new config option preferredMediaType. For each path in the Swagger spec, if the path supports the preferred type, then that type will be used. If the path does not support the preferred type, then one of the supported types will be used by following the logic as currently implemented.
Swagger-codegen version
We're using 2.4.8, but this feature could also be applied to 3.0.11
Swagger declaration file content or url
https://gist.github.com/packleader/30f9f0b6602ab749af1998787c6f72c5
Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i swagger.yaml \
-l java \
-o temp/client/petstore/java
Steps to reproduce
The resulting code will always use application/json. Although DefaultApi has both application/json and application/xml, ApiClient will always use application/json due to the logic in selectHeaderAccept and selectHeaderContentType.
Related issues/PRs
Suggest a fix/enhancement
I'll volunteer to implement this feature, and my proposed implementation is as follows:
- Add a new property
preferredMediaTypetoCodegenConfig - Add a new property
preferredMediaTypetoCodeGenMojo - In
DefaultCodegen::fromOperation, compareconsumestopreferredMediaType. IfpreferredMediaTypeis not null andconsumescontainspreferredMediaType, thenconsumes = Arrays.asList(preferredMediaType). Otherwise, BAU. - Apply the same logic to
produces. - Update documentation as needed.
The above algorithm will ensure that if a given path supports preferredMediaType, then only preferredMediaType will be included in the generated code. Otherwise, the generated code will be same as BAU.
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 with CodegenConfig, CodeGenMojo, and DefaultCodegen::fromOperation, then trace how consumes and produces reach the generated ApiClient methods selectHeaderAccept and selectHeaderContentType. Confirm the behavior using the provided Swagger declaration and Java generation command. Done means preferredMediaType is configurable, preferred when supported, falls back to existing selection otherwise, and documentation is updated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100