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

Parse unreferenced and indirectly referenced schemas to patterns. #1432

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,32 @@ class OpenApiSpecification(
val name = File(openApiFilePath).name

val (scenarioInfos, stubsFromExamples) = toScenarioInfos()
val unreferencedSchemaPatterns = parseUnreferencedSchemas()
val updatedScenarios = scenarioInfos.map {
Scenario(it).copy(
dictionary = dictionary,
attributeSelectionPattern = specmaticConfig.attributeSelectionPattern,
patterns = it.patterns + unreferencedSchemaPatterns
)
}

return Feature(
scenarioInfos.map { Scenario(it).copy(dictionary = dictionary, attributeSelectionPattern = specmaticConfig.attributeSelectionPattern) }, name = name, path = openApiFilePath, sourceProvider = sourceProvider,
updatedScenarios, name = name, path = openApiFilePath, sourceProvider = sourceProvider,
sourceRepository = sourceRepository,
sourceRepositoryBranch = sourceRepositoryBranch,
specification = specificationPath,
serviceType = SERVICE_TYPE_HTTP,
stubsFromExamples = stubsFromExamples,
specmaticConfig = specmaticConfig
specmaticConfig = specmaticConfig,
)
}

private fun parseUnreferencedSchemas(): Map<String, Pattern> {
return openApiSchemas().filterNot { withPatternDelimiters(it.key) in patterns }.map {
withPatternDelimiters(it.key) to toSpecmaticPattern(it.value, emptyList())
}.toMap()
}

override fun toScenarioInfos(): Pair<List<ScenarioInfo>, Map<String, List<Pair<HttpRequest, HttpResponse>>>> {
val (
scenarioInfos: List<ScenarioInfo>,
Expand Down Expand Up @@ -791,6 +805,8 @@ class OpenApiSpecification(

private fun openApiPaths() = parsedOpenApi.paths.orEmpty()

private fun openApiSchemas() = parsedOpenApi.components?.schemas.orEmpty()

private fun isNumber(value: String): Boolean {
return value.toIntOrNull() != null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9560,6 +9560,64 @@ paths:
}
}

@Test
fun `should include unreferenced or indirectly referenced schema patterns`() {
val specContent = """
openapi: 3.0.3
info:
title: Products API
version: 1.0.0
paths:
/products:
get:
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: array
items:
${'$'}ref: '#/components/schemas/ExtendedDetails'
components:
schemas:
User:
type: object
properties:
name:
type: string
ExtendedDetails:
allOf:
- ${'$'}ref: '#/components/schemas/BaseDetails'
- ${'$'}ref: '#/components/schemas/User'
BaseDetails:
type: object
properties:
id:
type: string
email:
type: string
required:
- id
Address:
type: object
properties:
street:
type: string
city:
type: string
""".trimIndent()
val feature = OpenApiSpecification.fromYAML(specContent, "").toFeature()
val directlyNonReferencedPatterns = listOf("(BaseDetails)", "(User)", "(Address)")

assertThat(feature.scenarios).allSatisfy { scenario ->
directlyNonReferencedPatterns.forEach {
assertThat(scenario.patterns).containsKey(it)
assertThat(scenario.resolver.newPatterns).containsKey(it)
}
}
}

private fun ignoreButLogException(function: () -> OpenApiSpecification) {
try {
function()
Expand Down