2017-01-20 9 views
6

私はopenapi.jsonに次のような結果を得るためにSecurityDefinitionため闊歩設定を構築しようとしている:私のsettings.pyでDjango REST Swagger:Swagger設定でセキュリティセクションを使用するには?

"securityDefinitions": { 
    "password": { 
    "type": "oauth2", 
    "tokenUrl": "http://example.com/oauth/token", 
    "flow": "password", 
    "scopes": { 
     "write": "allows modifying resources", 
     "read": "allows reading resources" 
    } 
    } 
}, 
"security": [{ 
    "password": ["read", "write"] 
}] 

を、私は、次の闊歩設定をadddedています

# Swagger settings 
SWAGGER_SETTINGS = { 
    "SECURITY_DEFINITIONS": { 
    "password": { 
     "type": "oauth2", 
     "tokenUrl": "http://example.com/oauth/token", 
     "flow": "password", 
     "scopes": { 
      "write": "allows modifying resources", 
      "read": "allows reading resources" 
     } 
    } 
    }, 
    "SECURITY": [{ 
    "password": ["read", "write"] 
    }] 
} 

問題はSwaggerによって生成されたopenapi.jsonにはsecurity dictが存在せず、どのように生成されるのかわかりません。

{ 
    "info": { 
     "title": "Example Service API", 
     "version": "" 
    }, 
    "host": "http://example.com", 
    "swagger": "2.0", 
    "securityDefinitions": { 
     "password": { 
      "type": "oauth2", 
      "scopes": { 
       "write": "allows modifying resources", 
       "read": "allows reading resources" 
      }, 
      "tokenUrl": "http://example.com/oauth/token", 
      "flow": "password" 
     } 
    }, 
    "paths": {...} 
} 

私の闊歩設定でこの概念を説明するための任意のより良い方法はあり:以下

は、生成されたopenapi.jsonを発表しましたか? openapi.jsonファイルを生成するために、どのプロセスがどのように動作しているのかを教えてください。

+0

あなたは簡単にデバッグできるようにopenapi.jsonファイルの内容を投稿することができます –

+0

質問を更新しました! –

+0

セキュリティを定義するためにリスト内にdictを構築したのはなぜですか?プレーンな辞書で試してみてください。 –

答えて

3

不明な点がある場合は、コードを確認してください。あなたはOpenAPIRenderer hereの定義を見ることができます:

class OpenAPIRenderer(BaseRenderer): 
    media_type = 'application/openapi+json' 
    charset = None 
    format = 'openapi' 

    def render(self, data, accepted_media_type=None, renderer_context=None): 
     if renderer_context['response'].status_code != status.HTTP_200_OK: 
      return JSONRenderer().render(data) 
     extra = self.get_customizations() 

     return OpenAPICodec().encode(data, extra=extra) 

    def get_customizations(self): 
     """ 
     Adds settings, overrides, etc. to the specification. 
     """ 
     data = {} 
     if swagger_settings.SECURITY_DEFINITIONS: 
      data['securityDefinitions'] = swagger_settings.SECURITY_DEFINITIONS 

     return data 

だから、これを行うための一つの方法は、例えば、サブクラス化することです:

class MyOpenAPIRenderer(OpenAPIRenderer): 
    def get_customizations(self): 
     data = super().get_customizations() 

     # your customizations 
     data["security"] = swagger_settings.SECURITY 

     return data 

次に、あなたがあなたのビューのために、このレンダラークラスを使用することができます。それが役に立てば幸い!

+0

残念ながら、フィールドSECURITYがデフォルトで定義されていないため、swagger_settings.SECURITYを定義することができないため、これは機能しません。私はこれが図書館で更新されるべきだと思う。 Githubに関する問題:https://github.com/marcgibbons/django-rest-swagger/issues/628 – physicalattraction

+0

@physicalattractionああ、私は参照してください。あなたもDjangoで作業していることを覚えておいてください。これは非常に柔軟な 'settings'モジュールを持っていますので、上記の問題を' django.confのインポート設定から '解決することができます:' data ["security"] = settings。 SWAGGER_SETTINGS ["SECURITY"] ' –

関連する問題