Skip to content

Commit

Permalink
Merge pull request #1269 from zalando/gh-1268-fix-content-validation
Browse files Browse the repository at this point in the history
Fix `content` field validation
  • Loading branch information
vadeg authored Jun 24, 2021
2 parents e01adf2 + b138ca8 commit a343dbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class PathParameterRule {
if (context.isOpenAPI3()) {
return context.api.getAllParameters()
.filterValues {
val contentMap = it.content.orEmpty()
contentMap.isEmpty() || contentMap.size > 1
if (it.content != null) {
it.content.isEmpty() || it.content.size > 1
} else false
}
.map { (_, parameter) ->
context.violation(contentMapStructureErrorMessage(parameter.name), parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,50 @@ class PathParameterRuleTest {

val violations = rule.validateParameterContentMapStructure(context)

assertThat(violations).hasSize(2)
assertThat(violations).containsDescriptionsInAnyOrder(
PathParameterRule.contentMapStructureErrorMessage("X-HEADER-ID"),
PathParameterRule.contentMapStructureErrorMessage("item-id")
assertThat(violations).hasSize(1)
assertThat(violations).descriptionsAllEqualTo(
PathParameterRule.contentMapStructureErrorMessage("X-HEADER-ID")
)
}

@Test
fun `return no violations if 'content' is not defined`() {
@Language("YAML")
val context = DefaultContextFactory().getOpenApiContext(
"""
openapi: '3.0.0'
info:
title: Schema and content Parameter properties validation
contact:
info: "Team One"
paths:
/endpoint:
post:
summary: |
Some summary.
security:
- oauth2: ["uid"]
parameters:
- name: X-HEADER-ID
description: Header description
in: header
required: false
schema:
type: string
- $\ref: "#/components/parameters/QueryParameter"
components:
parameters:
QueryParameter:
name: item-id
in: path
description: The id of the pet to retrieve
schema:
type: string
""".trimIndent()
)

val violations = rule.validateParameterContentMapStructure(context)

assertThat(violations).isEmpty()
}
}

0 comments on commit a343dbe

Please sign in to comment.