Forum Discussion

pfelsted's avatar
pfelsted
New Contributor
5 years ago

Reference to a SecurityScheme

Is there a way to reference the component securityScheme from a common swagger definitions so I do not need to duplicate it in each of my swagger definitions?  Like $ref http://somurl.com/component/securityScheme/oauthSample.  If so what is the syntax to add this globally and per path?

 

security:
- oauthSample:
- read
- write

 

security:
- oAuthSample: [write]

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You need to "import" the external security scheme definition into the "components/securitySchemes" section of the current file. Then you can define the global and path-level security requirements in the usual way.

    components:
      securitySchemes:
        oauthSample:
          $ref: 'http://somurl.com/component/securityScheme/oauthSample'
    
    security:
      - oauthSample:
          - read
          - write
    • pfelsted's avatar
      pfelsted
      New Contributor

      Thanks for your response.  Swagger is giving me an error saying it can not resolve the reference.  I triple checked the path.  I even tried adding a reference from the same file as the securitySchemes that I am using and that is not resolving for the securitySchemes.  

       

      Any other tips?

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        Which Swagger tool do you mean - Swagger Editor, Swagger UI, Swagger Codegen, ...?