JoelLefkowitz / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg2.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
65 stars 2 forks source link

Schemes in Swagger UI #37

Open DmitriyKhalturin opened 3 years ago

DmitriyKhalturin commented 3 years ago

I have a trouble with Schemes in Swagger UI. Details: I create schema (method _get_schemaview) without url param, because my backend works on many addresses. And have result: Schemes in Swagger UI only with http. That isn't correct for my staging backend (only https is working).

JuliColombo commented 3 years ago

Same issue here! Locally it worked perfectly, but when I deployed my project to the staging environment, it didn't work.

JoelLefkowitz commented 3 years ago

I'll take a look into this now

JoelLefkowitz commented 3 years ago

@dmitriykhalturin Please post a copy of your DJANGO_SETTINGS_MODULE (with all sensitive data removed) a list of your project dependencies and your get_schema_view method call.

DmitriyKhalturin commented 3 years ago

@JoelLefkowitz hope this helps.

DJANGO_SETTINGS_MODULE:

DEBUG = True

ALLOWED_HOSTS = ('*',)

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'drf_yasg2',
    'apps',
)

MIDDLEWARE = (
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'apps.urls'

TEMPLATES = (
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': (os.path.join(BASE_DIR, 'templates'),),
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
)

WSGI_APPLICATION = 'apps.wsgi.application'

DATABASES = {
    'default': config(
        'DATABASE_URL',
        default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3'),
        cast=db_url
    ),
}

AUTH_PASSWORD_VALIDATORS = (
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    }, {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    }, {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    }, {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
)

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'contrib.authentication.backends.ApiBackend',
)

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'contrib.rest_framework.renderers.ApiJSONRenderer',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'contrib.rest_framework_simplejwt.authentications.ApiJWTAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'contrib.rest_framework.permissions.ApiIsAuthenticated',
    ),
}

SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(weeks=12 * 4),
    'REFRESH_TOKEN_LIFETIME': timedelta(weeks=12 * 4),
    'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': False,

    'ALGORITHM': 'HS256',
    'SIGNING_KEY': SECRET_KEY,
    'VERIFYING_KEY': None,
    'AUDIENCE': None,
    'ISSUER': None,

    'AUTH_HEADER_TYPES': ('Bearer',),
    'USER_ID_FIELD': 'id',
    'USER_ID_CLAIM': 'user_id',

    'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
    'TOKEN_TYPE_CLAIM': 'access',

    'JTI_CLAIM': 'jti',
}

CORS_ORIGIN_ALLOW_ALL = True

SWAGGER_SETTINGS = {
   'SECURITY_DEFINITIONS': {
      'Bearer': {
            'type': 'apiKey',
            'name': 'Authorization',
            'in': 'header',
      },
   },
}

get_schema_view in my urls config:

schema_view = get_schema_view(
    info=openapi.Info(
        title="API",
        default_version='v1'
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

if settings.DEBUG:
    urlpatterns += [
        path(r'swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
        path(r'redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    ]

And my Pipfile or Pipfile.lock:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django = "*"
psycopg2 = "*"
djangorestframework = "*"
djangorestframework-simplejwt = "*"
django-cors-headers = "*"
django-storages = "*"
python-decouple = "*"
dj-database-url = "*"
factory-boy = "*"
requests = "*"
sentry-sdk = "*"
drf-yasg2 = "*"
packaging = "*"

[requires]
python_version = "3.9"