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

Make serialize() on a CONSTRUCT result act like normal g.serialize() #1834

Open
aucampia opened this issue Apr 16, 2022 Discussed in #1612 · 5 comments
Open

Make serialize() on a CONSTRUCT result act like normal g.serialize() #1834

aucampia opened this issue Apr 16, 2022 Discussed in #1612 · 5 comments
Assignees
Labels
breaking change This involves or proposes breaking RDFLib's public API. enhancement New feature or request

Comments

@aucampia
Copy link
Member

Discussed in #1612

Originally posted by nicholascar June 11, 2021
Currently (RDFlib 6.0.0a0 AKA master), g.serialize() defaults to turtle format and is decoded, however calling serialize() on a SPARQL CONSTRUCT result (i.e. on a Result object) returns the RDFlib 5.0.0 form of an RDF/XML, UTF-8-encoded result.

The old style serialize() in Result should be updated to match the main Graph() serialize().

@aucampia aucampia added the enhancement New feature or request label Apr 16, 2022
@aucampia aucampia self-assigned this Apr 16, 2022
@nicholascar
Copy link
Member

nicholascar commented Apr 22, 2022

I'm encountering issues related to this now. I can easily (two word change) make the serialize() return Turtle, not XML, by default, however the result object is still a rdflib.plugins.sparql.processor.SPARQLResult, not a rdflib.graph.Graph and I think it should be a Graph.

The documentation for Result says:

There is a bit of magic here that makes this appear like different Python objects, depending on the type of result.

but the reults aren't real bool, Graph etc objects, as far as I can tell, they are all SPARQLResult objects, but I think they suold be 'pure' bool, Graph etc.

@ghost
Copy link

ghost commented Apr 22, 2022

I think it should be a Graph.

Indeed it should be, according to the SPARQL 1.1 spec - “The CONSTRUCT query form returns a single RDF graph specified by a graph template.”

@Priyansh19077
Copy link

We are interested in this issue and are currently working on having a way to get the turtle format in the returned results for CONSTRUCT query. We looked into the xmlresults.py file in the reults directory of sparql. We realised that the serialize() function is being called in that file to actually serialize those results.

We are planning to have a way to convert the generated xml statements into turtle and then result that. Another way could be to have a separate file like turtleresults.py and then call the serialize function there.

In case you have any ideas we can go forward with this, please do share them.

@aucampia
Copy link
Member Author

aucampia commented May 14, 2022

There was a PR for this before:

@aucampia
Copy link
Member Author

aucampia added a commit to aucampia/rdflib that referenced this issue Jul 31, 2022
…lize`

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on RDFLib#1834
  soon and in that provide a way to avoid the ambiguity there also.
aucampia added a commit to aucampia/rdflib that referenced this issue Jul 31, 2022
…lize`

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on RDFLib#1834
  soon and in that provide a way to avoid the ambiguity there also.
aucampia added a commit to aucampia/rdflib that referenced this issue Jul 31, 2022
…lize`

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on RDFLib#1834
  soon and in that provide a way to avoid the ambiguity there also.
aucampia added a commit to aucampia/rdflib that referenced this issue Jul 31, 2022
…lize`

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on RDFLib#1834
  soon and in that provide a way to avoid the ambiguity there also.
aucampia added a commit to aucampia/rdflib that referenced this issue Jul 31, 2022
…lize`

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on RDFLib#1834
  soon and in that provide a way to avoid the ambiguity there also.
aucampia added a commit to aucampia/rdflib that referenced this issue Jul 31, 2022
…lize`

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on RDFLib#1834
  soon and in that provide a way to avoid the ambiguity there also.
aucampia added a commit that referenced this issue Aug 2, 2022
…lize` (#2065)

Change `{Graph,Result}.serialize` to only handle string destinations as URIs if
the schema is `file` and to treat it as operating system paths in all
other cases.

This is for the following reasons:
- `Result.serialize` was treating URI paths as OS paths which only works
  for some cases, for special charachters and percentage encoding it
  does not work.
- Many valid Unix and Windows paths parse using `urlparse` and have no netloc, e.g. `C:\some\path`
  and `some:/path`, however they should be treated as paths and not as URIs.
- `Graph` and `Result` should behave consistently.

Some caveats in this change:
- non-file URIs will now be treated as OS paths which may result in
  slightly confusing error messages, such as
  `FileNotFoundError: [Errno 2] No such file or directory: 'http://example.com/'`
  if http://example.com/ is passed.
- some valid file URIs (e.g. `file:/path/to/file` from https://datatracker.ietf.org/doc/html/rfc8089)
  are also valid Posix paths but will be treated as file-URIs instead of
  Posix paths. For `Graph.serialize` this ambiguity can be avoided by
  using `pathlib.Path`, but for `Result.serialize` there is currently no
  way to avoid it, though I will work on #1834
  soon and in that provide a way to avoid the ambiguity there also.
@aucampia aucampia added the breaking change This involves or proposes breaking RDFLib's public API. label May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change This involves or proposes breaking RDFLib's public API. enhancement New feature or request
Projects
None yet
3 participants