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

[#353] Validation of external $ref property values should show error … #358

Merged
merged 11 commits into from
Oct 5, 2017

Conversation

ghillairet
Copy link
Member

@ghillairet ghillairet commented Jun 9, 2017

…on unexpected object type

Validation of reference types has been moved to ReferenceValidator that previously only validate correctness of references pointer/URIs.
It now checks the type of the reference if this one is valid, this allow to reuse the code for loading the JSON document which is referenced by it.
This validation could be slow because it will parse the referenced documents into Models.

See #353

…on unexpected object type

Validation of reference types has been moved to ReferenceValidator that previously only validate correctness of references pointer/URIs.
It now checks the type of the reference if this one is valid, this allow to reuse the code for loading the JSON document which is referenced by it.
This validation could be slow because it will parse the referenced documents into Models.
@ghillairet
Copy link
Member Author

@tfesenko Can you please review. I updated this PR to include validation of types for external references. See #345 #353

val resolvedURI = new URI(null, null, "/components/parameters/Valid")
val errors = validator(#{resolvedURI -> document.asJson}).validate(baseURI, document)

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!

if (file == null || !file.exists()) {
return null;
}

try {
return getDocument(uri.toURL());
Copy link
Member

@tfesenko tfesenko Sep 29, 2017

Choose a reason for hiding this comment

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

We are passing an entire URL to JsonDocumentManager.getDocument(URL) which includes both the file and ref parts. JsonDocumentManager.getDocument(URL) has a table whose keys are URLs in a form we are passing the here. As a result, for two different refs from the same external model will load the model twice. For example, for the beamup_path_estimates_price.yaml from the Multi-file Beamup (v3) example the documents map will contain a different JsonNode model for each parameter:

{<...>/Multi-file%20Beamup%20(v3)/models/components/beamup_parameters.yaml#/components/parameters/start_longitude
    ={"openapi":"3.0.0", <...>}, 

<...>/Multi-file%20Beamup%20(v3)/models/components/beamup_parameters.yaml#/components/parameters/end_longitude
   ={"openapi":"3.0.0",<...>}
}

But it should be a single entry for <...>/Multi-file%20Beamup%20(v3)/models/components/beamup_parameters.yaml!
We should fix JsonDocumentManager.getDocument(URL).

@@ -223,4 +216,19 @@ public static JsonPointer getPointer(ObjectNode node) {
}
}

/**
Copy link
Member

Choose a reason for hiding this comment

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

Javadoc for JsonReference.isMissing() should be corrected:

    /**
     * Returns true if the reference points to an existing JSON document and the pointer to an existing node inside that
     * document.
     * 
     * @param baseURI
     * @return true if the reference can be resolved.
     */
    public boolean isMissing(JsonDocument document, URI baseURI) {
        if (isInvalid()) {
            return false;
        }

        JsonNode resolved = resolve(document, baseURI);
        return resolved == null || resolved.isMissingNode();
    }

It returns true if the reference cannot be resolved and the pointer points to an inexistent element.

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.

I left several comments including a comment about the documents cache.
Also, we have several test failures on this branch:

Failed tests: 
  CodeTemplateTextTest.test_code_template_text:148 JSON Schema validation error: 

Tests in error: 
  ValidatorTest.testValidationShouldFail_ForInvalidPointers:314 » IllegalArgument
  ValidatorTest.testValidationShouldFail_refNotJson:368 » Scanner null; mapping ...

tfesenko and others added 2 commits September 29, 2017 15:56
…on unexpected object type

Fix cache of model documents in DocumentManager.
Fix javadoc.
@ghillairet
Copy link
Member Author

@tfesenko The PR has been updated.

@tfesenko
Copy link
Member

tfesenko commented Oct 4, 2017

@ghillairet , I am still getting two test failures on this branch (after merge master in to the task branch, the master is green):

Failed tests: 
  ValidatorTest.testValidationShouldFail_ForInvalidPointers:317 expected:<1> but was:<2>
  ValidatorTest.testValidationShouldFail_refNotJson:371 expected:<1> but was:<2>

@tfesenko
Copy link
Member

tfesenko commented Oct 4, 2017

Passed code review and QA.
All OK except for test failures.

@ghillairet
Copy link
Member Author

Tests have been fixed.

MyType1:
type: object
Copy link
Member

Choose a reason for hiding this comment

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

😄

@tfesenko
Copy link
Member

tfesenko commented Oct 5, 2017

Looks good. Thanks!!

@tfesenko tfesenko merged commit 26f604d into master Oct 5, 2017
@tfesenko tfesenko deleted the task/353 branch October 5, 2017 18:21
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