swagger-api / swagger-api/swagger-codegen
C# Custom JSON Content-Types not working
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When generating an SDK for API endpoints with custom json content-types, the current logic in the ApiClient class overrides the content-type to 'application/json' if the type of the request body is string.
Swagger-codegen version
Latest stable version 2.2.3
Command line used for generation
-jar sdk-generate.jar generate -l csharp -i -o output -DmodelTests=false
-DapiTests=false",
Steps to reproduce
- Include custom consumes / produces in the path definition for a swagger document.
eg: "application/vnd.ed-fi.student.student-enrollment-readable-includes-embedded-object.readable+json"
-
Generate sdk and compile.
-
Make a request to the api using the sdk and capture using fiddler.
-
Inspect body and notice the content type is not the custom json type but rather 'application/json'
Related issues/PRs
None found
Suggest a fix/enhancement
ApiClient.mustache in the csharp resource has been updated locally to reflect the java implementation of Content-Type selection (Was going to open a prq but wanted to mention it here first). Regardless of body type (string or byte[]), I think we should use the content-type the SDK was generated with and not default to Application/Json in certain scenarios. The logic found in the java mustache file is as follows and was replicated for csharp.
- If none we're provided, default to application/json.
- If multiple we're provided, iterate over the list and if any 'IsJsonMime', return it.
- If not just return the first Content-Type in the list.
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 the C# ApiClient.mustache template and compare its Content-Type selection with the Java mustache implementation described in the issue. Generate a C# SDK from a Swagger document using the custom JSON content type, compile it, and capture a request to verify that the generated client preserves the custom type instead of using application/json.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100