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

JsonField can't resolve dot notation for source parameter #4

Closed
fbigand opened this issue Dec 26, 2020 · 6 comments
Closed

JsonField can't resolve dot notation for source parameter #4

fbigand opened this issue Dec 26, 2020 · 6 comments

Comments

@fbigand
Copy link

fbigand commented Dec 26, 2020

What did you want to do
I wanted to pass a dot notation string to JsonField component
<JsonField source="source.nested" />

What did you expect
I expected to have my nested component displayed correctly

What happened instead
It displays "root":{}

I don't know if this a bug, a upcoming feature or a wanted behavior, but the strange thing is that I have no problem with the JsonInput component using dot notation.

@MrHertal
Copy link
Owner

Hi @fbigand thanks for reporting this bug.

It should be fixed in this new release: https://github.com/MrHertal/react-admin-json-view/releases/tag/v1.0.4

@fbigand
Copy link
Author

fbigand commented Dec 28, 2020

Thank you for your response!
I installed the new release (1.0.4), but unfortunately the problem is not solved on my side. I tested with both nested object and nested array, but none of them work (I don't know if they are handled differently).

I used this object for the test:

{
    ...
    "content": {
        "test_array": [
            1,
            2,
            3,
            4
        ],
        "test_object": {
            "test": "test"
        }
    }
}

And I called JsonField like this inside my Show view:

...
<Tab label="Content">
    <JsonField source="content.test_array" />
    <JsonField source="content.test_object" />
</Tab>
...

Both of them render an empty json.

@MrHertal MrHertal reopened this Dec 29, 2020
@MrHertal
Copy link
Owner

Thanks for your response.

It would be interesting to know what your content field looks like after react-admin gets the data.
Could you show the log of the record at this point? You can use a function field like this:

import { FunctionField } from 'react-admin';

<FunctionField
  label="Test"
  render={(record) => {
    console.log(record);

    return <JsonField source="content" record={record} />;
  }}
/>

I would like to know if your content field is a string like this:

content: "{"test_array":[1,2,3,4],"test_object":{"test":"test"}}"

Or if it is an already parsed nested object like this:

content:
  test_array: Array(4)
    0: 1
    1: 2
    2: 3
    3: 4
    length: 4
    __proto__: Array(0)
  test_object:
    test: "test"
    __proto__: Object
__proto__: Object

@fbigand
Copy link
Author

fbigand commented Dec 29, 2020

The console.log displays the record that wraps the "content" attribute, and it is displayed as Javascript object.

{…}
  content: {…}
    test_array: (5) […]
      0: 0
      1: 1
      2: 2
      3: 3
      4: 4
      length: 5
      <prototype>: Array []
    test_object: {…}
      test: "test"
      <prototype>: Object { … }
    <prototype>: Object { … }
  id: 12
  title: "Some title"
  <prototype>: Object { … }

@MrHertal
Copy link
Owner

This is weird, I could not reproduce. Here is what I tried:

import { SimpleShowLayout, FunctionField } from "react-admin-json-view";
import { JsonField } from "react-admin-json-view";

<SimpleShowLayout>
  <FunctionField
    label="content.test_array"
    render={(record) => {
      const flowParams = JSON.parse(record.flowParams);
      console.log(flowParams);

      return (
        <JsonField
          source="content.test_array"
          record={{ ...record, content: flowParams }}
        />
      );
    }}
  />
  <FunctionField
    label="content.test_object"
    render={(record) => {
      const flowParams = JSON.parse(record.flowParams);
      console.log(flowParams);

      return (
        <JsonField
          source="content.test_object"
          record={{ ...record, content: flowParams }}
        />
      );
    }}
  />
</SimpleShowLayout>

On my project, flowParams field is a JSON string that has this value: {"test_array":[1,2,3,4],"test_object":{"test":"test"}}.
In order to be as close as possible to your case, I parsed this value and set the result to record.content.

console.log(flowParams); is printing:

{test_array: Array(4), test_object: {}}
  test_array: (4) [1, 2, 3, 4]
  test_object: {test: "test"}
  __proto__: Object

As a result, I got this:

snap

Can you check the difference with your code?

Did you restart your project after upgrading this package? (sorry I have to make sure 🙂)

@fbigand
Copy link
Author

fbigand commented Jan 4, 2021

Thank you for your last remark, because it is most probably that the package was not updated inside the docker container that is running the application.
Now the problem is solved and it works perfectly. So thank you for the update.

@MrHertal MrHertal closed this as completed Jan 5, 2021
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

No branches or pull requests

2 participants