From f174ad6a6a18ae904bda9e9104b4968633402bf1 Mon Sep 17 00:00:00 2001 From: Marcel Joss Date: Thu, 15 Jun 2023 21:07:16 +0200 Subject: [PATCH] fix(codegen): use unicode codepoints as length --- .../codegen/kotlin/ktor/KotlinKtorGeneratorUtilities.kt | 6 +++--- .../tools/samt/client/generated/consumer/Consumer.kt | 4 ++-- .../tools/samt/client/generated/greeter/KtorMappings.kt | 8 ++++---- .../samt/server/generated/greeter/GreeterEndpoint.kt | 6 +++--- .../tools/samt/server/generated/greeter/KtorMappings.kt | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/codegen/src/main/kotlin/tools/samt/codegen/kotlin/ktor/KotlinKtorGeneratorUtilities.kt b/codegen/src/main/kotlin/tools/samt/codegen/kotlin/ktor/KotlinKtorGeneratorUtilities.kt index 50d03a79..eb271561 100644 --- a/codegen/src/main/kotlin/tools/samt/codegen/kotlin/ktor/KotlinKtorGeneratorUtilities.kt +++ b/codegen/src/main/kotlin/tools/samt/codegen/kotlin/ktor/KotlinKtorGeneratorUtilities.kt @@ -225,12 +225,12 @@ private fun validateLiteralConstraintsSuffix(typeReference: TypeReference): Stri } } typeReference.sizeConstraint?.let { constraint -> - val property = if (typeReference.type is StringType) "length" else "size" + val accessor = if (typeReference.type is StringType) "codePointCount(0, it.length)" else "size" constraint.lowerBound?.let { - add("it.${property} >= ${constraint.lowerBound}") + add("it.${accessor} >= ${constraint.lowerBound}") } constraint.upperBound?.let { - add("it.${property} <= ${constraint.upperBound}") + add("it.${accessor} <= ${constraint.upperBound}") } } typeReference.patternConstraint?.let { constraint -> diff --git a/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/consumer/Consumer.kt b/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/consumer/Consumer.kt index ea986154..55555784 100644 --- a/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/consumer/Consumer.kt +++ b/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/consumer/Consumer.kt @@ -64,7 +64,7 @@ class GreeterEndpointImpl(private val baseUrl: String) : tools.samt.client.gener appendPathSegments("all", encodeSlash = true) // Encode query parameters - this.parameters.append("names", (JsonArray(names.map { it?.let { it -> JsonPrimitive(it.also { require(it.length >= 1 && it.length <= 50) }) } ?: JsonNull })).toString()) + this.parameters.append("names", (JsonArray(names.map { it?.let { it -> JsonPrimitive(it.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }) } ?: JsonNull })).toString()) } contentType(ContentType.Application.Json) this.method = HttpMethod.Get @@ -101,7 +101,7 @@ class GreeterEndpointImpl(private val baseUrl: String) : tools.samt.client.gener val bodyAsText = `client response`.bodyAsText() val jsonElement = Json.parseToJsonElement(bodyAsText) - jsonElement.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 100) } + jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 100) } } override fun allTheTypes(long: Long, float: Float, double: Double, decimal: java.math.BigDecimal, boolean: Boolean, date: java.time.LocalDate, dateTime: java.time.LocalDateTime, duration: java.time.Duration): Unit = runBlocking { diff --git a/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/greeter/KtorMappings.kt b/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/greeter/KtorMappings.kt index f6c75715..e269a0a8 100644 --- a/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/greeter/KtorMappings.kt +++ b/codegen/src/test/resources/generator-test-model/out/ktor-client/tools/samt/client/generated/greeter/KtorMappings.kt @@ -15,7 +15,7 @@ fun `encode Greeting`(record: tools.samt.client.generated.greeter.Greeting): Jso // Encode field message val `field message` = run { val value = record.message - JsonPrimitive(value.also { require(it.length <= 128) }) + JsonPrimitive(value.also { require(it.codePointCount(0, it.length) <= 128) }) } // Create JSON for tools.samt.greeter.Greeting return buildJsonObject { @@ -27,7 +27,7 @@ fun `decode Greeting`(json: JsonElement): tools.samt.client.generated.greeter.Gr // Decode field message val `field message` = run { val jsonElement = json.jsonObject["message"]!! - jsonElement.jsonPrimitive.content.also { require(it.length <= 128) } + jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) <= 128) } } // Create record tools.samt.greeter.Greeting return tools.samt.client.generated.greeter.Greeting( @@ -103,10 +103,10 @@ fun `decode GreetingType`(json: JsonElement): tools.samt.client.generated.greete /** Encode alias tools.samt.greeter.ID to JSON */ fun `encode ID`(value: tools.samt.client.generated.greeter.ID): JsonElement = - value?.let { value -> JsonPrimitive(value.also { require(it.length >= 1 && it.length <= 50) }) } ?: JsonNull + value?.let { value -> JsonPrimitive(value.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }) } ?: JsonNull /** Decode alias tools.samt.greeter.ID from JSON */ fun `decode ID`(json: JsonElement): String? { if (json is JsonNull) return null - return json.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) } + return json.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) } } diff --git a/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/GreeterEndpoint.kt b/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/GreeterEndpoint.kt index 3cccb284..1ecdd628 100644 --- a/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/GreeterEndpoint.kt +++ b/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/GreeterEndpoint.kt @@ -45,7 +45,7 @@ fun Routing.routeGreeterEndpoint( val `parameter name` = run { // Read from path val jsonElement = call.parameters["name"]!!.toJson() - jsonElement.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) } + jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) } } // Decode parameter type @@ -75,7 +75,7 @@ fun Routing.routeGreeterEndpoint( val `parameter names` = run { // Read from queryParameter val jsonElement = call.request.queryParameters["names"]!!.toJson() - jsonElement.jsonArray.map { it.takeUnless { it is JsonNull }?.let { it.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) } } } + jsonElement.jsonArray.map { it.takeUnless { it is JsonNull }?.let { it.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) } } } } // Call user provided implementation @@ -105,7 +105,7 @@ fun Routing.routeGreeterEndpoint( val value = greeter.greeting(`parameter who`) // Encode response - val response = JsonPrimitive(value.also { require(it.length >= 1 && it.length <= 100) }) + val response = JsonPrimitive(value.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 100) }) // Return response with 200 OK call.respond(HttpStatusCode.OK, response) diff --git a/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/KtorMappings.kt b/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/KtorMappings.kt index 97b53e06..4a917568 100644 --- a/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/KtorMappings.kt +++ b/codegen/src/test/resources/generator-test-model/out/ktor-server/tools/samt/server/generated/greeter/KtorMappings.kt @@ -15,7 +15,7 @@ fun `encode Greeting`(record: tools.samt.server.generated.greeter.Greeting): Jso // Encode field message val `field message` = run { val value = record.message - JsonPrimitive(value.also { require(it.length <= 128) }) + JsonPrimitive(value.also { require(it.codePointCount(0, it.length) <= 128) }) } // Create JSON for tools.samt.greeter.Greeting return buildJsonObject { @@ -27,7 +27,7 @@ fun `decode Greeting`(json: JsonElement): tools.samt.server.generated.greeter.Gr // Decode field message val `field message` = run { val jsonElement = json.jsonObject["message"]!! - jsonElement.jsonPrimitive.content.also { require(it.length <= 128) } + jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) <= 128) } } // Create record tools.samt.greeter.Greeting return tools.samt.server.generated.greeter.Greeting( @@ -103,10 +103,10 @@ fun `decode GreetingType`(json: JsonElement): tools.samt.server.generated.greete /** Encode alias tools.samt.greeter.ID to JSON */ fun `encode ID`(value: tools.samt.server.generated.greeter.ID): JsonElement = - value?.let { value -> JsonPrimitive(value.also { require(it.length >= 1 && it.length <= 50) }) } ?: JsonNull + value?.let { value -> JsonPrimitive(value.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }) } ?: JsonNull /** Decode alias tools.samt.greeter.ID from JSON */ fun `decode ID`(json: JsonElement): String? { if (json is JsonNull) return null - return json.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) } + return json.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) } }