Skip to content

Commit 014b88b

Browse files
authored
Merge pull request #54 from AVSystem/fixes-before-0.0.2
fix: correct baseUrl interpolation and spurious body null checks
2 parents 8b718bd + 7b80eda commit 014b88b

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

core/src/main/kotlin/com/avsystem/justworks/core/gen/client/BodyGenerator.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ internal object BodyGenerator {
8282
beginControlFlow("$CLIENT.%M(%L)", httpMethodFun, urlString)
8383
addCommonRequestParts(params)
8484

85-
optionalGuard(endpoint.requestBody?.required ?: false, BODY) {
86-
addStatement("%M(%T.Json)", CONTENT_TYPE_FUN, CONTENT_TYPE_APPLICATION)
87-
addStatement("%M(%L)", SET_BODY_FUN, BODY)
85+
if (endpoint.requestBody != null) {
86+
optionalGuard(endpoint.requestBody.required, BODY) {
87+
addStatement("%M(%T.Json)", CONTENT_TYPE_FUN, CONTENT_TYPE_APPLICATION)
88+
addStatement("%M(%L)", SET_BODY_FUN, BODY)
89+
}
8890
}
8991

9092
// Don't endControlFlow here — the outer buildFunctionBody closes with .toResult()
@@ -198,7 +200,7 @@ internal object BodyGenerator {
198200
private fun buildUrlString(endpoint: Endpoint, params: Map<ParameterLocation, List<Parameter>>): CodeBlock {
199201
val (format, args) = params[ParameterLocation.PATH]
200202
.orEmpty()
201-
.fold($$"${'$'}{$$BASE_URL}" + endpoint.path to emptyList<Any>()) { (format, args), param ->
203+
.fold($$"${%L}" + endpoint.path to listOf<Any>(BASE_URL)) { (format, args), param ->
202204
format.replace("{${param.name}}", $$"${%M(%L)}") to args + ENCODE_PARAM_FUN + param.name.toCamelCase()
203205
}
204206
return CodeBlock.of("%P", CodeBlock.of(format, *args.toTypedArray<Any>()))

core/src/test/kotlin/com/avsystem/justworks/core/gen/ClientGeneratorTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,42 @@ class ClientGeneratorTest {
536536
assertFalse(body.contains("submitForm"), "Should NOT contain submitForm for JSON")
537537
}
538538

539+
// -- No body: endpoint without requestBody should not emit setBody or contentType --
540+
541+
@Test
542+
fun `endpoint without requestBody does not generate body null check`() {
543+
val ep = endpoint(
544+
method = HttpMethod.GET,
545+
operationId = "listPets",
546+
requestBody = null,
547+
)
548+
val cls = clientClass(ep)
549+
val funSpec = cls.funSpecs.first { it.name == "listPets" }
550+
val body = funSpec.body.toString()
551+
assertFalse(body.contains("setBody"), "Should NOT contain setBody when no requestBody")
552+
assertFalse(body.contains("contentType"), "Should NOT set contentType when no requestBody")
553+
assertFalse(body.contains("if (body"), "Should NOT check body != null when no requestBody")
554+
}
555+
556+
// -- URL interpolation: baseUrl must be interpolated, not literal --
557+
558+
@Test
559+
fun `generated URL interpolates baseUrl property`() {
560+
val ep = endpoint(
561+
path = "/pets/{petId}",
562+
operationId = "getPet",
563+
parameters = listOf(
564+
Parameter("petId", ParameterLocation.PATH, true, TypeRef.Primitive(PrimitiveType.LONG), null),
565+
),
566+
)
567+
val cls = clientClass(ep)
568+
val funSpec = cls.funSpecs.first { it.name == "getPet" }
569+
val body = funSpec.body.toString()
570+
// Must contain ${baseUrl} as interpolation, not ${'$'}{baseUrl} (escaped/literal)
571+
assertTrue(body.contains("\${baseUrl}"), "Expected \${baseUrl} interpolation in URL")
572+
assertFalse(body.contains("\${'$'}{baseUrl}"), "baseUrl must not be escaped as literal text")
573+
}
574+
539575
// -- CONT-02: Form-urlencoded code generation --
540576

541577
@Test

0 commit comments

Comments
 (0)