Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue with invalid example value generation for an array based query parameter #1465

Merged
merged 6 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions core/src/main/kotlin/io/specmatic/core/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.ktor.http.*
import io.ktor.http.content.*
import io.specmatic.core.utilities.Flags.Companion.SPECMATIC_PRETTY_PRINT
import io.specmatic.core.utilities.Flags.Companion.getBooleanValue
import org.apache.http.HttpHeaders.AUTHORIZATION
import org.apache.http.client.utils.URLEncodedUtils
import org.apache.http.message.BasicNameValuePair
import java.io.File
Expand Down Expand Up @@ -130,7 +129,7 @@ data class HttpRequest(
method?.let { requestMap["method"] = StringValue(it) }
?: throw ContractException("Can't serialise the request without a method.")

setIfNotEmpty(requestMap, "query", queryParams.asMap())
setIfNotEmpty(requestMap, "query", queryParams.asJsonMap())
setIfNotEmpty(requestMap, "headers", headers)

when {
Expand Down Expand Up @@ -387,9 +386,17 @@ data class HttpRequest(
}
}

private fun setIfNotEmpty(dest: MutableMap<String, Value>, key: String, data: Map<String, String>) {
private fun setIfNotEmpty(dest: MutableMap<String, Value>, key: String, data: Map<String, Any>) {
if (data.isNotEmpty())
dest[key] = JSONObjectValue(data.mapValues { StringValue(it.value) })
dest[key] = JSONObjectValue(data.mapValues {
if (it.value is List<*>) {
JSONArrayValue((it.value as List<*>).map { listValue ->
StringValue(listValue.toString())
})
} else {
StringValue(it.value.toString())
}
})
}

fun nativeString(json: Map<String, Value>, key: String): String? {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/kotlin/io/specmatic/core/QueryParameters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ data class QueryParameters(val paramPairs: List<Pair<String, String>> = emptyLis
}.toMap()
}

fun asJsonMap(): Map<String, Any> {
return paramPairs.groupBy { it.first }.map { (parameterName, parameterValues) ->
if (parameterValues.size > 1) {
parameterName to parameterValues.map { it.second }
} else {
parameterName to parameterValues.single().second
}
}.toMap()
}

fun getValues(key: String): List<String> {
return paramPairs.filter { it.first == key }.map { it.second }
}
Expand Down
Loading