Skip to content

Commit

Permalink
Merge pull request #1432 from znsio/unref-schemas
Browse files Browse the repository at this point in the history
Parse unreferenced and indirectly referenced schemas to patterns.
  • Loading branch information
joelrosario authored Nov 18, 2024
2 parents 3428447 + f386bde commit afc03f3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
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

0 comments on commit afc03f3

Please sign in to comment.