swagger-api / swagger-api/swagger-codegen

[Scala] Handling map[String, Model] in Scala client generated code

Open
#6,225 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Scala Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The swagger-codegen generated code for Scala client doesn't handle map[String, SchemaObject]

Swagger-codegen version
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2</version>

Swagger yaml spec:

        200:
          description: OK
          schema:
            type: object
            additionalProperties:
              $ref: "#/definitions/dataSchema"

Exception:

Exception in thread "main" java.lang.ClassCastException: com.xyz.model.dataSchema cannot be cast to scala.collection.immutable.Map

Fix proposed:
ApiInvoker.scala

case "map" => {
          val typeInfo = mapper.getTypeFactory().constructMapType(classOf[java.util.Map[_,_]], classOf[String], cls);
          println(json)
          val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.Map[_,_]]
          response.asScala.toMap
        }

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 ApiInvoker.scala and reproduce the Swagger YAML case where a 200 response is an object with additionalProperties referencing dataSchema. Check the generated Scala client's map deserialization path and verify the result is handled as a Scala map without the ClassCastException; completion should include a passing regression test if the repository provides one.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.