Skip to content

Commit

Permalink
Merge pull request #1302 from znsio/discriminator_allOf_error_messages
Browse files Browse the repository at this point in the history
Discriminator all of error messages
  • Loading branch information
joelrosario authored Sep 16, 2024
2 parents 503f591 + b7fcb2f commit 712f723
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
39 changes: 28 additions & 11 deletions core/src/main/kotlin/io/specmatic/core/HttpRequestPattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.specmatic.core.Result.Success
import io.specmatic.core.pattern.*
import io.specmatic.core.value.StringValue
import io.ktor.util.*
import io.specmatic.core.utilities.Flags
import io.specmatic.core.utilities.Flags.Companion.EXTENSIBLE_QUERY_PARAMS
import io.specmatic.core.value.JSONObjectValue
import io.specmatic.core.value.Value

Expand Down Expand Up @@ -229,7 +231,17 @@ data class HttpRequestPattern(
private fun matchQuery(parameters: Triple<HttpRequest, Resolver, List<Failure>>): MatchingResult<Triple<HttpRequest, Resolver, List<Failure>>> {
val (httpRequest, resolver, failures) = parameters

val result = httpQueryParamPattern.matches(httpRequest, resolver)
val updatedResolver =
if(Flags.getBooleanValue(EXTENSIBLE_QUERY_PARAMS))
resolver.copy(
findKeyErrorCheck = resolver.findKeyErrorCheck.copy(
unexpectedKeyCheck = IgnoreUnexpectedKeys
)
)
else
resolver

val result = httpQueryParamPattern.matches(httpRequest, updatedResolver)

return if (result is Failure)
MatchSuccess(Triple(httpRequest, resolver, failures.plus(result)))
Expand Down Expand Up @@ -370,25 +382,30 @@ data class HttpRequestPattern(

val matchResult = Result.fromResults(results)

if(matchResult is Failure)
if (matchResult is Failure)
throw ContractException(matchResult.toFailureReport())

paramsUnaccountedFor.map { (name, values) ->
val pattern = if (values.size > 1) {
QueryParameterArrayPattern(values.map { ExactValuePattern(StringValue(it.second)) }, name)
} else {
QueryParameterScalarPattern(ExactValuePattern(StringValue(values.single().second)))
}

name to pattern
}.toMap()
unaccountedQueryParamsToMap(paramsUnaccountedFor)
} else if(Flags.getBooleanValue(EXTENSIBLE_QUERY_PARAMS)) {
unaccountedQueryParamsToMap(paramsUnaccountedFor)
} else {
emptyMap()
}

return paramsWithinPattern + paramsOutsidePattern
}

private fun unaccountedQueryParamsToMap(paramsUnaccountedFor: Map<String, List<Pair<String, String>>>) =
paramsUnaccountedFor.map { (name, values) ->
val pattern = if (values.size > 1) {
QueryParameterArrayPattern(values.map { ExactValuePattern(StringValue(it.second)) }, name)
} else {
QueryParameterScalarPattern(ExactValuePattern(StringValue(values.single().second)))
}

name to pattern
}.toMap()

private fun encompassedType(valueString: String, key: String?, type: Pattern, resolver: Resolver): Pattern {
return when {
isPatternToken(valueString) -> resolvedHop(parsedPattern(valueString, key), resolver).let { parsedType ->
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/io/specmatic/core/utilities/Flags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Flags {
const val SPECMATIC_PRETTY_PRINT = "SPECMATIC_PRETTY_PRINT"
const val EXAMPLE_DIRECTORIES = "EXAMPLE_DIRECTORIES"

const val EXTENSIBLE_QUERY_PARAMS = "EXTENSIBLE_QUERY_PARAMS"

fun getStringValue(flagName: String): String? = System.getenv(flagName) ?: System.getProperty(flagName)

fun getBooleanValue(flagName: String, default: Boolean = false) = getStringValue(flagName)?.toBoolean() ?: default
Expand Down

0 comments on commit 712f723

Please sign in to comment.