简体   繁体   中英

Swagger adding custom attributes to parameter definition

We have a python web server app based on connexion and Flask libraries. For audit purposes we print every request, including request body, to the logs. Those logs are much more extensive than what Flask prints by default.

However, there are parameters like passwords or keys that I don't want to print their values to the logs.

I want to add custom attribute to several parameters' definition in swagger.yml so on web app start up I will get parameters definitions from connexion and save all the parameters that have this custom attribute, so later on I will hide values of those parameters in the logs.

My desired definition:

 paths: /demo_add_user: post: summary: "add user" operationId: api.api.demo_add_user parameters: - name: "new_user" in: body required: true description: "Use to add" schema: $ref: "#/definitions/NewUser" - name: "password" in: body description: "user password" required: false type: string x-hidden: true responses: ...

Obviously, I would like connexion to ignore this attribute since this is specific to our implementation. Any way to add custom attribute like x-hidden to parameter definition?

The solution is to use OpenApi extensions by adding x-attr like attribute.

The details are in next link: https://swagger.io/specification/#specificationExtensions

Just extending @D'artanian's answer :

using nodejs, for example it may be :

swagger :

/liveness:
get:
  operationId: apiHealthLiveness
  x-myCustomParam : "test"
  summary: Liveliness test for service
  description: deterimines if a service is still alive
  responses:
    "200": 
  ...

And using req object, access it :

const myCustomParamValue = req.swagger.operation["x-myCustomParam"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM