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

Accessing API using CLI gives incorrect result when ResultType has multiple views #2671

Closed
sm43 opened this issue Aug 25, 2020 · 4 comments
Closed

Comments

@sm43
Copy link

sm43 commented Aug 25, 2020

I have an example here.
If you run the service and invoke the API using CLI.

./user-cli user get

this returns response

{
    "ID": 1,
    "Name": "abc",
    "LatestCompany": {
        "ID": 0,
        "Name": "Redhat",
        "Location": ""
    },
    "Companies": [
        {
            "ID": 1,
            "Name": "Symantec",
            "Location": "India"
        },
        {
            "ID": 2,
            "Name": "Red Hat",
            "Location": "India"
        }
    ]
}

Here, view used for LatestCompany is tiny which is supposed to return only Name but it returns all fields.

I am currently using goa in one other project where if the value for other fields are not defined then it breaks the API and returns

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x78b3db]

I think this is similar to #2613.

@raphael

@raphael
Copy link
Member

raphael commented Sep 20, 2020

Thank you for the report! This behavior is a result of the design behind views and what the generated CLI does. A view defines which fields in the result struct to initialize and validate but there is still a single type defined for the method result. The CLI prints the service method level result which has these fields defined. The generated code takes care of validating that fields present in a view definition (and only these) have valid values. So TL;DR the JSON serialized by the server is as you would expect:

{
    "companies": [
        {
            "id": 1,
            "location": "India",
            "name": "Symantec"
        },
        {
            "id": 2,
            "location": "India",
            "name": "Red Hat"
        }
    ],
    "id": 1,
    "latestCompany": {
        "name": "Redhat"
    },
    "name": "abc"
}

This JSON gets unmarshaled into the result type by the client code which is what the CLI prints. Hopes that make sense.

@sm43
Copy link
Author

sm43 commented Sep 21, 2020

@raphael Thanks for the explanation. :)
But I am getting another issue which is

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x77205b]

goroutine 1 [running]:
github.com/sm43/user-goa/gen/http/user/client.unmarshalCompanyResponseBodyToUserviewsCompanyView(...)
	/home/smukhade/go/src/github.com/sm43/user-goa/gen/http/user/client/encode_decode.go:83
github.com/sm43/user-goa/gen/http/user/client.NewGetUserOK(0xc000012090, 0x7991c0)
	/home/smukhade/go/src/github.com/sm43/user-goa/gen/http/user/client/types.go:45 +0x8b
github.com/sm43/user-goa/gen/http/user/client.DecodeGetResponse.func1(0xc000076000, 0x0, 0x0, 0x0, 0x0)
	/home/smukhade/go/src/github.com/sm43/user-goa/gen/http/user/client/encode_decode.go:64 +0x2e9
github.com/sm43/user-goa/gen/http/user/client.(*Client).Get.func1(0x8c3ec0, 0xc0000a0000, 0x0, 0x0, 0x1e, 0x0, 0xc0000aac00, 0x0)
	/home/smukhade/go/src/github.com/sm43/user-goa/gen/http/user/client/client.go:67 +0x120
main.main()
	/home/smukhade/go/src/github.com/sm43/user-goa/cmd/user-cli/main.go:82 +0x53f

This is similar to what I am getting in another project.
I tried to recreate this in the example I shared with you. you can take a look here.
So, there are 3 cases I have created, the 3rd one seems to be breaking.
HTTP response are correct but in CLI it breaks.

@raphael
Copy link
Member

raphael commented Sep 21, 2020

The above is clearly a bug, thank you for putting together a repro. Would you mind creating a new issue for the panic? thank you.

@raphael raphael closed this as completed Sep 21, 2020
@sm43
Copy link
Author

sm43 commented Sep 21, 2020

Created a Issue #2681

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