OpenAPITools / OpenAPITools/openapi-generator

[BUG][csharp][dotnet] Query parameters with explode: true not supported

Open
#17,991 2 comments 9 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

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

In OpenAPI 3.0, we can wrap query params into one param by using style: form and explode: true. (Ref: https://swagger.io/docs/specification/serialization/)

And the generated C# code has only one param in this method.

[Expected] But the param cannot be converted into the correct HTTP query param, eg. in CURL:

curl -X 'GET' \
  'https://raw.githubusercontent.com/resource?lang=en&scope=mobile' \ # correct
  -H 'accept: application/json'

[Actual] The generated code will convert the object's root name into the query param, eg. in CURL:

curl -X 'GET' \
  'https://raw.githubusercontent.com/resource?filter=XXXParam' \ # wrong
  -H 'accept: application/json'
openapi-generator version

v7.3.0

OpenAPI declaration file content or url
info:
  title: Example
  version: 1.0.0
openapi: 3.0.3
paths:
  "/resource":
    parameters:
      - name: filters
        in: query
        required: true
        schema:
          type: object
          additionalProperties:
            type: string
          example:
            lang: en
            scope: mobile
        style: form
        explode: true
    get:
      responses:
        '200':
          description: Ok
          content:
            application/json:
              schema:
                properties:
                  id:
                    type: string
                type: object

Generation Details
openapi-generator-cli generate -g csharp --additional-properties=library=httpclient --additional-properties=targetFramework=net6.0 -i file.yaml 
Steps to reproduce
  1. Generate the code
  2. new ResourceGetFiltersParam() and put some value into it
  3. Call await DefaultApi().resourceGet(param)
  4. Check the result
Related issues/PRs
  • #10438
  • #15633
  • #7973
  • #16435
Suggest a fix

In the ClientUtils.mustache, the generated code will call the function ParameterToMultiMap and the process will go to this step
https://github.com/OpenAPITools/openapi-generator/blob/275ed9296dea4a1a894cc5b3402b5b8f3a61e7d7/modules/openapi-generator/src/main/resources/csharp/ClientUtils.mustache#L87-L90

Like the suggestion in #16435 , we can use the similar way to solve this problem:

                  {{#isExplode}}
                    {{#isModel}}
                      for (let key of Object.keys({{paramName}})) {
                        localVarQueryParameter[key] = ({{paramName}} as any)[key];
                      }

                    {{/isModel}}
                    {{#isPrimitiveType}}
                        localVarQueryParameter['{{baseName}}'] = {{paramName}};
                    {{/isPrimitiveType}}
                  {{/isExplode}}
                  {{^isExplode}}
                      localVarQueryParameter['{{baseName}}'] = {{paramName}};
                  {{/isExplode}}

[!NOTE]
I want to open a Pull Request to solve this issue. ✍🙏

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# ClientUtils.mustache section linked in the issue, then trace the generated call to ParameterToMultiMap. Reproduce with the provided OpenAPI YAML and csharp generation command, using the filters example to compare the generated request with the expected lang=en&scope=mobile query. Done means exploded object properties are serialized as separate query parameters.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.