python-microservices / pyms

Library of utils to create REST Python Microservices
https://python-microservices.github.io/home/
GNU General Public License v3.0
269 stars 45 forks source link

Support more parameters for Connexion #180

Closed Agalin closed 3 years ago

Agalin commented 4 years ago

Is your feature request related to a problem? Please describe. I've recently wanted to use Connexion's feature that validates responses according to API schema. Then found that parameters described in config.yml docs are ALL that can be used and there seem to be absolutely no way of passing any additional value to the API.

Describe the solution you'd like Either there should be additional settings available in config.yml or at least some kind of a callback that could be overriden to easily pass additional parameters to API constructor. Maybe a mapping CUSTOM_OPTIONS or similar that would just be passed as unpacked kwargs to add_api? Even if not, there aren't that many parameters supporrted by this method so possibly all of them could be moved to config.yml.

Describe alternatives you've considered Currently end user needs to basically copy whole init_app of the Swagger service - and who knows how much of the Microservice class to support custom service.

avara1986 commented 4 years ago

Thanks a lot @Agalin for your issue and proposal!

Could be a correct approach this solution?

Add this parameters to swagger config: validate_responses and validator_map

    swagger:
      path: ""
      file: "swagger.yaml"
      url: "ws-doc/"
     ....
     validate_responses: [True|False]
     validator_map: "[path.to.your.CustomResponseValidator]"

and import validate_responses and validator_map like:

app = connexion.App(__name__,
                            specification_dir=specification_dir,
                            resolver=RestyResolver(self.project_dir)
                            validator_map={'response': importlib.import_module(self.validator_map)}
                                                                                      ^^^^
)
        params = {
            ....
            "options": {"swagger_url": self.url},
            "validate_responses": self.validate_responses
                                              ^^^^
        }
Agalin commented 4 years ago

Validator map should be passed per add_api call if I understand docs correctly (can't check implementation atm.) but it's exactly the way I'd like to see.

avara1986 commented 4 years ago

Sorry, you are right :+1:

        params = {
            ....
            "options": {"swagger_url": self.url},
            "validator_map": {'response': importlib.import_module(self.validator_map)},
            "validate_responses": self.validate_responses
        }
Agalin commented 4 years ago

Might as well add custom body and parameter validators - those cannot be set from the schema either and implementation would be the same.

avara1986 commented 4 years ago

ok! do you mean multiple custom valdations? :+1: in example....

    swagger:
      path: ""
      file: "swagger.yaml"
      url: "ws-doc/"
     ....
     validate_responses: [True|False]
     validator_map: 
       ....
       response : "[path.to.your.CustomResponseValidator]"
       body : "[path.to.your.CustomResponseValidator]"
       parameter : "[path.to.your.CustomResponseValidator]"
        validator_map = {k:  importlib.import_module(v) for k, v in self.validator_map.items()}
        params = {
            ....
            "options": {"swagger_url": self.url},
            "validator_map": validator_map,
            "validate_responses": self.validate_responses
        }
Agalin commented 4 years ago

Yeah. Although body and parameter are actually request validators. Those 3 can easily be placed under the same optional key in config.

mbcaldeiro commented 4 years ago

Te lo pido!!!