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

Removed internal variable pass_context_arg_name #1568

Conversation

leonardofesta
Copy link
Contributor

Fixes #1532 .

Changes proposed in this pull request:

  • I removed the parameter pass_context_arg_name passed internally as asked from the various constructors, and left in the SecurityHandlerFactory a constant called context_kw, following the same pattern of required_scopes_kw.

I don't know if it is better to put in uppercase both values (context_kw and required_scopes_kw), now they are constants in the end.

I left as the other one to have consistency, but if you prefer I can change them both.

@RobbeSneyders
Copy link
Member

Thanks @leonardofesta!
Could you update the failing tests to make them pass?

@RobbeSneyders
Copy link
Member

Hi @leonardofesta, did you have a chance to have another look at this? Let me know if you need support.

@leonardofesta
Copy link
Contributor Author

leonardofesta commented Aug 25, 2022

Sorry @RobbeSneyders I was on vacation and I didn't saw the emails. Now it should pass all tests.

Should i rebase and force push?

Copy link
Collaborator

@nielsbox nielsbox left a comment

Choose a reason for hiding this comment

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

LGTM apart from the thing that Ruwann said about collisions, could you maybe add a test where there is a parameter called context? Also converting the constants to all uppercase seems like a refactor we should do in a separate PR. Let's see what Ruwann and Robbe say.

@coveralls
Copy link

coveralls commented Aug 26, 2022

Pull Request Test Coverage Report for Build 3108587472

  • 7 of 9 (77.78%) changed or added relevant lines in 3 files are covered.
  • 43 unchanged lines in 10 files lost coverage.
  • Overall coverage increased (+0.09%) to 94.749%

Changes Missing Coverage Covered Lines Changed/Added Lines %
connexion/security/security_handler_factory.py 2 4 50.0%
Files with Coverage Reduction New Missed Lines %
connexion/apps/flask_app.py 1 95.24%
connexion/jsonifier.py 1 92.86%
connexion/middleware/main.py 1 96.43%
connexion/middleware/routing.py 1 96.08%
connexion/operations/openapi.py 1 95.21%
connexion/json_schema.py 2 85.57%
connexion/middleware/swagger_ui.py 3 95.18%
connexion/decorators/validation.py 5 96.41%
connexion/apis/abstract.py 11 90.48%
connexion/middleware/security.py 17 83.66%
Totals Coverage Status
Change from base Build 2910344107: 0.09%
Covered Lines: 2851
Relevant Lines: 3009

💛 - Coveralls

@nielsbox nielsbox self-requested a review August 26, 2022 13:52
Copy link
Member

@RobbeSneyders RobbeSneyders left a comment

Choose a reason for hiding this comment

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

Thanks @LeenaBhegade

You've removed some functionality that we need to keep. I added some comments to indicate where.

This means that we need the context_kw both in the parameter decorator and the security handler factory. We can probably just define it on both separately, as they are quite decoupled from each other.

I would also propose to use context_ as the default context_kw. The underscore lowers the chance of collision and indicates that it's a special variable.

@@ -124,10 +119,6 @@ def wrapper(request):
else:
logger.debug("Context parameter '%s' not in function arguments", key)

# attempt to provide the request context to the function
Copy link
Member

Choose a reason for hiding this comment

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

We still want to pass the context as an argument to the view function, so we can't just remove this. We need to include the context in kwargs using context_kw.

@@ -23,11 +23,6 @@ def get_arguments(self, *args, **kwargs):
parameter_to_arg(Op(), handler)(request)
func.assert_called_with(p1="123")

parameter_to_arg(Op(), handler, pass_context_arg_name="framework_request_ctx")(
Copy link
Member

Choose a reason for hiding this comment

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

We want to keep this behaviour, so should keep testing for it

def get_bye_secure_from_connexion(req_context):
return "Goodbye {user} (Secure!)".format(user=req_context["user"])
def get_bye_secure_from_connexion():
return "Goodbye {user} (Secure!)".format(user=context["user"])
Copy link
Member

Choose a reason for hiding this comment

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

We want to check that the context is passed to the function, so we shouldn't use the global context here.

now the function accept a pass_context_arg boolean parameter,
instead of the pass_context_arg_name value
@RobbeSneyders
Copy link
Member

Any update @leonardofesta?

@leonardofesta
Copy link
Contributor Author

leonardofesta commented Sep 20, 2022

Any update @leonardofesta?
I just pushed the update, I hope I understood correctly.
I'm using context_ as suggested as value, in both cases used as constant.

  • I basically restored the decorator functionality by using a boolean with default == False to maintain the same logic
  • In the security handler part, please recheck but now it should be correct

Let me know

Copy link
Member

@RobbeSneyders RobbeSneyders left a comment

Choose a reason for hiding this comment

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

  • I basically restored the decorator functionality by using a boolean with default == False to maintain the same logic

The reason to remove the pass_context_arg_name variable, was that it needs to be passed around a lot for a small benefit. Replacing it with a boolean doesn't really solve this.

Instead it would be nice if the context is passed automatically if the context_ parameter is added to the view function.

need_context = self.pass_context_arg_name and (
has_kwargs or self.pass_context_arg_name in arguments
)
need_context = has_kwargs or self.context_kw in arguments
Copy link
Member

Choose a reason for hiding this comment

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

We could do this by removing the has_kwargs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok i'll fix the two things as suggested

fixed security handler as suggested
removed pass_context_arg as before
@RobbeSneyders RobbeSneyders added this to the Connexion 3.0 milestone Sep 22, 2022
@RobbeSneyders
Copy link
Member

Thanks @leonardofesta, looks good to me.

I pushed a change to the test, as it wasn't actually testing the new behavior.

@RobbeSneyders RobbeSneyders merged commit fc003ca into spec-first:main Sep 22, 2022
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.

Remove pass_context_arg_name argument
4 participants