OpenAPITools / OpenAPITools/openapi-generator

[BUG][Kotlin] Root-level arrays in response can't be parsed to List

Open
#7,818 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

My server returns an JSON array at root level. When I'm using the Kotlin Generator with "collectionType": "list" it crashes, no matter which JSON parser is being used. The reason is that the root level array can't be serialized correctly to kotlin.List<*>. Instead it returns an ArrayList of LinkedHashTreeMaps containing the JSON data.

However if the array is wrapped in an JSON object, it's working fine.
Also if I switch to "collectionType": "array" or to the Java client, it's working fine.

openapi-generator version

4.X.X up to current 5.0.0-snapshot

OpenAPI declaration file content or url

https://gist.github.com/fl034/c24adc469b154dc61e8b70b5f969bc28

Generation Details
{
  "dateLibrary"   : "threetenbp",
  "collectionType": "list"
}
Steps to reproduce
  1. Have a server returning an array of objects at root level of the JSON response body.
  2. Try parsing it with the Kotlin client configured with "collectionType": "list"
Related issues/PRs
Suggest a fix

I'm fairly new to Kotlin/Java, so I can't really help out here. But I suppose the problem is in ApiClient.kt in the responseBody function where the serialization is going wrong.

Also I suggest setting collectionType: array as new default for the Kotlin generator to avoid this bug for most users.

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 OpenAPI declaration in the linked gist and the Kotlin generator's ApiClient.kt responseBody function. Reproduce a root-level array response with collectionType set to list, compare it with the working array and Java-client cases, and confirm that Kotlin parses the response into the expected List rather than an ArrayList of LinkedHashTreeMaps.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.