Skip to content

[CSharp] JSON encoding on a form value not supported #5215

@milesressler

Description

@milesressler
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 - #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions