diff --git a/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt b/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt index 3268c626e..231e97222 100644 --- a/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt +++ b/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt @@ -6,10 +6,12 @@ import `in`.specmatic.core.git.GitCommand import `in`.specmatic.core.git.SystemGit import `in`.specmatic.core.testBackwardCompatibility import `in`.specmatic.core.utilities.exitWithMessage +import kotlinx.serialization.json.Json import org.springframework.stereotype.Component import picocli.CommandLine.Command import java.io.File import java.util.concurrent.Callable +import java.util.regex.Pattern @Component @Command( @@ -17,7 +19,7 @@ import java.util.concurrent.Callable mixinStandardHelpOptions = true, description = ["Checks backward compatibility of a directory across the current HEAD and the main branch"] ) -open class BackwardCompatibilityCheckCommand( +class BackwardCompatibilityCheckCommand( private val gitCommand: GitCommand = SystemGit(), ) : Callable { @@ -120,13 +122,17 @@ open class BackwardCompatibilityCheckCommand( println() } - internal fun filesReferringToChangedSchemaFiles(schemaFiles: Set): Set { - if (schemaFiles.isEmpty()) return emptySet() + internal fun filesReferringToChangedSchemaFiles(inputFiles: Set): Set { + if (inputFiles.isEmpty()) return emptySet() - val schemaFileBaseNames = schemaFiles.map { File(it).name } + val inputFileNames = inputFiles.map { File(it).name } val result = allOpenApiSpecFiles().filter { - it.readText().let { specContent -> - schemaFileBaseNames.any { schemaFileBaseName -> schemaFileBaseName in specContent } + it.readText().trim().let { specContent -> + inputFileNames.any { inputFileName -> + val pattern = Pattern.compile("\\b$inputFileName\\b") + val matcher = pattern.matcher(specContent) + matcher.find() + } } }.map { it.path }.toSet() diff --git a/application/src/test/kotlin/application/BackwardCompatibilityCheckCommandTest.kt b/application/src/test/kotlin/application/BackwardCompatibilityCheckCommandTest.kt index 71bc9b104..b57ea19f0 100644 --- a/application/src/test/kotlin/application/BackwardCompatibilityCheckCommandTest.kt +++ b/application/src/test/kotlin/application/BackwardCompatibilityCheckCommandTest.kt @@ -40,10 +40,36 @@ class BackwardCompatibilityCheckCommandTest { assertEquals(setOf("file1.yaml"), result) } + @Test + fun `filesReferringToChangedSchemaFiles returns set of files which are referring to a changed schema that is one level down`() { + val command = spyk() + every { command.allOpenApiSpecFiles() } returns listOf( + File("file1.yaml").apply { referTo("schema_file1.yaml") }, + File("schema_file2.yaml").apply { referTo("schema_file1.yaml") }, // schema within a schema + File("file2.yaml").apply { referTo("schema_file2.yaml") } + ) + val result = command.filesReferringToChangedSchemaFiles(setOf("schema_file1.yaml")) + assertEquals(setOf("file1.yaml", "file2.yaml"), result) + } + @AfterEach fun `cleanup files`() { - listOf(File("file1.yaml"), File("file2.yaml")).forEach { - it.delete() + listOf("file1.yaml", "file2.yaml", "file3.yaml", "file4.yaml", "schema_file1.yaml", "schema_file2.yaml").forEach { + File(it).delete() } } + + private fun File.referTo(schemaFileName: String) { + val specContent = """ + openapi: 3.1.0 # OpenAPI version specified here + info: + title: My API + version: 1.0.0 + components: + schemas: + User: + ${"$"}ref: '#/components/schemas/$schemaFileName' + """.trimIndent() + this.writeText(specContent) + } } \ No newline at end of file diff --git a/version.properties b/version.properties index b6d155129..e8bce3fbb 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version=1.3.22 +version=1.3.23