swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.73k stars 6.02k forks source link

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

Open anujshahx opened 6 years ago

anujshahx commented 6 years ago
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
        }
wing328 commented 6 years ago

Please try the latest master (2.3.0), which has the Scala API client refactored.

pallavi-kotkar commented 6 years ago

I used the sonatype snapshot repo to test it out but it could not find the com.wordnik.swagger package.

pom.xml:

<repository>
            <id>snapshots-repo</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
</repository>
...
<plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.3.0-SNAPSHOT</version>
                <executions>

error:

[ERROR] import com.wordnik.swagger.client._
wing328 commented 6 years ago

Please also try the akka-scala generator to see if it has the same issue.