Skip to content

Make json require string and pass around metadata - #7010

Merged
rgwood merged 3 commits into
nushell:mainfrom
Kixunil:strict-json-decoding
Nov 21, 2022
Merged

rgwood merged 3 commits into
nushell:mainfrom
Kixunil:strict-json-decoding

Conversation

@Kixunil

@Kixunil Kixunil commented Nov 5, 2022

Copy link
Copy Markdown
Contributor

The json deserializer was accepting any inputs by coercing non-strings into strings. As an example, if the input was [1, 2] the coercion would turn into [12] and deserialize as a list containing number twelve instead of a list of two numbers, one and two. This could lead to silent data corruption.

Aside from that pipeline metadata wasn't passed aroud.

This commit fixes the type issue by adding a strict conversion function that errors if the input type is not a string or external stream. It then uses this function instead of the original collect_string(). In addition, this function returns the pipeline metadata so it can be passed along.

Description

(Description of your pull request goes here. Provide examples and/or screenshots unless the change is trivial)

Tests + Formatting

Make sure you've done the following, if applicable:

  • Add tests that cover your changes (either in the command examples, the crate/tests folder, or in the /tests folder)
    • Try to think about corner cases and various ways how your changes could break. Cover those in the tests

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace --features=extra -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace --features=extra to check that all tests pass

User-Facing Changes

If you're making changes that will affect the user experience of Nushell (ex: adding/removing a command, changing an input/output type, adding a new flag):

@Kixunil
Kixunil force-pushed the strict-json-decoding branch 2 times, most recently from 1089308 to 3a310c2 Compare November 5, 2022 16:20
@rgwood

rgwood commented Nov 5, 2022

Copy link
Copy Markdown
Contributor

image

Oh dear. That does seem bad, good catch!

@rgwood

rgwood commented Nov 5, 2022

Copy link
Copy Markdown
Contributor

Here's the error after this PR:
image

@rgwood

rgwood commented Nov 5, 2022

Copy link
Copy Markdown
Contributor

One question that came up on Discord is whether this should be more general. It seems like many different from subcommands have the same issue; if we only fix from json things will be pretty inconsistent.

@Kixunil

Kixunil commented Nov 5, 2022

Copy link
Copy Markdown
Contributor Author

It seems like many different from subcommands have the same issue

Oh, good point, I started with json but I will be happy to look at others (should I put them in this PR or another one?)
I specifically added the collect_string_strict function into shared part because I suspected other formats/commands may want to use it, so it should be good starting point.

I'm also thinking of adding some more parsers for other formats and maybe they will use the method as well. (But I want to make them streaming.)

@dandavison

Copy link
Copy Markdown
Contributor

Ah, good catch. I also noticed this today, with xml:

〉[4 5] | to xml
45

@rgwood

rgwood commented Nov 6, 2022

Copy link
Copy Markdown
Contributor

(should I put them in this PR or another one?)

I think doing it in this PR makes sense, unless it turns out to be more work than expected.

@Kixunil

Kixunil commented Nov 6, 2022

Copy link
Copy Markdown
Contributor Author

OK, I will look into it. Hopefully later today.

@rgwood rgwood added the notes:breaking-changes Noted in Breaking Changes section label Nov 7, 2022
@Kixunil

Kixunil commented Nov 7, 2022

Copy link
Copy Markdown
Contributor Author

I pushed a commit fixing the other commands but didn't have time to test properly yet. I should be able to test tomorrow but feel free to give it a go in the meantime if you want.

@fdncred

fdncred commented Nov 19, 2022

Copy link
Copy Markdown
Contributor

@Kixunil How's this PR coming? Will you be able to get back to it soon?

The json deserializer was accepting any inputs by coercing non-strings
into strings. As an example, if the input was `[1, 2]` the coercion
would turn into `[12]` and deserialize as a list containing number
twelve instead of a list of two numbers, one and two. This could lead
to silent data corruption.

Aside from that pipeline metadata wasn't passed aroud.

This commit fixes the type issue by adding a strict conversion
function that errors if the input type is not a string or external
stream. It then uses this function instead of the original
`collect_string()`. In addition, this function returns the pipeline
metadata so it can be passed along.
@Kixunil
Kixunil force-pushed the strict-json-decoding branch from c4730b8 to 189a48e Compare November 20, 2022 20:30
@Kixunil

Kixunil commented Nov 20, 2022

Copy link
Copy Markdown
Contributor Author

Thanks for the ping, I didn't have time but I just got some. :)

The problem with json coercing non-string types to string was present in
all other text formats. This reuses the `collect_string_strict` function
to fix them.
The method `into_pipeline_data_with_metadata` can now be conveniently
used.
@Kixunil
Kixunil force-pushed the strict-json-decoding branch from 189a48e to a27ffd5 Compare November 20, 2022 22:45
@Kixunil

Kixunil commented Nov 21, 2022

Copy link
Copy Markdown
Contributor Author

The tests pass now. 🎉

@rgwood

rgwood commented Nov 21, 2022

Copy link
Copy Markdown
Contributor

This is an awesome change, thanks! I'm reading through the code and giving it a try, looks good so far and we should be able to merge it soon.

@rgwood

rgwood commented Nov 21, 2022

Copy link
Copy Markdown
Contributor

This looks super. Did some manual testing and it works nicely.

@rgwood
rgwood merged commit d9d6cea into nushell:main Nov 21, 2022
@Kixunil
Kixunil deleted the strict-json-decoding branch November 21, 2022 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants