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

relation expects foreignKey on the Object #1719

Closed
virkt25 opened this issue Sep 17, 2018 · 4 comments · Fixed by #2754
Closed

relation expects foreignKey on the Object #1719

virkt25 opened this issue Sep 17, 2018 · 4 comments · Fixed by #2754
Labels
bug Relations Model relations (has many, etc.)

Comments

@virkt25
Copy link
Contributor

virkt25 commented Sep 17, 2018

Description / Steps to reproduce / Feature proposal

When I was working on user order history component for https://github.com/strongloop/loopback4-example-shopping I ran into an issue ...

/users/{userId}/orders expects the orders object to have a userId field (Since it's a required property of the Order model and is enforced by the controller coercion / validation). The issue is that passing in the userId on the Order object passes controller validation but causes the relation validation to throw an error saying the foreign key can't be changed -- despite the fact that the foreign key is exactly the same as the one in the URL and the one given to the repository.

Current Behavior

Throws error if foreign key on object saying you can't change it even if it's the same one.

Expected Behavior

Be smart enough to not throw an error if the key given is the same as the one that's expected.


My current solution is to delete the foreign key in the controller. The other alternatives I can think of is to mark the key as an optional property (do we want this?) ... or to mark the requestBody type as Partial<Order> but this will mean losing type validation of expected properties on the Order type.

See Reporting Issues for more tips on writing good issues

@raymondfeng
Copy link
Contributor

I think it's possible for TypeScript to exclude some properties explicitly using Exclude, for example:

type OrderWithoutUserId = Pick<Order, Exclude<keyof Order, 'userId'>>

@virkt25
Copy link
Contributor Author

virkt25 commented Sep 18, 2018

@raymondfeng That worked in terms of TS Validation BUT it breaks the explorer whereby Try it out only gives me a {} instead of the model that I could just edit.

@raymondfeng
Copy link
Contributor

Yes, I commented in #1722. We need to have a way to reflect such TS types in OpenAPI spec.

@bajtos
Copy link
Member

bajtos commented Apr 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Relations Model relations (has many, etc.)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants