Skip to content

Feature/issue-40 #41

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

Merged
merged 13 commits into from
Jun 25, 2020
Merged

Feature/issue-40 #41

merged 13 commits into from
Jun 25, 2020

Conversation

dnllowe
Copy link
Contributor

@dnllowe dnllowe commented Jun 17, 2019

My team and I have been using this library and it's made life much easier when working with quads and the rdf format. Thanks!

However, we have a need to return jsonld where order matters. But, the JsonLdApi.FromRDF method sorts properties by default. We need to be able to control when to sort the top-level graphs, the child nodes, or both.

The new SortingTests provide examples for what we're after, but as a summary, given the json below, we'd want to control whether nodes 1, 2, and 3 get sorted, and whether their children (objects 1, 2, and 3) are sorted. As of now, the JsonLdApi.FromRDF will always sort the graph nodes and children, losing the orignal order.

[
    {
        "@id": "http://example.org/node/3",
        "@graph": [
            {
                "@id": "http://example.org/object/3",
                "http://example.org/value": [
                    {
                        "@id": "n3-o3-value"
                    }
                ]
            },
            {
                "@id": "http://example.org/object/1",
                "http://example.org/value": [
                    {
                        "@id": "n3-o1-value"
                    }
                ]
            },
            {
                "@id": "http://example.org/object/2",
                "http://example.org/value": [
                    {
                        "@id": "n3-o2-value"
                    }
                ]
            }
        ]
    },
    {
        "@id": "http://example.org/node/1",
        "@graph": [
            {
                "@id": "http://example.org/object/3",
                "http://example.org/value": [
                    {
                        "@id": "n1-o3-value"
                    }
                ]
            },
            {
                "@id": "http://example.org/object/1",
                "http://example.org/value": [
                    {
                        "@id": "n1-o1-value"
                    }
                ]
            },
            {
                "@id": "http://example.org/object/2",
                "http://example.org/value": [
                    {
                        "@id": "n1-o2-value"
                    }
                ]
            }
        ]
    },
    {
        "@id": "http://example.org/node/2",
        "@graph": [
            {
                "@id": "http://example.org/object/3",
                "http://example.org/value": [
                    {
                        "@id": "n2-o3-value"
                    }
                ]
            },
            {
                "@id": "http://example.org/object/1",
                "http://example.org/value": [
                    {
                        "@id": "n2-o1-value"
                    }
                ]
            },
            {
                "@id": "http://example.org/object/2",
                "http://example.org/value": [
                    {
                        "@id": "n2-o2-value"
                    }
                ]
            }
        ]
    }
]

I've gotten this to work only with JsonLdApi.FromRDF, but that's the only need we have, so I've left the rest of the code alone as much as possible. Testing the same changes with Compact revealed just how complicated it might be to try and control the order in all cases--but if the original input for the other methods (Compact, Flatten, Expand, etc) is in the order that's desired, then it seems to remain unchanged. It was only FromRDF the changed the order of the input.

Fixes #40.

@@ -43,6 +42,9 @@ public virtual JsonLD.Core.JsonLdOptions Clone()

private bool produceGeneralizedRdf = false;

private bool sortGraphsFromRdf = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since I've only been able to control the order with the FromRDF method (which luckily is all that's needed to get the desired behavior), I made the option pretty narrow in scope.

@@ -16,7 +13,7 @@ namespace JsonLD.Test
public class ConformanceTests
{
[Theory, ClassData(typeof(ConformanceCases))]
public void ConformanceTestPasses(string id, string testname, ConformanceCase conformanceCase)
public void ConformanceTestPasses(string id, ConformanceCase conformanceCase)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The testname variable was never used, so removing it had no impact

@@ -77,20 +74,21 @@ public ConformanceCases()

public IEnumerator<object[]> GetEnumerator()
{
var jsonFetcher = new JsonFetcher();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created the JsonFetcher class so I could reuse some of this logic in my own test class

@codecov-io
Copy link

codecov-io commented Jun 17, 2019

Codecov Report

Merging #41 into master will increase coverage by 0.20%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #41      +/-   ##
==========================================
+ Coverage   68.38%   68.59%   +0.20%     
==========================================
  Files          22       22              
  Lines        4043     4053      +10     
  Branches     1026     1028       +2     
==========================================
+ Hits         2765     2780      +15     
+ Misses       1133     1130       -3     
+ Partials      145      143       -2     
Impacted Files Coverage Δ
src/json-ld.net/Core/JsonLdProcessor.cs 68.68% <ø> (ø)
src/json-ld.net/Core/NormalizeUtils.cs 95.47% <ø> (ø)
src/json-ld.net/Core/JsonLdApi.cs 84.85% <100.00%> (+0.03%) ⬆️
src/json-ld.net/Core/JsonLdOptions.cs 81.63% <100.00%> (+3.58%) ⬆️
src/json-ld.net/Core/JsonLdUtils.cs 27.98% <0.00%> (+0.72%) ⬆️
src/json-ld.net/Core/RDFDataset.cs 69.54% <0.00%> (+0.75%) ⬆️

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 5114477...025f592. Read the comment docs.

}
}
}

