Repository: OpenAPITools/openapi-generator (language: python)

Issue:
[BUG] Kotlin generator: generateOneOfAnyOfWrappers=true leads to broken code
##### Description

While using the Kotlin generator in a Android Project with "generateOneOfAnyOfWrappers=true", the generator generates code with the Gson Library even tough it should be Moshi by default(?). After including the Gson Library to the project, it still failed. Partly because of invalid naming of variables. As you can see in the picture below, on line 37 and 38, the generates variables are interpreted by kotlin as extension functions. So this won't compile.

![Bildschirmfoto 2024-10-22 um 12 02 06](https://github.com/user-attachments/assets/55062733-63cc-492c-a89a-4e75562737ac)

To start from the beginning: We received a new OpenAPI Spec from our server team. The spec contains some "OneOf" relations. The file itself should be valid, since our iOS team was able to generate the desired code with `openapi-generator generate -i Common/MeteoMapsAPI/meteomapsapi.json -g swift5 -o Common/MeteoMapsAPI --model-name-prefix SRF --additional-properties=projectName=MeteoMapsAPI,generateAliasAsModel=true`. 
If we (Android team) run the generator without "generateOneOfAnyOfWrappers=true", an error get's thrown `@JsonClass can't be applied to ch.srf.meteo.data.meteomap.model.MeteoMapLayer: must be a Kotlin class`. This makes sense looking into the generated code:

![Bildschirmfoto 2024-10-22 um 11 59 23](https://github.com/user-attachments/assets/6fde3437-ea16-4d96-a246-a9af91fa8856)

So I guess, the approach of using "generateOneOfAnyOfWrappers=true" should be correct?

The full OpenAPI declaration file can be found below.

##### openapi-generator version

7.9.0, 7.8.0

##### OpenAPI declaration file content or url

[meteomap-api.json](https://github.com/user-attachments/files/17473660/meteomap-api.json)

##### Generation Details

```
    val generateMeteoMap by registering(GenerateTask::class) {
        generatorName.set("kotlin")
        .....
        configOptions.set(apiConfigOptions)
        additionalProperties.set(
            mapOf(
                "enumPropertyNaming" to "UPPERCASE",
                "useCoroutines" to "true",
                "moshiCodeGen" to "true",
                "generateOneOfAnyOfWrappers" to "true",
            )
        )
        library.set("jvm-retrofit2")
    }
```

##### Steps to reproduce

If you run the generator either with Gradle or via CLI with the attached OpenAPI declaration file (which should be valid in my opinion), the error occures.


Discussion / hints:

Fix patch:
diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache
index a745e7745f0c..bac91b313f90 100644
--- a/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache
+++ b/modules/openapi-generator/src/main/resources/kotlin-client/anyof_class.mustache
@@ -83,7 +83,7 @@ import java.io.IOException
             {{#anyOf}}
             {{^isArray}}
             {{^vendorExtensions.x-duplicated-data-type}}
-            val adapter{{{dataType}}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}::class.java))
+            val adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}::class.java))
             {{/vendorExtensions.x-duplicated-data-type}}
             {{/isArray}}
             {{#isArray}}
@@ -122,7 +122,7 @@ import java.io.IOException
                         return
                     {{/isPrimitiveType}}
                     {{^isPrimitiveType}}
-                        val element = adapter{{{dataType}}}.toJsonTree(value.actualInstance as {{{dataType}}}?)
+                        val element = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(value.actualInstance as {{{dataType}}}?)
                         elementAdapter.write(out, element)
                         return
                     {{/isPrimitiveType}}
@@ -185,7 +185,7 @@ import java.io.IOException
                         for(element in jsonElement.getAsJsonArray()) {
                         {{#items}}
                         {{#isNumber}}
-                            require(jsonElement.getAsJsonPrimitive().isNumber) {
+                            require(element.getAsJsonPrimitive().isNumber) {
                                 String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
                             }
                         {{/isNumber}}
@@ -238,4 +238,99 @@ import java.io.IOException
             }.nullSafe() as TypeAdapter<T>
         }
     }
+
+    companion object {
+        /**
+        * Validates the JSON Element and throws an exception if issues found
+        *
+        * @param jsonElement JSON Element
+        * @throws IOException if the JSON Element is invalid with respect to {{classname}}
+        */
+        @Throws(IOException::class)
+        fun validateJsonElement(jsonElement: JsonElement?) {
+            requireNotNull(jsonElement) {
+                "Provided json element must not be null"
+            }
+            var match = 0
+            val errorMessages = ArrayList<String>()
+            {{#composedSchemas}}
+            {{#anyOf}}
+            {{^vendorExtensions.x-duplicated-data-type}}
+            {{^hasVars}}
+            // validate the json string with {{{dataType}}}
+            try {
+                // validate the JSON object to see if any exception is thrown
+            {{^isArray}}
+            {{#isNumber}}
+                require(jsonElement.getAsJsonPrimitive().isNumber()) {
+                    String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
+                }
+            {{/isNumber}}
+            {{^isNumber}}
+            {{#isPrimitiveType}}
+                require(jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
+                    String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
+                }
+            {{/isPrimitiveType}}
+            {{/isNumber}}
+            {{^isNumber}}
+            {{^isPrimitiveType}}
+                {{{dataType}}}.validateJsonElement(jsonElement)
+            {{/isPrimitiveType}}
+            {{/isNumber}}
+            {{/isArray}}
+            {{#isArray}}
+                require(jsonElement.isJsonArray) {
+                    String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
+                }
+
+                // validate array items
+                for(element in jsonElement.getAsJsonArray()) {
+                {{#items}}
+                {{#isNumber}}
+                    require(element.getAsJsonPrimitive().isNumber) {
+                        String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
+                    }
+                {{/isNumber}}
+                {{^isNumber}}
+                {{#isPrimitiveType}}
+                    require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) {
+                        String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
+                    }
+                {{/isPrimitiveType}}
+                {{/isNumber}}
+                {{^isNumber}}
+                {{^isPrimitiveType}}
+                    {{{dataType}}}.validateJsonElement(element)
+                {{/isPrimitiveType}}
+                {{/isNumber}}
+                {{/items}}
+                }
+            {{/isArray}}
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message))
+            }
+            {{/hasVars}}
+            {{#hasVars}}
+            // validate json string for {{{.}}}
+            try {
+                // validate the JSON object to see if any exception is thrown
+                {{.}}.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // validation failed, continue
+                errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message))
+            }
+            {{/hasVars}}
+            {{/vendorExtensions.x-duplicated-data-type}}
+            {{/anyOf}}
+            {{/composedSchemas}}
+
+            if (match != 1) {
+                throw IOException(String.format("Failed validation for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache
index eebf9858773b..a7d43e97acc5 100644
--- a/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache
+++ b/modules/openapi-generator/src/main/resources/kotlin-client/oneof_class.mustache
@@ -83,7 +83,7 @@ import java.io.IOException
             {{#oneOf}}
             {{^isArray}}
             {{^vendorExtensions.x-duplicated-data-type}}
-            val adapter{{{dataType}}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}::class.java))
+            val adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}} = gson.getDelegateAdapter(this, TypeToken.get({{{dataType}}}::class.java))
             {{/vendorExtensions.x-duplicated-data-type}}
             {{/isArray}}
             {{#isArray}}
@@ -122,7 +122,7 @@ import java.io.IOException
                         return
                     {{/isPrimitiveType}}
                     {{^isPrimitiveType}}
-                        val element = adapter{{{dataType}}}.toJsonTree(value.actualInstance as {{{dataType}}}?)
+                        val element = adapter{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}.toJsonTree(value.actualInstance as {{{dataType}}}?)
                         elementAdapter.write(out, element)
                         return
                     {{/isPrimitiveType}}
@@ -179,7 +179,7 @@ import java.io.IOException
                         for(element in jsonElement.getAsJsonArray()) {
                         {{#items}}
                         {{#isNumber}}
-                            require(jsonElement.getAsJsonPrimitive().isNumber) {
+                            require(element.getAsJsonPrimitive().isNumber) {
                                 String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
                             }
                         {{/isNumber}}
@@ -236,4 +236,99 @@ import java.io.IOException
             }.nullSafe() as TypeAdapter<T>
         }
     }
+
+    companion object {
+        /**
+        * Validates the JSON Element and throws an exception if issues found
+        *
+        * @param jsonElement JSON Element
+        * @throws IOException if the JSON Element is invalid with respect to {{classname}}
+        */
+        @Throws(IOException::class)
+        fun validateJsonElement(jsonElement: JsonElement?) {
+            requireNotNull(jsonElement) {
+                "Provided json element must not be null"
+            }
+            var match = 0
+            val errorMessages = ArrayList<String>()
+            {{#composedSchemas}}
+            {{#oneOf}}
+            {{^vendorExtensions.x-duplicated-data-type}}
+            {{^hasVars}}
+            // validate the json string with {{{dataType}}}
+            try {
+                // validate the JSON object to see if any exception is thrown
+            {{^isArray}}
+            {{#isNumber}}
+                require(jsonElement.getAsJsonPrimitive().isNumber()) {
+                    String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
+                }
+            {{/isNumber}}
+            {{^isNumber}}
+            {{#isPrimitiveType}}
+                require(jsonElement.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}()) {
+                    String.format("Expected json element to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
+                }
+            {{/isPrimitiveType}}
+            {{/isNumber}}
+            {{^isNumber}}
+            {{^isPrimitiveType}}
+                {{{dataType}}}.validateJsonElement(jsonElement)
+            {{/isPrimitiveType}}
+            {{/isNumber}}
+            {{/isArray}}
+            {{#isArray}}
+                require(jsonElement.isJsonArray) {
+                    String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
+                }
+
+                // validate array items
+                for(element in jsonElement.getAsJsonArray()) {
+                {{#items}}
+                {{#isNumber}}
+                    require(jsonElement.getAsJsonPrimitive().isNumber) {
+                        String.format("Expected json element to be of type Number in the JSON string but got `%s`", jsonElement.toString())
+                    }
+                {{/isNumber}}
+                {{^isNumber}}
+                {{#isPrimitiveType}}
+                    require(element.getAsJsonPrimitive().is{{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}}) {
+                        String.format("Expected array items to be of type {{#isBoolean}}Boolean{{/isBoolean}}{{#isString}}String{{/isString}}{{^isString}}{{^isBoolean}}Number{{/isBoolean}}{{/isString}} in the JSON string but got `%s`", jsonElement.toString())
+                    }
+                {{/isPrimitiveType}}
+                {{/isNumber}}
+                {{^isNumber}}
+                {{^isPrimitiveType}}
+                    {{{dataType}}}.validateJsonElement(element)
+                {{/isPrimitiveType}}
+                {{/isNumber}}
+                {{/items}}
+                }
+            {{/isArray}}
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for {{{dataType}}} failed with `%s`.", e.message))
+            }
+            {{/hasVars}}
+            {{#hasVars}}
+            // validate json string for {{{.}}}
+            try {
+                // validate the JSON object to see if any exception is thrown
+                {{.}}.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // validation failed, continue
+                errorMessages.add(String.format("Validation for {{{.}}} failed with `%s`.", e.message))
+            }
+            {{/hasVars}}
+            {{/vendorExtensions.x-duplicated-data-type}}
+            {{/oneOf}}
+            {{/composedSchemas}}
+
+            if (match != 1) {
+                throw IOException(String.format("Failed validation for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt
index 3d523a450eac..e7c337f2ebd7 100644
--- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt
+++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPet.kt
@@ -111,4 +111,43 @@ data class ApiAnyOfUserOrPet(var actualInstance: Any? = null) {
             }.nullSafe() as TypeAdapter<T>
         }
     }
+
+    companion object {
+        /**
+        * Validates the JSON Element and throws an exception if issues found
+        *
+        * @param jsonElement JSON Element
+        * @throws IOException if the JSON Element is invalid with respect to ApiAnyOfUserOrPet
+        */
+        @Throws(IOException::class)
+        fun validateJsonElement(jsonElement: JsonElement?) {
+            requireNotNull(jsonElement) {
+                "Provided json element must not be null"
+            }
+            var match = 0
+            val errorMessages = ArrayList<String>()
+            // validate the json string with ApiUser
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiUser.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
+            }
+            // validate the json string with ApiPet
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiPet.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
+            }
+
+            if (match != 1) {
+                throw IOException(String.format("Failed validation for ApiAnyOfUserOrPet: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
+            }
+        }
+    }
 }
diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt
index 1ec7549f45c0..4f908f690796 100644
--- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt
+++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiAnyOfUserOrPetOrArrayString.kt
@@ -144,4 +144,61 @@ data class ApiAnyOfUserOrPetOrArrayString(var actualInstance: Any? = null) {
             }.nullSafe() as TypeAdapter<T>
         }
     }
+
+    companion object {
+        /**
+        * Validates the JSON Element and throws an exception if issues found
+        *
+        * @param jsonElement JSON Element
+        * @throws IOException if the JSON Element is invalid with respect to ApiAnyOfUserOrPetOrArrayString
+        */
+        @Throws(IOException::class)
+        fun validateJsonElement(jsonElement: JsonElement?) {
+            requireNotNull(jsonElement) {
+                "Provided json element must not be null"
+            }
+            var match = 0
+            val errorMessages = ArrayList<String>()
+            // validate the json string with ApiUser
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiUser.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
+            }
+            // validate the json string with ApiPet
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiPet.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
+            }
+            // validate the json string with kotlin.collections.List<kotlin.String>
+            try {
+                // validate the JSON object to see if any exception is thrown
+                require(jsonElement.isJsonArray) {
+                    String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
+                }
+
+                // validate array items
+                for(element in jsonElement.getAsJsonArray()) {
+                    require(element.getAsJsonPrimitive().isString) {
+                        String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString())
+                    }
+                }
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for kotlin.collections.List<kotlin.String> failed with `%s`.", e.message))
+            }
+
+            if (match != 1) {
+                throw IOException(String.format("Failed validation for ApiAnyOfUserOrPetOrArrayString: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
+            }
+        }
+    }
 }
diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt
index 372906b0e56b..5922d1746556 100644
--- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt
+++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPet.kt
@@ -115,4 +115,43 @@ data class ApiUserOrPet(var actualInstance: Any? = null) {
             }.nullSafe() as TypeAdapter<T>
         }
     }
+
+    companion object {
+        /**
+        * Validates the JSON Element and throws an exception if issues found
+        *
+        * @param jsonElement JSON Element
+        * @throws IOException if the JSON Element is invalid with respect to ApiUserOrPet
+        */
+        @Throws(IOException::class)
+        fun validateJsonElement(jsonElement: JsonElement?) {
+            requireNotNull(jsonElement) {
+                "Provided json element must not be null"
+            }
+            var match = 0
+            val errorMessages = ArrayList<String>()
+            // validate the json string with ApiUser
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiUser.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
+            }
+            // validate the json string with ApiPet
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiPet.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
+            }
+
+            if (match != 1) {
+                throw IOException(String.format("Failed validation for ApiUserOrPet: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
+            }
+        }
+    }
 }
diff --git a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt
index 009b3bef5230..060b98933e64 100644
--- a/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt
+++ b/samples/client/petstore/kotlin-model-prefix-type-mappings/src/main/kotlin/org/openapitools/client/models/ApiUserOrPetOrArrayString.kt
@@ -147,4 +147,61 @@ data class ApiUserOrPetOrArrayString(var actualInstance: Any? = null) {
             }.nullSafe() as TypeAdapter<T>
         }
     }
+
+    companion object {
+        /**
+        * Validates the JSON Element and throws an exception if issues found
+        *
+        * @param jsonElement JSON Element
+        * @throws IOException if the JSON Element is invalid with respect to ApiUserOrPetOrArrayString
+        */
+        @Throws(IOException::class)
+        fun validateJsonElement(jsonElement: JsonElement?) {
+            requireNotNull(jsonElement) {
+                "Provided json element must not be null"
+            }
+            var match = 0
+            val errorMessages = ArrayList<String>()
+            // validate the json string with ApiUser
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiUser.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiUser failed with `%s`.", e.message))
+            }
+            // validate the json string with ApiPet
+            try {
+                // validate the JSON object to see if any exception is thrown
+                ApiPet.validateJsonElement(jsonElement)
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for ApiPet failed with `%s`.", e.message))
+            }
+            // validate the json string with kotlin.collections.List<kotlin.String>
+            try {
+                // validate the JSON object to see if any exception is thrown
+                require(jsonElement.isJsonArray) {
+                    String.format("Expected json element to be a array type in the JSON string but got `%s`", jsonElement.toString())
+                }
+
+                // validate array items
+                for(element in jsonElement.getAsJsonArray()) {
+                    require(element.getAsJsonPrimitive().isString) {
+                        String.format("Expected array items to be of type String in the JSON string but got `%s`", jsonElement.toString())
+                    }
+                }
+                match++
+            } catch (e: Exception) {
+                // Validation failed, continue
+                errorMessages.add(String.format("Validation for kotlin.collections.List<kotlin.String> failed with `%s`.", e.message))
+            }
+
+            if (match != 1) {
+                throw IOException(String.format("Failed validation for ApiUserOrPetOrArrayString: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonElement.toString()))
+            }
+        }
+    }
 }


Test patch:
diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/ClientLibrary.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/ClientLibrary.java
new file mode 100644
index 000000000000..cfb09f3a5827
--- /dev/null
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/ClientLibrary.java
@@ -0,0 +1,29 @@
+package org.openapitools.codegen.kotlin;
+
+import lombok.Getter;
+import org.jetbrains.kotlin.com.intellij.openapi.util.text.Strings;
+
+@Getter
+enum ClientLibrary {
+    JVM_KTOR("main/kotlin"),
+    JVM_OKHTTP4("main/kotlin"),
+    JVM_SPRING_WEBCLIENT("main/kotlin"),
+    JVM_SPRING_RESTCLIENT("main/kotlin"),
+    JVM_RETROFIT2("main/kotlin"),
+    MULTIPLATFORM("commonMain/kotlin"),
+    JVM_VOLLEY("gson", "main/java"),
+    JVM_VERTX("main/kotlin");
+    private final String serializationLibrary;
+    private final String libraryName;
+    private final String sourceRoot;
+
+    ClientLibrary(String serializationLibrary, String sourceRoot) {
+        this.serializationLibrary = serializationLibrary;
+        this.sourceRoot = sourceRoot;
+        this.libraryName = Strings.toLowerCase(this.name()).replace("_", "-");
+    }
+
+    ClientLibrary(String sourceRoot) {
+        this("jackson", sourceRoot);
+    }
+}
diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java
index 4349e88d4438..16e7e6e6457e 100644
--- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java
@@ -22,7 +22,7 @@
 
 public class KotlinClientCodegenApiTest {
 
-    @DataProvider(name = "pathResponses")
+    @DataProvider(name = "clientLibraries")
     public Object[][] pathResponses() {
         return new Object[][]{
                 {ClientLibrary.JVM_KTOR},
@@ -36,7 +36,7 @@ public Object[][] pathResponses() {
         };
     }
 
-    @Test(dataProvider = "pathResponses")
+    @Test(dataProvider = "clientLibraries")
     void testPathVariableIsNotEscaped_19930(ClientLibrary library) throws IOException {
 
         OpenAPI openAPI = new OpenAPIParser()
@@ -76,29 +76,4 @@ private KotlinClientCodegen createCodegen(ClientLibrary library) throws IOExcept
         codegen.additionalProperties().put(KotlinClientCodegen.DATE_LIBRARY, "kotlinx-datetime");
         return codegen;
     }
-
-    @Getter
-    private enum ClientLibrary {
-        JVM_KTOR("main/kotlin"),
-        JVM_OKHTTP4("main/kotlin"),
-        JVM_SPRING_WEBCLIENT("main/kotlin"),
-        JVM_SPRING_RESTCLIENT("main/kotlin"),
-        JVM_RETROFIT2("main/kotlin"),
-        MULTIPLATFORM("commonMain/kotlin"),
-        JVM_VOLLEY("gson", "main/java"),
-        JVM_VERTX("main/kotlin");
-        private final String serializationLibrary;
-        private final String libraryName;
-        private final String sourceRoot;
-
-        ClientLibrary(String serializationLibrary, String sourceRoot) {
-            this.serializationLibrary = serializationLibrary;
-            this.sourceRoot = sourceRoot;
-            this.libraryName = Strings.toLowerCase(this.name()).replace("_", "-");
-        }
-
-        ClientLibrary(String sourceRoot) {
-            this("jackson", sourceRoot);
-        }
-    }
 }
diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java
index f3026827af7f..7834549424bc 100644
--- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java
@@ -433,6 +433,66 @@ public void testFailOnUnknownPropertiesAdditionalProperty() {
         configAssert.assertValue(KotlinClientCodegen.FAIL_ON_UNKNOWN_PROPERTIES, codegen::isFailOnUnknownProperties, Boolean.FALSE);
     }
 
+    @DataProvider(name = "gsonClientLibraries")
+    public Object[][] pathResponses() {
+        return new Object[][]{
+                {ClientLibrary.JVM_KTOR},
+                {ClientLibrary.JVM_OKHTTP4},
+                {ClientLibrary.JVM_RETROFIT2},
+                {ClientLibrary.MULTIPLATFORM},
+                {ClientLibrary.JVM_VOLLEY},
+                {ClientLibrary.JVM_VERTX}
+        };
+    }
+
+    @Test(dataProvider = "gsonClientLibraries")
+    public void testLocalVariablesUseSanitizedDataTypeNamesForOneOfProperty_19942(ClientLibrary clientLibrary) throws IOException {
+        File output = Files.createTempDirectory("test").toFile();
+        String path = output.getAbsolutePath();
+        output.deleteOnExit();
+
+        final CodegenConfigurator configurator = new CodegenConfigurator()
+                .setGeneratorName("kotlin")
+                .setLibrary(clientLibrary.getLibraryName())
+                .setInputSpec("src/test/resources/3_0/issue_19942.json")
+                .addAdditionalProperty("omitGradleWrapper", true)
+                .addAdditionalProperty("serializationLibrary", "gson")
+                .addAdditionalProperty("dateLibrary", "kotlinx-datetime")
+                .addAdditionalProperty("useSpringBoot3", "true")
+                .addAdditionalProperty("generateOneOfAnyOfWrappers", true)
+                .setOutputDir(output.getAbsolutePath().replace("\\", "/"));
+        DefaultGenerator generator = new DefaultGenerator();
+
+        generator.opts(configurator.toClientOptInput()).generate();
+
+        TestUtils.assertFileNotContains(Paths.get(path + "/src/" + clientLibrary.getSourceRoot() + "/org/openapitools/client/models/ObjectWithComplexOneOfId.kt"),
+                "val adapterkotlin.String", "val adapterjava.math.BigDecimal");
+    }
+
+    @Test(dataProvider = "gsonClientLibraries")
+    public void testLocalVariablesUseSanitizedDataTypeNamesForAnyOfProperty_19942(ClientLibrary clientLibrary) throws IOException {
+        File output = Files.createTempDirectory("test").toFile();
+        String path = output.getAbsolutePath();
+        output.deleteOnExit();
+
+        final CodegenConfigurator configurator = new CodegenConfigurator()
+                .setGeneratorName("kotlin")
+                .setLibrary(clientLibrary.getLibraryName())
+                .setInputSpec("src/test/resources/3_0/issue_19942.json")
+                .addAdditionalProperty("omitGradleWrapper", true)
+                .addAdditionalProperty("serializationLibrary", "gson")
+                .addAdditionalProperty("dateLibrary", "kotlinx-datetime")
+                .addAdditionalProperty("useSpringBoot3", "true")
+                .addAdditionalProperty("generateOneOfAnyOfWrappers", true)
+                .setOutputDir(output.getAbsolutePath().replace("\\", "/"));
+        DefaultGenerator generator = new DefaultGenerator();
+
+        generator.opts(configurator.toClientOptInput()).generate();
+
+        TestUtils.assertFileNotContains(Paths.get(path + "/src/" + clientLibrary.getSourceRoot() + "/org/openapitools/client/models/ObjectWithComplexAnyOfId.kt"),
+                "val adapterkotlin.String", "val adapterjava.math.BigDecimal");
+    }
+
     private static class ModelNameTest {
         private final String expectedName;
         private final String expectedClassName;
diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_19942.json b/modules/openapi-generator/src/test/resources/3_0/issue_19942.json
new file mode 100644
index 000000000000..5f9d618dc56b
--- /dev/null
+++ b/modules/openapi-generator/src/test/resources/3_0/issue_19942.json
@@ -0,0 +1,72 @@
+{
+  "openapi": "3.0.1",
+  "info": {
+    "title": "xxxxx",
+    "version": "1.0"
+  },
+  "servers": [
+    {
+      "url": "https://xxxxx"
+    },
+    {
+      "url": "http://localhost:3000"
+    }
+  ],
+  "paths": {
+    "/test": {
+      "get": {
+        "operationId": "testOneOf",
+        "tags": [
+          "testOneOf"
+        ],
+        "summary": "fetches oneOf id",
+        "responses": {
+          "200": {
+            "description": "ok",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "$ref": "#/components/schemas/ObjectWithComplexOneOf"
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  },
+  "components" : {
+    "schemas": {
+      "ObjectWithComplexOneOf" : {
+        "type": "object",
+        "properties": {
+          "id": {
+            "oneOf": [
+              {
+                "type": "string"
+              },
+              {
+                "type": "number"
+              }
+            ]
+          }
+        }
+      },
+      "ObjectWithComplexAnyOf" : {
+        "type": "object",
+        "properties": {
+          "id": {
+            "anyOf": [
+              {
+                "type": "string"
+              },
+              {
+                "type": "number"
+              }
+            ]
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file