Skip to content

Commit

Permalink
Merge pull request #1480 from ktsypkina/main
Browse files Browse the repository at this point in the history
Address issue #1445 and fix failed test
  • Loading branch information
ktsypkina authored Mar 5, 2024
2 parents 9157547 + ae1d160 commit 8c7407d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion server/zally-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 8c7407d

Please sign in to comment.