Skip to content

Commit 1bbbc6d

Browse files
authored
[Fix][scala-sttp] Register JSON value type in importMapping to avoid model-package-prefixed import (#23652)
* [Fix][scala-sttp] Skip model-package prefix when type name is already fully qualified * Refactor
1 parent 878d821 commit 1bbbc6d

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaSttpClientCodegen.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ public ScalaSttpClientCodegen() {
125125
additionalProperties.put("fnCamelize", new CamelizeLambda(false));
126126
additionalProperties.put("fnEnumEntry", new EnumEntryLambda());
127127

128-
// importMapping.remove("Seq");
129-
// importMapping.remove("List");
130-
// importMapping.remove("Set");
131-
// importMapping.remove("Map");
132-
133128
// TODO: there is no specific sttp mapping. All Scala Type mappings should be in AbstractScala
134129
typeMapping = new HashMap<>();
135130
typeMapping.put("array", "Seq");
@@ -170,9 +165,12 @@ public void processOpts() {
170165
modelPackage = PACKAGE_PROPERTY.getModelPackage(additionalProperties);
171166

172167
String jsonLibrary = JSON_LIBRARY_PROPERTY.getValue(additionalProperties);
173-
String jsonValueClass = "circe".equals(jsonLibrary) ? "io.circe.Json" : "org.json4s.JValue";
174-
typeMapping.put("object", jsonValueClass);
175-
typeMapping.put("AnyType", jsonValueClass);
168+
String jsonValueFqn = "circe".equals(jsonLibrary) ? "io.circe.Json" : "org.json4s.JValue";
169+
String jsonValueSimpleName = jsonValueFqn.substring(jsonValueFqn.lastIndexOf('.') + 1);
170+
171+
typeMapping.put("object", jsonValueSimpleName);
172+
typeMapping.put("AnyType", jsonValueSimpleName);
173+
importMapping.put(jsonValueSimpleName, jsonValueFqn);
176174

177175
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
178176
supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));

modules/openapi-generator/src/test/resources/3_0/scala-sttp-circe/petstore.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,22 @@ paths:
283283
description: file to upload
284284
type: string
285285
format: binary
286+
/store/stats:
287+
get:
288+
tags:
289+
- store
290+
summary: Returns store statistics as a free-form JSON object
291+
description: Returns arbitrary store metrics whose schema is not fixed
292+
operationId: getStoreStats
293+
responses:
294+
'200':
295+
description: successful operation
296+
content:
297+
application/json:
298+
schema:
299+
type: object
300+
security:
301+
- api_key: []
286302
/store/inventory:
287303
get:
288304
tags:

samples/client/petstore/scala-sttp-circe/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Class | Method | HTTP request | Description
7878
*StoreApi* | **deleteOrder** | **DELETE** /store/order/${orderId} | Delete purchase order by ID
7979
*StoreApi* | **getInventory** | **GET** /store/inventory | Returns pet inventories by status
8080
*StoreApi* | **getOrderById** | **GET** /store/order/${orderId} | Find purchase order by ID
81+
*StoreApi* | **getStoreStats** | **GET** /store/stats | Returns store statistics as a free-form JSON object
8182
*StoreApi* | **placeOrder** | **POST** /store/order | Place an order for a pet
8283
*UserApi* | **createUser** | **POST** /user | Create user
8384
*UserApi* | **createUsersWithArrayInput** | **POST** /user/createWithArray | Creates list of users with given input array

samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/api/StoreApi.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
package org.openapitools.client.api
1313

14+
import io.circe.Json
1415
import org.openapitools.client.model.Order
1516
import org.openapitools.client.core.JsonSupport._
1617
import sttp.client3._
@@ -72,6 +73,23 @@ class StoreApi(baseUrl: String) {
7273
.contentType("application/json")
7374
.response(asJson[Order])
7475

76+
/**
77+
* Returns arbitrary store metrics whose schema is not fixed
78+
*
79+
* Expected answers:
80+
* code 200 : Json (successful operation)
81+
*
82+
* Available security schemes:
83+
* api_key (apiKey)
84+
*/
85+
def getStoreStats(apiKeyHeader: String)(
86+
): Request[Either[ResponseException[String, Exception], Json], Any] =
87+
basicRequest
88+
.method(Method.GET, uri"$baseUrl/store/stats")
89+
.contentType("application/json")
90+
.header("api_key", apiKeyHeader)
91+
.response(asJson[Json])
92+
7593
/**
7694
*
7795
*

0 commit comments

Comments
 (0)