OpenAPITools / OpenAPITools/openapi-generator

[CSharp] JSON encoding on a form value not supported

Open
#5,215 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

C-Sharp generator doesn't support complex serialization in form data. For example, the Slack example here under Complex Serialization in Form Data (after removing application/json block under content) will send form data like "payload=class Message...etc" instead of "payload={"text":"Swagger is awesome"}", since it uses the ToString on the object instead of ToJson.

openapi-generator version

Tested on 4.3 and 5.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Slack Incoming Webhook
externalDocs:
  url: https://api.slack.com/incoming-webhooks
paths:
  /services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX:
    post:
      summary: Post a message to Slack
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                payload:     # <--- form field that contains the JSON message
                  $ref: '#/components/schemas/Message'
            encoding:
              payload:
                contentType: application/json
      responses:
        '200':
          description: OK
components:
  schemas:
    Message:
      title: A Slack message
      type: object
      properties:
        text:
          type: string
          description: Message text
      required:
        - text
Command line used for generation

./mvn clean install

Steps to reproduce
  1. Generator the CSharp client
  2. Use the API
  3. Notice that the form value for "payload" will not be valid json
Related issues/PRs

Similar issue in Java - https://github.com/OpenAPITools/openapi-generator/issues/724

Suggest a fix/enhancement
  • Add jsonEncoding field to CodegenParameter
  • In DefaultCodegen, check for encoding override in fromRequestBodyToFormParameters
  • Modify the csharp api.mustache to use ToJson instead of ParameterToString if jsonEncoding is true

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 DefaultCodegen.fromRequestBodyToFormParameters and the CodegenParameter fields mentioned in the proposed fix, then inspect the CSharp api.mustache template. Reproduce the Slack form-data example with the provided OpenAPI declaration and generated CSharp client. Done means the payload form value is emitted as valid JSON rather than through ParameterToString.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.