2323import java .util .regex .Matcher ;
2424import java .util .regex .Pattern ;
2525
26+ import static org .openapitools .codegen .utils .StringUtils .camelize ;
27+
2628public class ScalaSttp4JsoniterClientCodegen extends AbstractScalaCodegen implements CodegenConfig {
2729 private static final StringProperty STTP_CLIENT_VERSION = new StringProperty ("sttpClientVersion" ,
2830 "The version of " +
@@ -59,6 +61,9 @@ public class ScalaSttp4JsoniterClientCodegen extends AbstractScalaCodegen implem
5961
6062 Map <String , ModelsMap > enumRefs = new HashMap <>();
6163
64+ private Map <String , String > apiNameMappings = new HashMap <>();
65+ private Set <String > uniqueApiNames = new HashSet <>();
66+
6267 public ScalaSttp4JsoniterClientCodegen () {
6368 super ();
6469 generatorMetadata = GeneratorMetadata .newBuilder (generatorMetadata )
@@ -183,7 +188,7 @@ public String encodePath(String input) {
183188 return buf .toString ();
184189 }
185190
186- private PathMetadata encPath (String input ) {
191+ private PathMetadata parseAndEncodePath (String input ) {
187192 String path = super .encodePath (input );
188193 ArrayList <String > pathParams = new ArrayList <>();
189194
@@ -206,7 +211,7 @@ public CodegenOperation fromOperation(String path,
206211 List <Server > servers ) {
207212 CodegenOperation op = super .fromOperation (path , httpMethod , operation , servers );
208213
209- PathMetadata pathMetadata = encPath (path );
214+ PathMetadata pathMetadata = parseAndEncodePath (path );
210215
211216 op .path = pathMetadata .getPath ();
212217
@@ -248,6 +253,41 @@ public String escapeReservedWord(String name) {
248253 return "`" + name + "`" ;
249254 }
250255
256+ @ Override
257+ public String toApiName (String name ) {
258+ // first come, first served
259+ // if a tag name is already mapped, use that mapping
260+ if (apiNameMappings .containsKey (name )) {
261+ return apiNameMappings .get (name );
262+ }
263+
264+ String generatedApiName = super .toApiName (name );
265+ String lowerCasedApiName = generatedApiName .toLowerCase (Locale .ROOT );
266+
267+ // check if the name is unique (case-insensitive)
268+ // if it's unique, add it to the mappings and return the generated name
269+ if (!uniqueApiNames .contains (lowerCasedApiName )) {
270+ uniqueApiNames .add (lowerCasedApiName );
271+ apiNameMappings .put (name , generatedApiName );
272+
273+ return generatedApiName ;
274+ } else {
275+ // if the name is not unique, generate a new name with a unique suffix
276+ int i = 0 ;
277+ while (true ) {
278+ String nextGeneratedApiName = super .toApiName (name + i );
279+ String lowerCasedNextGeneratedApiName = nextGeneratedApiName .toLowerCase (Locale .ROOT );
280+ if (!uniqueApiNames .contains (lowerCasedNextGeneratedApiName )) {
281+ uniqueApiNames .add (lowerCasedNextGeneratedApiName );
282+ apiNameMappings .put (name , nextGeneratedApiName );
283+
284+ return nextGeneratedApiName ;
285+ }
286+ i ++;
287+ }
288+ }
289+ }
290+
251291 @ Override
252292 public ModelsMap postProcessModels (ModelsMap objs ) {
253293 return objs ;
@@ -352,13 +392,6 @@ private boolean isEnumClass(final String importPath, final Map<String, ModelsMap
352392 public OperationsMap postProcessOperationsWithModels (OperationsMap objs , List <ModelMap > allModels ) {
353393 OperationMap ops = objs .getOperations ();
354394
355- // allModels.forEach(model -> {
356- // if (model.getModel().name.equals("PluginStatus")) {
357- // System.out.println("Found plugin status model");
358- // System.out.println(model.getModel());
359- // }
360- // });
361-
362395 for (CodegenOperation operation : ops .getOperation ()) {
363396 if (operation .returnType != null && !NO_JSON_CODEC_TYPES .contains (operation .returnType )) {
364397 String identifier = formatIdentifier (operation .returnType , false ) + "Codec" ;
0 commit comments