OpenAPITools / OpenAPITools/openapi-generator

[BUG][csharp-netcore] HttpClient always uses "application/json" as Content-Type even other type is specified.

Open
#12,084 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

This is a bit of a poor bug report, I'll fill it out later if required, and if this change is acceptable I'll make a quick PR. Since it will be a mayor behaviour change to fix it.

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

We have some Patch endpoint which only accept application/json-patch+json Content-Type.

These lines: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache#L379-L383=

Should probably be:

{
    contentType = contentType ?? "application/json";

    var serializer = new CustomJsonCodec(SerializerSettings, configuration);
    request.Content = new StringContent(serializer.Serialize(options.Data), new UTF8Encoding(),
        contentType);
}
openapi-generator version

6.0.0-SNAPSHOT (:latest docker tag)

OpenAPI declaration file content or url
requestBody:
    content:
      application/json-patch+json:
        schema:
          $ref: '#/components/schemas/JsonPatchDocument'
Generation Details

This is for the csharp-netcore generator with the library: httpclient config option. Using the :latest docker tag.

Steps to reproduce
  1. Do request to server
  2. Check Content-Type that the server receives. (It's application/json instead of application/json-patch+json)
Related issues/PRs
Suggest a fix

These lines: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache#L379-L383=

Should probably be:

{
    contentType = contentType ?? "application/json";

    var serializer = new CustomJsonCodec(SerializerSettings, configuration);
    request.Content = new StringContent(serializer.Serialize(options.Data), new UTF8Encoding(),
        contentType);
}

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 modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache around lines 379-383 and inspect how the request content type is selected. Generate a csharp-netcore client with the httpclient library for an application/json-patch+json request body, then confirm the generated request preserves that media type instead of always using application/json.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.