private JToken GetJson(JToken j)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to the JsonFetcher class

@@ -0,0 +1,89 @@
[
Copy link
Contributor Author

Choose a reason for hiding this comment

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

After getting FromRDF to work, I tried the same approach with Compact, but it seemed too complicated to get it to work. Plus, if Compact or the other methods are given input, they seem to maintain the order. I left the remnants of the test here though in case it seemed worthwhile to try again later.

Or we can just delete it.

@@ -0,0 +1,58 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the easiest way for me to create quads to provide the FromRDF method.

@@ -0,0 +1,143 @@
using JsonLD.Core;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to keep the sorting tests self-contained and not mix them in with the other tests, assuming that if the other tests are serializing correctly, and this is sorting correctly, then all is well.

@goofballLogic
Copy link
Member

It's important to maintain the same API as that exposed by jsonld.js can you check what they are doing on this issue?

@goofballLogic goofballLogic requested a review from xivk August 21, 2019 11:48
@asbjornu
Copy link
Member

asbjornu commented May 4, 2020

@dnllowe, do you think we could move this PR forward by answering @goofballLogic's question? Looks like @xivk's review would be appreciated as well.

@dnllowe
Copy link
Contributor Author

dnllowe commented May 15, 2020

@dnllowe, do you think we could move this PR forward by answering @goofballLogic's question? Looks like @xivk's review would be appreciated as well.

Sorry--the company had ended up forking the repository for the change. But let me take a look this weekend and get back to you!

@dnllowe
Copy link
Contributor Author

dnllowe commented May 16, 2020

The jsonld.js library also sorts by default, with no option to preserve the sorting order. I've opened an issue/PR for the library to allow the same behavior I've proposed here:

digitalbazaar/jsonld.js#396

@goofballLogic
Copy link
Member

sounds like the jsonld guys are on the fence about this functionality. Is there a way we could distinguish between "conformance" tests and "extra functionality" tests in our suite - so that there's a clear distinction between the official suite's tests and this extra function?

@dnllowe
Copy link
Contributor Author

dnllowe commented May 23, 2020

sounds like the jsonld guys are on the fence about this functionality. Is there a way we could distinguish between "conformance" tests and "extra functionality" tests in our suite - so that there's a clear distinction between the official suite's tests and this extra function?

That sounds reasonable. Could go with either CustomOrderingTests to keep it specific and isolated, or something like ExtendedFunctionalityTests if you want a destination for any possible diverging functionality in the future.

@goofballLogic
Copy link
Member

ExtendedFunctionalityTests sounds good. Apologies for the lax response on this - challenging times for everyone at the moment I think!

@dnllowe
Copy link
Contributor Author

dnllowe commented Jun 1, 2020

ExtendedFunctionalityTests sounds good. Apologies for the lax response on this - challenging times for everyone at the moment I think!

Hey, no worries at all-and no rush! You've already been a great help, and I appreciate it :)

dnllowe added 2 commits June 1, 2020 23:19
Moves to ExtendedFunctionalityTests

Refactors to create room for more extended functionality tests
Copy link
Member

@goofballLogic goofballLogic left a comment

Choose a reason for hiding this comment

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

Seems sound to me

@asbjornu
Copy link
Member

Is this a breaking change so we should consider bumping the major version after merging this?

@goofballLogic
Copy link
Member

goofballLogic commented Jun 25, 2020

Is this a breaking change so we should consider bumping the major version after merging this?

I think this is adding a predictable behaviour (sorting) where previously the behaviour was, in theory, unpredictable. Is it possible that somebody could depend on things not being sorted???

@asbjornu
Copy link
Member

I agree that sounds highly unlikely, @goofballLogic. Let's merge and bump minor.

@asbjornu asbjornu merged commit 7202bc6 into linked-data-dotnet:master Jun 25, 2020
@dnllowe dnllowe deleted the feature/issue-40 branch June 30, 2020 03:27
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.

Control order of graph nodes and children nodes
4 participants