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

fixes #54 support for draft V6, V7 and V2019-09 #213

Merged
merged 7 commits into from
Nov 18, 2019
Merged

fixes #54 support for draft V6, V7 and V2019-09 #213

merged 7 commits into from
Nov 18, 2019

Conversation

stevehu
Copy link
Contributor

@stevehu stevehu commented Nov 15, 2019

I have spent several hours today to refactor the library so that we can support multiple specification versions easily for both developers and end-users. @jawaff and I have discussed the design for several days in the #206 and we finally decide to implement it this way.

I also added a document in the doc folder named specversion.md for both users and developers.

@jawaff
Copy link
Contributor

jawaff commented Nov 15, 2019

The changes look really good for the most part. The only other thing I can think to comment on is the fact that the draft 4 SpecVersion is hard coded into a lot of the tests. Maybe you could use Junit's Parameterized test runner. (I use TestNG more than Junit, so sorry if I'm not sure how to use this correctly.) It looks like the Parameterized test runner would allow the tests to be executed for each SpecVersion. That's just a suggestion in order to improve the tests, but it might not be necessary.

@stevehu
Copy link
Contributor Author

stevehu commented Nov 15, 2019

I haven't thought about the test cases in detail yet. Hardcoded V4 is to ensure that all the changes are backward compatible so that it won't impact the current users. If there is a way that we can run the test against all spec factory, it will be great. I am not familiar with TestNG, would you please try something? Thanks.

@codecov-io
Copy link

codecov-io commented Nov 15, 2019

Codecov Report

Merging #213 into master will increase coverage by 0.66%.
The diff coverage is 79.12%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #213      +/-   ##
============================================
+ Coverage     70.12%   70.79%   +0.66%     
- Complexity      520      567      +47     
============================================
  Files            62       68       +6     
  Lines          2032     2236     +204     
  Branches        441      482      +41     
============================================
+ Hits           1425     1583     +158     
- Misses          419      457      +38     
- Partials        188      196       +8
Impacted Files Coverage Δ Complexity Δ
.../main/java/com/networknt/schema/TrueValidator.java 100% <100%> (ø) 3 <3> (?)
src/main/java/com/networknt/schema/JsonSchema.java 87.67% <100%> (+1.51%) 28 <0> (+2) ⬆️
.../main/java/com/networknt/schema/EnumValidator.java 67.44% <100%> (+2.44%) 10 <0> (+2) ⬆️
...main/java/com/networknt/schema/FalseValidator.java 100% <100%> (ø) 3 <3> (?)
...main/java/com/networknt/schema/ConstValidator.java 100% <100%> (ø) 7 <7> (?)
...rc/main/java/com/networknt/schema/SpecVersion.java 100% <100%> (ø) 6 <6> (?)
...a/com/networknt/schema/SchemaValidatorsConfig.java 87.5% <100%> (ø) 8 <0> (ø) ⬇️
...om/networknt/schema/ExclusiveMaximumValidator.java 40.47% <40.47%> (ø) 5 <5> (?)
...om/networknt/schema/ExclusiveMinimumValidator.java 40.47% <40.47%> (ø) 6 <6> (?)
...n/java/com/networknt/schema/JsonSchemaFactory.java 74.21% <92.3%> (+2.47%) 24 <5> (+4) ⬆️
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a34af03...08ca9e4. Read the comment docs.

@jawaff
Copy link
Contributor

jawaff commented Nov 15, 2019

I don't know if I should commit to your pull request. I was always taught not to commit to other people's branches at my workplace. I'm just going to show an example of what I'm talking about and I can add a commit if you really want me to.

// This tells Junit that this test class should be parameterized.
@RunWith(Parameterized.class)
public class UriMappingTest {
    // This specifies the static method that supplies the parameters for each execution.
    @Parameterized.Parameters
    public static Collection<?> parameters() {
      return Arrays.asList(new Object[][] {
         { "draft4", JsonMetaSchema.getV4() },
         { "draft6", JsonMetaSchema.getV6() },
         { "draft7", JsonMetaSchema.getV7() },
         { "draft201909", JsonMetaSchema.getV201909() }
      });
    }

    private final ObjectMapper mapper = new ObjectMapper();
    private final ClasspathURLFactory classpathURLFactory = new ClasspathURLFactory();
    private final URLFactory urlFactory = new URLFactory();

    private final String resourcePrefix;
    private final JsonMetaSchema metaSchema;
    // This constructor is where the parameters are injected by Junit.
    public UriMappingTest(String resourcePrefix, JsonMetaSchema metaSchema) {
        super()
        this.resourcePrefix = resourcePrefix;
        this.metaSchema = metaSchema;
    }

   // This test will run 4 times. Each time will be with different this.resourcePrefix and this.metaSchema values.
    // One assumption is that there is a uri-mapping.json file in each one of the draft resource directories. It's possible that the same uri-mapping.json file could be reused for each draft version to ensure backwards compatibility, but maybe the uri-mapping.json files need to be different depending on the draft version.
   @Test
    public void testBuilderUriMappingUri() throws IOException {
        URL mappings = ClasspathURLFactory.convert(
                this.classpathURLFactory.create(String.format("resource:%s/uri_mapping/uri-mapping.json", this.resourcePrefix)));
        Builder builder = JsonSchemaFactory.builder()
                .defaultMetaSchemaURI(this.metaSchema.getUri())
                .addMetaSchema(this.metaSchema)
                .addUriMappings(getUriMappingsFromUrl(mappings));
        JsonSchemaFactory instance = builder.build();
        JsonSchema schema = instance.getSchema(this.urlFactory.create(
                String.format("https://raw.githubusercontent.com/networknt/json-schema-validator/master/src/test/resources/%s/uri_mapping/uri-mapping.schema.json", this.resourcePrefix)));
        assertEquals(0, schema.validate(mapper.readTree(mappings)).size());
    }
}

@stevehu
Copy link
Contributor Author

stevehu commented Nov 15, 2019

@jawaff I don't mind if you check into the same branch and this might be the most efficient way to work together. However, if you don't feel comfortable, could you please approve the PR and you can start another one after the merge. Thanks.

Copy link

@andersonf andersonf left a comment

Choose a reason for hiding this comment

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

@stevehu, I liked the way you handle which commands are implemented for each schema using the position of bits. Thanks for the documentation and all your effort. This change will make it even easier to add new commands.

@stevehu
Copy link
Contributor Author

stevehu commented Nov 18, 2019

@andersonf @jawaff Thanks a lot for the reviews and feedbacks. I have merged this PR. Please submit the PRs for the test runner and if-else-then. Contribution credits need to go to the contributors :)

@stevehu stevehu restored the issue54 branch November 18, 2019 01:18
@jawaff
Copy link
Contributor

jawaff commented Nov 18, 2019

@stevehu This was such a great change. Thanks for the effort, Steve. I'll look into making a pull request later today or tomorrow.

@stevehu stevehu deleted the issue54 branch November 18, 2019 22:21
@stevehu
Copy link
Contributor Author

stevehu commented Nov 18, 2019

@jawaff I was inspired by the ideas you suggested.

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.

4 participants