From 57913391d8d04eebd1526cd79aec042cf81f5c84 Mon Sep 17 00:00:00 2001 From: ktsypkina Date: Mon, 19 Feb 2024 20:22:03 +0100 Subject: [PATCH 1/5] fix failed test --- server/zally-core/build.gradle.kts | 2 +- .../kotlin/org/zalando/zally/core/DefaultContextFactory.kt | 7 ++++++- .../org/zalando/zally/apireview/RestApiViolationsTest.kt | 6 ++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/server/zally-core/build.gradle.kts b/server/zally-core/build.gradle.kts index 9c36281ad..4372f8f0a 100644 --- a/server/zally-core/build.gradle.kts +++ b/server/zally-core/build.gradle.kts @@ -2,7 +2,7 @@ dependencies { kapt("com.google.auto.service:auto-service:1.0.1") api(project(":zally-rule-api")) - api("io.swagger.parser.v3:swagger-parser:2.1.9") + api("io.swagger.parser.v3:swagger-parser:2.1.12") api("io.github.config4k:config4k:0.5.0") implementation("com.google.auto.service:auto-service:1.0.1") diff --git a/server/zally-core/src/main/kotlin/org/zalando/zally/core/DefaultContextFactory.kt b/server/zally-core/src/main/kotlin/org/zalando/zally/core/DefaultContextFactory.kt index fa379b540..6013d2e88 100644 --- a/server/zally-core/src/main/kotlin/org/zalando/zally/core/DefaultContextFactory.kt +++ b/server/zally-core/src/main/kotlin/org/zalando/zally/core/DefaultContextFactory.kt @@ -102,7 +102,12 @@ class DefaultContextFactory( val parseResult = OpenAPIV3Parser().readContents(content, authorizationValue, parseOptions) return if (parseResult.openAPI === null) { - if (parseResult.messages.isEmpty() || parseResult.messages.contains("attribute openapi is missing")) { + val openApiIsMissing = parseResult.messages.contains("attribute openapi is missing") + val errorConstructingInstance = parseResult.messages.any { + it.contains("Cannot construct instance", ignoreCase = true) + } + + if (parseResult.messages.isEmpty() || openApiIsMissing || errorConstructingInstance) { ContentParseResult.NotApplicable() } else { ContentParseResult.ParsedWithErrors(parseResult.messages.filterNotNull().map(::errorToViolation)) diff --git a/server/zally-server/src/test/kotlin/org/zalando/zally/apireview/RestApiViolationsTest.kt b/server/zally-server/src/test/kotlin/org/zalando/zally/apireview/RestApiViolationsTest.kt index dd3cd775a..891de6fdc 100644 --- a/server/zally-server/src/test/kotlin/org/zalando/zally/apireview/RestApiViolationsTest.kt +++ b/server/zally-server/src/test/kotlin/org/zalando/zally/apireview/RestApiViolationsTest.kt @@ -118,14 +118,16 @@ class RestApiViolationsTest : RestApiBaseTest() { @Test fun shouldRespondWithViolationWhenApiDefinitionFieldIsNotValidSwaggerDefinition() { + val rule101Link = "https://zalando.github.io/restful-api-guidelines/#101" val response = sendApiDefinition( ApiDefinitionRequest.fromJson("\"no swagger definition\"") ) assertThat(response.violations).hasSize(5) assertThat(response.violations[0].title).isEqualTo("provide API specification using OpenAPI") - // TODO: fails with exception after switch to swagger-parser:2.1.9 - // assertThat(response.violations[0].description).isEqualTo("attribute openapi is not of type `object`") + // the input is just a string, it has no attributes + assertThat(response.violations[0].description).isEqualTo("attribute is not of type `object`") + assertThat(response.violations[0].ruleLink).isEqualTo(rule101Link) assertThat(response.violations[1].title).isEqualTo("TestCheckIsOpenApi3") assertThat(response.externalId).isNotNull() } From dd16142bad97f76bad45186629f9c726cf1e1fd8 Mon Sep 17 00:00:00 2001 From: ktsypkina Date: Tue, 20 Feb 2024 11:49:15 +0100 Subject: [PATCH 2/5] add test of ObjectMapper failure on non parsable context --- .../org/zalando/zally/core/DefaultContextFactoryTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt index 9f3da8f4e..7e7ce9ba9 100644 --- a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt +++ b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt @@ -14,6 +14,15 @@ class DefaultContextFactoryTest { // OPEN API // + @Test + fun `OPEN API -- not applicable when content is a String without any properties`() { + @Language("YAML") + val content = "Pets API".trimIndent() + val result = defaultContextFactory.parseOpenApiContext(content) + + assertThat(result).resultsInNotApplicable() + } + @Test fun `OPEN API -- not applicable when content does not contain the openapi property`() { @Language("YAML") From ff94ac5c8b68b86c8f9fbf141f29a77b54e1713c Mon Sep 17 00:00:00 2001 From: ktsypkina Date: Mon, 4 Mar 2024 12:34:22 +0100 Subject: [PATCH 3/5] add test to cover parsed with errors case --- .../zalando/zally/core/DefaultContextFactoryTest.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt index 7e7ce9ba9..071fc25f7 100644 --- a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt +++ b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt @@ -23,6 +23,16 @@ class DefaultContextFactoryTest { assertThat(result).resultsInNotApplicable() } + @Test + fun `OPEN API -- Openapi property is expected to be String according to the OpenAPI3 spec`() { + @Language("YAML") + val content = """ + openapi: {la: 3.0.0} + """ + val result = defaultContextFactory.parseOpenApiContext(content) + assertThat(result).resultsInParsedWithErrors() + } + @Test fun `OPEN API -- not applicable when content does not contain the openapi property`() { @Language("YAML") From 083810f1f12153e64117f8de964116ab0e5fc6af Mon Sep 17 00:00:00 2001 From: ktsypkina Date: Mon, 4 Mar 2024 12:50:33 +0100 Subject: [PATCH 4/5] add expected violation to test assert --- .../org/zalando/zally/core/DefaultContextFactoryTest.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt index 071fc25f7..ba20f2f9c 100644 --- a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt +++ b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt @@ -30,7 +30,12 @@ class DefaultContextFactoryTest { openapi: {la: 3.0.0} """ val result = defaultContextFactory.parseOpenApiContext(content) - assertThat(result).resultsInParsedWithErrors() + assertThat(result).resultsInErrors( + Violation( + description = "attribute openapi is not of type `string`", + pointer = JsonPointer.empty() + ) + ) } @Test From ae1d1604f73fa02860ec21bba405ac532191a670 Mon Sep 17 00:00:00 2001 From: ktsypkina Date: Mon, 4 Mar 2024 13:11:19 +0100 Subject: [PATCH 5/5] fix the build --- .../kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt index ba20f2f9c..bdff7ce6f 100644 --- a/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt +++ b/server/zally-core/src/test/kotlin/org/zalando/zally/core/DefaultContextFactoryTest.kt @@ -1,10 +1,12 @@ package org.zalando.zally.core +import com.fasterxml.jackson.core.JsonPointer import org.assertj.core.api.Assertions.assertThat import org.intellij.lang.annotations.Language import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.zalando.zally.core.ContentParseResultAssert.Companion.assertThat +import org.zalando.zally.rule.api.Violation class DefaultContextFactoryTest {