forked from commercetools/rmf-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'commercetools:main' into feat/go-add-404
- Loading branch information
Showing
10 changed files
with
135 additions
and
24 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
ctp-validators/src/main/kotlin/com/commercetools/rmf/validators/MethodResponseRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.commercetools.rmf.validators | ||
|
||
import com.commercetools.rmf.diff.resource | ||
import io.vrap.rmf.raml.model.resources.HttpMethod | ||
import io.vrap.rmf.raml.model.resources.Method | ||
import io.vrap.rmf.raml.model.resources.Resource | ||
import io.vrap.rmf.raml.model.resources.ResourceType | ||
import org.eclipse.emf.common.util.Diagnostic | ||
import java.util.* | ||
|
||
@ValidatorSet | ||
class MethodResponseRule(severity: RuleSeverity, options: List<RuleOption>? = null) : ResolvedResourcesRule(severity, options) { | ||
|
||
private val exclude: List<String> = | ||
(options?.filter { ruleOption -> ruleOption.type.lowercase(Locale.getDefault()) == RuleOptionType.EXCLUDE.toString() }?.map { ruleOption -> ruleOption.value }?.plus("") ?: defaultExcludes) | ||
|
||
override fun caseMethod(method: Method): List<Diagnostic> { | ||
val validationResults: MutableList<Diagnostic> = ArrayList() | ||
|
||
if (method.eContainer() is ResourceType) { | ||
return validationResults | ||
} | ||
if (method.responses.isEmpty() && exclude.contains("${method.method.name} ${(method.eContainer() as Resource).fullUri.template}").not()) { | ||
validationResults.add(error(method, "Method \"{0} {1}\" must have at least one response defined", method.method.name, (method.eContainer() as Resource).fullUri.template)) | ||
} | ||
return validationResults | ||
} | ||
|
||
companion object : ValidatorFactory<MethodResponseRule> { | ||
private val defaultExcludes by lazy { listOf("") } | ||
|
||
@JvmStatic | ||
override fun create(options: List<RuleOption>?): MethodResponseRule { | ||
return MethodResponseRule(RuleSeverity.ERROR, options) | ||
} | ||
|
||
@JvmStatic | ||
override fun create(severity: RuleSeverity, options: List<RuleOption>?): MethodResponseRule { | ||
return MethodResponseRule(severity, options) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
ctp-validators/src/test/resources/method-response-rule.raml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#%RAML 1.0 | ||
title: method responses | ||
|
||
resourceTypes: | ||
test: | ||
get?: | ||
responses: | ||
200: | ||
post?: | ||
/carts: | ||
get: | ||
description: test | ||
responses: | ||
200: | ||
head: | ||
responses: | ||
200: | ||
404: | ||
/invalid: | ||
head: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters