Skip to content

Commit

Permalink
merging main with branch
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan83 committed Jan 3, 2024
2 parents 43a9f97 + f249fdc commit c218632
Show file tree
Hide file tree
Showing 13 changed files with 841 additions and 113 deletions.
6 changes: 3 additions & 3 deletions application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
implementation 'com.arakelian:java-jq:1.3.0'
testImplementation 'com.arakelian:java-jq:1.3.0'

implementation 'org.assertj:assertj-core:3.24.2'
implementation 'org.assertj:assertj-core:3.25.0'
implementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'

implementation('info.picocli:picocli-spring-boot-starter:4.7.5') {
Expand All @@ -55,7 +55,7 @@ dependencies {
implementation "org.eclipse.jgit:org.eclipse.jgit:$jgit_version"
implementation "org.eclipse.jgit:org.eclipse.jgit.ssh.apache:$jgit_version"

implementation 'org.slf4j:slf4j-nop:2.0.9'
implementation 'org.slf4j:slf4j-nop:2.0.10'

implementation 'org.apache.ant:ant-junit:1.10.14'

Expand All @@ -80,7 +80,7 @@ dependencies {
testImplementation('com.ninja-squad:springmockk:3.1.1') {
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation 'org.assertj:assertj-core:3.25.0'
testImplementation 'com.ginsberg:junit5-system-exit:1.1.2'
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"
Expand Down
4 changes: 2 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ dependencies {

implementation 'com.squareup.okhttp3:okhttp:4.12.0'
testImplementation 'org.wiremock:wiremock:3.3.1'
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation 'org.assertj:assertj-core:3.25.0'
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit_version"
testImplementation 'org.json:json:20231013'
testImplementation 'org.springframework:spring-web:5.3.31'
testImplementation 'io.mockk:mockk:1.13.2'
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation 'org.assertj:assertj-core:3.25.0'
testImplementation "io.ktor:ktor-client-mock-jvm:$ktor_version"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.swagger.v3.parser.core.models.SwaggerParseResult
import java.io.File

private const val BEARER_SECURITY_SCHEME = "bearer"
private const val SERVICE_TYPE_HTTP = "HTTP"
const val SERVICE_TYPE_HTTP = "HTTP"

private const val testDirectoryEnvironmentVariable = "SPECMATIC_TESTS_DIRECTORY"
private const val testDirectoryProperty = "specmaticTestsDirectory"
Expand Down Expand Up @@ -667,10 +667,37 @@ class OpenApiSpecification(private val openApiFilePath: String, private val pars
securitySchemes = operationSecuritySchemes(operation, securitySchemes)
)

val exampleQueryParams = namedExampleParams(operation, QueryParameter::class.java)
val examplePathParams = namedExampleParams(operation, PathParameter::class.java)
val exampleHeaderParams = namedExampleParams(operation, HeaderParameter::class.java)

val unionOfParameterKeys = (exampleQueryParams.keys + examplePathParams.keys + exampleHeaderParams.keys).distinct()

return when (val requestBody = resolveRequestBody(operation)) {
null -> listOf(
Pair(requestPattern, emptyMap())
)
null -> {
val examples: Map<String, List<HttpRequest>> = unionOfParameterKeys.map { exampleName ->
val queryParams = exampleQueryParams[exampleName] ?: emptyMap()
val pathParams = examplePathParams[exampleName] ?: emptyMap()
val headerParams = exampleHeaderParams[exampleName] ?: emptyMap()

val path = pathParams.entries.fold(urlMatcher.toOpenApiPath()) { acc, (key, value) ->
acc.replace("{$key}", value)
}

val httpRequest =
HttpRequest(method = httpMethod, path = path, queryParams = queryParams, headers = headerParams)

val requestsWithSecurityParams: List<HttpRequest> = securitySchemes.map { (_, securityScheme) ->
securityScheme.addTo(httpRequest)
}

exampleName to requestsWithSecurityParams
}.toMap()

listOf(
Pair(requestPattern, examples)
)
}
else -> {
requestBody.content.map { (contentType, mediaType) ->
when (contentType.lowercase()) {
Expand Down Expand Up @@ -727,9 +754,12 @@ class OpenApiSpecification(private val openApiFilePath: String, private val pars
} ?: emptyMap()

val examples: Map<String, List<HttpRequest>> = exampleBodies.map {
val queryParams = exampleQueryParams[it.key] ?: emptyMap()

val httpRequest = HttpRequest(
method = httpMethod,
path = urlMatcher.path,
queryParams = queryParams,
body = parsedValue(it.value ?: "")
)

Expand Down Expand Up @@ -758,6 +788,20 @@ class OpenApiSpecification(private val openApiFilePath: String, private val pars
contentType = contentType
)

private fun <T: Parameter> namedExampleParams(operation: Operation, parameterType: Class<T>): Map<String, Map<String, String>> = operation.parameters.orEmpty()
.filterIsInstance(parameterType)
.fold(emptyMap<String, Map<String, String>>()) { acc, parameter ->

parameter
.examples.orEmpty()
.entries.filter { it.value.value?.toString().orEmpty() !in OMIT }
.fold(acc) { acc, (exampleName, example) ->
val exampleValue = example.value?.toString() ?: ""
val exampleMap = acc[exampleName] ?: emptyMap()
acc.plus(exampleName to exampleMap.plus(parameter.name to exampleValue))
}
}

private fun resolveRequestBody(operation: Operation): RequestBody? =
operation.requestBody?.`$ref`?.let {
resolveReferenceToRequestBody(it).second
Expand Down Expand Up @@ -808,12 +852,18 @@ class OpenApiSpecification(private val openApiFilePath: String, private val pars
return BearerSecurityScheme(token)
}

private fun toFormFields(mediaType: MediaType) =
mediaType.schema.properties.map { (formFieldName, formFieldValue) ->
private fun toFormFields(mediaType: MediaType): Map<String, Pattern> {
val schema = mediaType.schema.`$ref`?.let {
val (_, resolvedSchema) = resolveReferenceToSchema(mediaType.schema.`$ref`)
resolvedSchema
} ?: mediaType.schema

return schema.properties.map { (formFieldName, formFieldValue) ->
formFieldName to toSpecmaticPattern(
formFieldValue, emptyList(), jsonInFormData = isJsonInString(mediaType, formFieldName)
)
}.toMap()
}

private fun isJsonInString(
mediaType: MediaType, formFieldName: String?
Expand Down
74 changes: 0 additions & 74 deletions core/src/test/kotlin/in/specmatic/conversions/ExamplesAsStub.kt

This file was deleted.

Loading

0 comments on commit c218632

Please sign in to comment.