-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(repository-json-schema): enhance getJsonSchema for navig props #2975
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
feat(repository-json-schema): enhance getJsonSchema for navig props #2975
Conversation
|
@bajtos Can you please do a quick review and see if I am on right track ? Its a WIP PR. I still need to add new tests. |
|
@samarpanB thank you for the pull request, the changes look reasonable so far. I think you will need to add code setting |
4a1bb79 to
08d6a7a
Compare
examples/log-extension/src/__tests__/acceptance/log.extension.acceptance.ts
Outdated
Show resolved
Hide resolved
examples/log-extension/src/__tests__/unit/providers/log-action.provider.unit.ts
Outdated
Show resolved
Hide resolved
|
@bajtos I updated the PR with all the changes and test cases also added now. Just a note. I kind of struggled a lot this time to run the tests. 'npm test' kept on failing. First, it was due to missing deps. So, I ran 'npm ci'. But it also failed due to access issues. I am on a linux machine. After a long search/hit-and-trial, I figured that it was not working properly when I was using my own user in the machine. When I switched to root user, it worked perfectly. Do you think this is something we should document somewhere for contributors to save some time in troubleshooting ? |
08d6a7a to
ed92393
Compare
|
@bajtos I am not sure why the build is failing. It is showing permission errors. I think that's unrelated. |
ed92393 to
a5c95db
Compare
Yes, you need to run
I find that really weird, to be honest. Did you perhaps run Besides running Anyhow, it's hard for me to help you without seeing the particular error logs. This is also very off topic in this pull request. Can you open a new issue please where we can take a closer look at the problems you are experiencing? |
Yes I agree it's off-topic. I'll raise a separate issue if I encounter this again. I just wanted a heads up if I was doing something wrong. Thanks a lot for the direction @bajtos |
a5c95db to
c1ebc12
Compare
bajtos
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good, I have few smaller comments to address (or discuss).
I'd like @nabdelgadir to review this pull request too, as she has been recently involved with the work related to Inclusion of related models too.
packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts
Show resolved
Hide resolved
packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! +1 to @bajtos's comments and I have a couple small ones.
Also I restarted the CI job so it's passing now. 👍
| * false for relations with a single target (e.g. BelongsTo, HasOne). | ||
| * This property is needed by OpenAPI/JSON Schema generator. | ||
| */ | ||
| targetsMany?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this should be a required property like in the issue description. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept it optional for backward compatibility. But if you think otherwise I can make this as required. Thoughts @bajtos ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point; let's wait for @bajtos' thoughts, but can you please still add it to the relation definitions? e.g.:
export interface HasManyDefinition extends RelationDefinitionBase {
type: RelationType.hasMany;
targetsMany: true;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added to relation definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept it optional for backward compatibility.
Good point!
I agree that making targetsMany a required property can break existing applications or 3rd-party extensions. On the other hand, I would be surprised if any such app or extension actually did exist.
Here is the important part: what is the expected behavior when targetsMany is not set? I don't think it's a good idea to default to false.
I am proposing the following:
- Add a comment starting with
TODO(semver-major)explaining that we should maketargetsManyrequired in LB5 - Modify the code reading
targetsManyto throw a helpful error whentargetsManydoes not exist or isundefined. The most important place to change is inmodelToJsonSchema, but there may be other places to update too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preferably add a unit test to verify that modeltoJsonSchema can handle missing targetsMany and demonstrate what happens in such case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modify the code reading targetsMany to throw a helpful error when targetsMany does not exist or is undefined
Don't you think throwing error in this case would also mean we are making it backward incompatible ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand it, the error will be thrown only when modeltoJsonSchema is called with the new option includeRelations. Since this option did not exist before, there shouldn't be any existing code setting it to true and thus there shouldn't be any code affected by the new error being thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. Got it. Let add some error handling and a test case to verify it as well. This makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Please check now.
packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts
Outdated
Show resolved
Hide resolved
9f0f042 to
82a1a9e
Compare
nabdelgadir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
bajtos
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like you have refactored & extracted the logic determining schema of the navigational property into a new function 👍
Let's find a better name for it please.
The rest LGTM.
| * @param relMeta Relation metadata object | ||
| * @param targetRef Schema definition for the target model | ||
| */ | ||
| export function getPropDefForRelatedModel( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer to use full names instead of abbreviations.
How about the following?
export function getNavigationalPropertyForRelation(
relationMeta: RelationMetadata,
targetSchema: JSONSchema
): JSONSchema {
// ...
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. That's better. Changing it. Will rebase and squash as well.
8f5ed7d to
9baf86a
Compare
9baf86a to
bf0caaf
Compare
…ational properties Enhance `getJsonSchema` to describe navigational properties fix loopbackio#2630
bf0caaf to
ef23aaa
Compare
bajtos
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏
|
Thanks for the contribution @samarpanB! It's now landed 👏 |
Enhance
getJsonSchemato describe navigational propertiesfix #2630
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated👉 Check out how to submit a PR 👈