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

Feat: allow path parameters on controllers #885

Conversation

vincentvanderweele
Copy link
Contributor

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you written unit tests?
  • Have you written unit tests that cover the negative cases (i.e.: if bad data is submitted, does the library respond properly)?
  • This PR is associated with an existing issue?

Closing issues

Close #261

If this is a new feature submission:

  • Has the issue had a maintainer respond to the issue and clarify that the feature is something that aligns with the goals and philosophy of the project?

Potential Problems With The Approach

This is quite a simple solution. I am not sure if passing down the controller path this way is the correct way to solve it, or if it would be better to get the parent information from within the MethodGenerator. I created this as a starting point for discussion; I'd happily update the approach if preferred.

In the related issue #318 Luke Autry wrote

The tricky thing would be verifying that each method within the controller properly accepts the path parameter (e.g. {companyId}).

I don't think that is implemented on method level yet either, so I did not add support for that here. Since the full path is in scope when handling the @Path decorator, I don't think this solution makes that validation harder, if someone would want to add that later.

Test plan

I verified that the correct parameters are generated and that both controller parameters and method parameters can be used in all 3 generated server frameworks.

@vincentvanderweele
Copy link
Contributor Author

I ran the tests locally and they passed there. Did I forget to register something somewhere?

Copy link
Collaborator

@WoH WoH left a comment

Choose a reason for hiding this comment

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

Looks very similar to the approach I was working on. Would be good to add one more test as outlined.

I ran the tests locally and they passed there. Did I forget to register something somewhere?

I haven't looked at your Path Parameter lookup, but it seems to be failing:

  1) Sub resource route generation
       should generate a path parameter for method without path parameter:
     AssertionError: Path object for /SubResourceTest/{mainResourceId}/SubResource route wasn't generated.: expected undefined to exist
      at Object.VerifyPath (unit/utilities/verifyPath.ts:16:71)
      at getValidatedParameters (unit/swagger/pathGeneration/subResourceRoutes.spec.ts:14:18)
      at Context.<anonymous> (unit/swagger/pathGeneration/subResourceRoutes.spec.ts:26:24)
      at processImmediate (internal/timers.js:461:21)

  2) Sub resource route generation
       should generate two path parameters for method with path parameter:
     AssertionError: Path object for /SubResourceTest/{mainResourceId}/SubResource/{subResourceId} route wasn't generated.: expected undefined to exist
      at Object.VerifyPath (unit/utilities/verifyPath.ts:16:71)
      at getValidatedParameters (unit/swagger/pathGeneration/subResourceRoutes.spec.ts:14:18)
      at Context.<anonymous> (unit/swagger/pathGeneration/subResourceRoutes.spec.ts:31:24)
      at processImmediate (internal/timers.js:461:21)

E: You can use the current file mocha debug config to check, but I think these are likely reproducible locally. Maybe run yarn run build && yarn run pretest to make sure.

tests/fixtures/controllers/subresourceController.ts Outdated Show resolved Hide resolved
tests/fixtures/controllers/subresourceController.ts Outdated Show resolved Hide resolved
@@ -1246,6 +1246,27 @@ describe('Express Server', () => {
});
});

describe('Sub resource', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy this to the base hapi and koa server.

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'm not sure if I correctly understand this comment. Do you mean I should copy-paste the exact same code to the other two tests (hapi-server and koa-server)? Or is there a shared place to copy this to so I don't need to copy-paste the same test three times? I did the former but if you meant the latter I might need a bit more guidance.

@vincentvanderweele
Copy link
Contributor Author

It seems that my unit tests are still failing although they pass on my machine. I'll have a closer look tomorrow what's going on there.

@WoH WoH changed the title Feature/allow path parameters on controllers Feat: allow path parameters on controllers Jan 16, 2021
@WoH WoH merged commit d6c758c into lukeautry:master Jan 16, 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

Successfully merging this pull request may close these issues.

Path parameters in @Route decorator
2 participants