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-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..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 { @@ -14,6 +16,30 @@ 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 -- 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).resultsInErrors( + Violation( + description = "attribute openapi is not of type `string`", + pointer = JsonPointer.empty() + ) + ) + } + @Test fun `OPEN API -- not applicable when content does not contain the openapi property`() { @Language("YAML") 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() }