Skip to content

Commit

Permalink
[#306] OpenAPI v3: Validation for Link#operationRef values
Browse files Browse the repository at this point in the history
Rename type pointers to more meaningfull names
  • Loading branch information
ghillairet committed Oct 13, 2017
1 parent 877a5fe commit 5068ec1
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import com.reprezen.swagedit.core.json.references.JsonReferenceValidator;
import com.reprezen.swagedit.core.model.AbstractNode;
import com.reprezen.swagedit.core.model.ValueNode;
import com.reprezen.swagedit.core.schema.TypeDefinition;
import com.reprezen.swagedit.core.validation.SwaggerError;

public class OpenApi3ReferenceValidator extends JsonReferenceValidator {

private final JsonPointer schemaPointer = JsonPointer.compile("/definitions/linkOrReference");
private final JsonPointer operationPointer = JsonPointer.compile("/definitions/operation");
private final JsonPointer linkTypePointer = JsonPointer.compile("/definitions/linkOrReference");
private final JsonPointer operationTypePointer = JsonPointer.compile("/definitions/operation");

public OpenApi3ReferenceValidator() {
super(new OpenApi3ReferenceFactory());
Expand All @@ -43,10 +44,9 @@ public OpenApi3ReferenceValidator() {
protected void validateType(JsonDocument doc, URI baseURI, AbstractNode node, JsonReference reference,
Set<SwaggerError> errors) {

if (schemaPointer.equals(node.getType().getPointer())) {
if (linkTypePointer.equals(node.getType().getPointer())) {
AbstractNode target = findTarget(doc, baseURI, reference);
boolean isValidType = target != null && target.getType() != null
&& Objects.equals(operationPointer, target.getType().getPointer());
boolean isValidType = isValidOperation(target);

if (!isValidType) {
errors.add(createReferenceError(SEVERITY_WARNING, error_invalid_operation_ref, reference));
Expand All @@ -56,6 +56,12 @@ protected void validateType(JsonDocument doc, URI baseURI, AbstractNode node, Js
}
}

protected boolean isValidOperation(AbstractNode operation) {
TypeDefinition type = operation != null ? operation.getType() : null;

return type != null && Objects.equals(operationTypePointer, type.getPointer());
}

public static class OpenApi3ReferenceFactory extends JsonReferenceFactory {

private static final String OPERATION_REF = "operationRef";
Expand Down

0 comments on commit 5068ec1

Please sign in to comment.