Skip to content
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

Merged
merged 18 commits into from
Jun 8, 2017

Conversation

ghillairet
Copy link
Member

…cted object type

See #323

Probably needs more tests.

@tfesenko
Copy link
Member

@ghillairet ,
This approach does not work for arrays:
screen shot 2017-05-31 at 10 27 14 am

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));
Copy link
Member

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

@ghillairet
Copy link
Member Author

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());
Copy link
Member

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.

@tfesenko
Copy link
Member

tfesenko commented Jun 2, 2017

@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)
Examples of such pointers:

  • VALID REF: References to the elements from the same workspace, e.g. $ref: "uber_schemas.yaml#/components/schemas/PriceEstimate". https://modelsolv.atlassian.net/secure/attachment/33000/Multi-file%20Uber%20%28OAS3%29.zip has good examples.
  • VALID REF: References to remote resources, e.g. $ref: "https://raw.githubusercontent.com/RepreZen/KaiZen-OpenAPI-Editor/master/com.reprezen.swagedit.openapi3.tests/resources/spec_examples/v3.0/uber.yaml#/components/schemas/PriceEstimate"
  • INVALID REF: just invalid pointer, like in my JUnit test: $ref: "INVALID". Yes, it is invalid, but it should show a warning, instead of breaking validation.

All these cases are caused by the same line of code. But we should test them (preferably with automated tests) separately after fixing it.
Exception:

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)

@tfesenko
Copy link
Member

tfesenko commented Jun 2, 2017

I added more tests for invalid refs, so the old tests + new tests cover these variations of invalid refs:

  • not a pointer
  • points to a top level element which is not a JSON
  • pointer to a nested element inside a non-JSON file
  • points to a JSON of invalid type (covered by initial set of tests)

The tests check that these scenarios don't break the validation

@ghillairet
Copy link
Member Author

@tfesenko I updated this PR to make your tests pass. The validation of remote refs as also been fixed, and support for links operationId, operationRef and securitySchemes (see #308 , #306 and #347)

@ghillairet
Copy link
Member Author

There is still an issue with validation of refs inside the same workspace/project, I'm trying to fix that now.

}
}
return errors;
}

protected void executeModelValidation(Model model, AbstractNode node, Set<SwaggerError> errors) {
Copy link
Member

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);
Copy link
Member

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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) thanks

Copy link
Member

@tfesenko tfesenko left a 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

@tfesenko tfesenko merged commit b923a58 into master Jun 8, 2017
@tfesenko tfesenko deleted the task/323 branch June 8, 2017 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants