-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#323] Validation of $ref property values should show error on unexpe… #342
Conversation
@ghillairet , I used this spec: openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
paths: {}
components:
schemas:
MyType2:
type: array
items:
$ref: "#/components/schemas/MyType1"
MyType1:
required:
- property
properties:
property:
type: integer
format: int64 |
|
||
if (valueNode != null && type.getPointer() != null && valueNode.getType() != null | ||
&& !type.getPointer().equals(valueNode.getType().getPointer())) { | ||
errors.add(error(nodeValue, IMarker.SEVERITY_ERROR, Messages.error_invalid_reference_type)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be really helpful to provide what type is actually expected. It looks like valueNode.getType().getPointer()
can provide this info
Ok I'll fix the array and add the type in the error. |
TypeDefinition type = node.getType(); | ||
|
||
AbstractNode nodeValue = node.get(JsonReference.PROPERTY); | ||
AbstractNode valueNode = model.find((String) nodeValue.asValue().getValue()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only works for local references. It throws and exception for external references:
- From the same workspace (local external reference via relative path)
- Or, remove (accessed via HTTP/HTTPS/any other protocol)
Because it throws an exception which is never processed, the exception is propagated to the top level com.reprezen.swagedit.core.validation.Validator.validate(JsonDocument, URI)
and it means that validation errors are not processed at all.
@ghillairet , this code works well with references to the elements from the same file. However, I found a severe problem with JSON pointers which are not starting with a "/". So, if such pointer is present in a spec, validation does NOT work at all. You can type anything and it will be accepted by the validator, see my comment #342 (comment)
All these cases are caused by the same line of code. But we should test them (preferably with automated tests) separately after fixing it. java.lang.IllegalArgumentException: Invalid input: JSON Pointer expression must start with '/': "./components/uber_path_products.yaml#/paths/~1products2"
at com.fasterxml.jackson.core.JsonPointer.compile(JsonPointer.java:122)
at com.fasterxml.jackson.core.JsonPointer.valueOf(JsonPointer.java:131)
at com.reprezen.swagedit.core.model.Model.find(Model.java:222)
at com.reprezen.swagedit.core.validation.Validator.checkReferenceType(Validator.java:305)
at com.reprezen.swagedit.core.validation.Validator.validateModel(Validator.java:175)
at com.reprezen.swagedit.core.validation.Validator.validate(Validator.java:118)
at com.reprezen.swagedit.core.validation.Validator.validate(Validator.java:100) |
I added more tests for invalid refs, so the old tests + new tests cover these variations of invalid refs:
The tests check that these scenarios don't break the validation |
… show error on unexpected object type
There is still an issue with validation of refs inside the same workspace/project, I'm trying to fix that now. |
…cted object type Disable type validation of externals refs.
} | ||
} | ||
return errors; | ||
} | ||
|
||
protected void executeModelValidation(Model model, AbstractNode node, Set<SwaggerError> errors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest validateNode
as method name
nodes.add(node); | ||
} | ||
} | ||
final List<AbstractNode> nodes = model.findByType(operationPointer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
|
||
document.set(content) | ||
val errors = validator.validate(document, null as URI) | ||
assertEquals(0, errors.size()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:) thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed code review and QA
…cted object type
See #323
Probably needs more tests.