swagger-api / swagger-api/swagger-codegen

C# Custom JSON Content-Types not working

Open
#6,629 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

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
  1. 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"

  1. Generate sdk and compile.

  2. Make a request to the api using the sdk and capture using fiddler.

  3. 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.

  1. If none we're provided, default to application/json.
  2. If multiple we're provided, iterate over the list and if any 'IsJsonMime', return it.
  3. If not just return the first Content-Type in the list.

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.