OpenAPITools / OpenAPITools/openapi-generator

[BUG][Kotlin][Multiplatform] Request under incorrect path made if query object parameter used

Open
#10,210 1 comment 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

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

Please read "OpenAPI declaration file content or url" section first. This paragraph uses API defined there.

If there's a query parameter that is not a simple type, but an object:

 - name: someComplexQueryParameter
   in: query
   required: true
   schema:
     "$ref": "#/components/schemas/SomeObject"

the generated code is invalid - it calls toString() on the object which fires the auto-generated toString() in the data class:

val localVariableQuery = mutableMapOf<String, List<String>>()
someComplexQueryParameter?.apply { localVariableQuery["someComplexQueryParameter"] = listOf("$someComplexQueryParameter") }

Compare paths under which the request is made (I presume):

Expected:
https://some-service.com/somePath?someProperty=someValue

Actual:
https://some-service.com/somePath?someComplexQueryParameter=SomeObject(someProperty=someValue)

openapi-generator version

5.2.1. I don't know if it's a regression, I never saw it working.

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test API
  version: '1'
paths:
  "/somePath":
    get:
      operationId: someOperation
      parameters:
      - name: someComplexQueryParameter
        in: query
        required: true
        schema:
          "$ref": "#/components/schemas/SomeObject"
      responses:
        '200':
          description: OK
components:
  schemas:
    SomeObject:
      type: object
      properties:
        someProperty:
          type: string
Generation Details

Using the Gradle plugin:

openApiGenerate {
    generatorName.set("kotlin")
    library.set("multiplatform")
}
Steps to reproduce

Just generate the code using the settings given above.

Related issues/PRs

I haven't found one.

Suggest a fix

I worked around the issue by changing the generated code from:

val localVariableQuery = mutableMapOf<String, List<String>>()
someComplexQueryParameter?.apply { localVariableQuery["someComplexQueryParameter"] = listOf("$someComplexQueryParameter") }

to

val localVariableQuery = mutableMapOf<String, List<String>>()
with(someComplexQueryParameter) {
    someProperty?.let { localVariableQuery["someProperty"] = listOf("$query") }
}

but I don't know how it should be adjusted for other types of properties, and didn't think about edge cases.

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

Run the Gradle openApiGenerate configuration with generatorName kotlin and library multiplatform against the supplied OpenAPI spec. Inspect the generated request query construction and compare it with the expected someProperty query; done means the request no longer serializes the object as SomeObject(someProperty=someValue).

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.