Skip to content

Conversation

@samarpanB
Copy link
Contributor

@samarpanB samarpanB commented May 27, 2019

Enhance getJsonSchema to describe navigational properties

fix #2630

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@samarpanB
Copy link
Contributor Author

@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.

@bajtos
Copy link
Member

bajtos commented May 31, 2019

@samarpanB thank you for the pull request, the changes look reasonable so far. I think you will need to add code setting targetsMany property to all relational decorators, see e.g. https://github.com/strongloop/loopback-next/pull/2592/files#diff-64c2bbb6c63151367ef935def7eb54da

@bajtos bajtos self-assigned this May 31, 2019
@bajtos bajtos added OpenAPI Relations Model relations (has many, etc.) labels May 31, 2019
@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch from 4a1bb79 to 08d6a7a Compare June 3, 2019 10:11
@samarpanB
Copy link
Contributor Author

@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 ?

@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch from 08d6a7a to ed92393 Compare June 3, 2019 11:47
@samarpanB
Copy link
Contributor Author

@bajtos I am not sure why the build is failing. It is showing permission errors. I think that's unrelated.

@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch from ed92393 to a5c95db Compare June 4, 2019 07:21
@bajtos
Copy link
Member

bajtos commented Jun 4, 2019

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'.

Yes, you need to run npm ci to install dependencies before you can run npm test, see https://loopback.io/doc/en/contrib/code-contrib-lb4.html#building-the-project

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 ?

I find that really weird, to be honest. Did you perhaps run git clone under root user? I am not aware of any dependencies that would require global install or root privileges. npm ci is supposed to install all dependencies locally, inside your checked-out repository.

Besides running npm install, we are also running lerna to create symlinks between monorepo packages. Is it perhaps possible that these symlinks cannot be created?

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?

@samarpanB
Copy link
Contributor Author

samarpanB commented Jun 4, 2019

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'.

Yes, you need to run npm ci to install dependencies before you can run npm test, see https://loopback.io/doc/en/contrib/code-contrib-lb4.html#building-the-project

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 ?

I find that really weird, to be honest. Did you perhaps run git clone under root user? I am not aware of any dependencies that would require global install or root privileges. npm ci is supposed to install all dependencies locally, inside your checked-out repository.

Besides running npm install, we are also running lerna to create symlinks between monorepo packages. Is it perhaps possible that these symlinks cannot be created?

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

@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch from a5c95db to c1ebc12 Compare June 4, 2019 08:42
Copy link
Member

@bajtos bajtos left a 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.

Copy link
Contributor

@nabdelgadir nabdelgadir left a 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;
Copy link
Contributor

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?

Copy link
Contributor Author

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 ?

Copy link
Contributor

@nabdelgadir nabdelgadir Jun 5, 2019

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;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to relation definitions.

Copy link
Member

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 make targetsMany required in LB5
  • Modify the code reading targetsMany to throw a helpful error when targetsMany does not exist or is undefined. The most important place to change is in modelToJsonSchema, but there may be other places to update too.

Copy link
Member

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.

Copy link
Contributor Author

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 ?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Please check now.

@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch 3 times, most recently from 9f0f042 to 82a1a9e Compare June 6, 2019 19:31
Copy link
Contributor

@nabdelgadir nabdelgadir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Copy link
Member

@bajtos bajtos left a 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(
Copy link
Member

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 {
  // ...
}

Copy link
Contributor Author

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.

@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch 3 times, most recently from 8f5ed7d to 9baf86a Compare June 9, 2019 17:44
@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch from 9baf86a to bf0caaf Compare June 10, 2019 13:55
…ational properties

Enhance `getJsonSchema` to describe navigational properties

fix loopbackio#2630
@samarpanB samarpanB force-pushed the getjsonschema_navig_prop branch from bf0caaf to ef23aaa Compare June 10, 2019 13:57
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@nabdelgadir nabdelgadir merged commit 7801f59 into loopbackio:master Jun 10, 2019
@nabdelgadir
Copy link
Contributor

Thanks for the contribution @samarpanB! It's now landed 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OpenAPI Relations Model relations (has many, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance getJsonSchema to describe navigational properties

4 participants