Skip to content

Conversation

@MyPyDavid
Copy link
Member

@MyPyDavid MyPyDavid commented Feb 21, 2025

copied from #1050, rebased onto 2.3.0 > dependency-updates > drf-spectacular

Description

Duplicate of PR: #1050
Related issue: #698

Tasks

  • decide about NOTICE
  • remove swagger.scss
  • decide about the schema
  • decide about the purpose of the static schema in https://github.com/rdmorganiser/rdmo-openapi, which is 6 years old
  • handle warning and errors in schema generation
  • add tests
  • delete temporary django-rest-swagger.yaml schema file
  • remove ignore warnings from pytest config
  • squash commits

drf-spectacular warnings to be fixed

1. /rdmo/conditions/

Serializers: serializers/v1.py

  • ConditionViewSet > ConditionSerializer: Unable to resolve type hint for function get_model. Consider using a type hint or @extend_schema_field. Defaulting to string.

Viewsets: viewsets.py

  • ConditionViewSet: Could not derive type of path parameter "var" because model rdmo.conditions.models.Condition contained no such field. Consider annotating parameter with @extend_schema. Defaulting to "string".

2. /rdmo/core/

Serializers: serializers.py

  • RelationViewSet > ChoicesSerializer: Unable to resolve type hint for functions get_id and get_text.
  • GroupViewSet > GroupSerializer: Identical component names "Group" found in rdmo.core.serializers.GroupSerializer and rdmo.accounts.serializers.v1.GroupSerializer. Consider renaming one.
  • SitesViewSet > SiteSerializer: Identical component names "Site" found in rdmo.core.serializers.SiteSerializer and rdmo.accounts.serializers.v1.SiteSerializer. Consider renaming one.

Viewsets: viewsets.py

  • SettingsViewSet: Exception raised while getting serializer. Ensure get_serializer_class() is not returning None and get_queryset() works.
  • TemplatesViewSet: Similar issue as above.

3. /rdmo/domain/

Serializers: serializers/v1.py

  • AttributeViewSet > AttributeListSerializer: Unable to resolve type hints for get_model and is_leaf_node.
  • AttributeViewSet > AttributeSerializer: Unable to resolve type hints for get_model, get_tasks, and get_attributes.

Viewsets: viewsets.py

  • AttributeViewSet: Could not derive type of path parameter "var" due to missing field in rdmo.domain.models.Attribute.

4. /rdmo/management/

Viewsets: viewsets.py

  • ImportViewSet, MetaViewSet, UploadViewSet: Unable to guess serializer. Consider using GenericAPIView or explicitly adding serializer_class.

5. /rdmo/options/

Serializers: serializers/v1/option.py

  • OptionViewSet > OptionSerializer: Unable to resolve type hints for get_model, text, help, view_text, label, and get_warning.

Serializers: serializers/v1/optionset.py

  • OptionSetViewSet > OptionSetSerializer: Unable to resolve type hints for get_model and get_condition_uris.

Viewsets: viewsets.py

  • OptionViewSet, OptionSetViewSet: Path parameter "var" could not be derived due to missing fields in rdmo.options.models.Option and rdmo.options.models.OptionSet.

6. /rdmo/projects/

Serializers: serializers/v1/__init__.py

  • MembershipViewSet > MembershipSerializer: Identical component names "Membership" found in rdmo.projects.serializers.v1.MembershipSerializer and rdmo.accounts.serializers.v1.MembershipSerializer. Consider renaming one.
  • ProjectViewSet > ProjectSerializer: Unable to resolve type hint for catalog_uri.
  • ProjectViewSet > ProjectSerializer > UserSerializer: Identical component names "User" found in rdmo.projects.serializers.v1.UserSerializer and rdmo.accounts.serializers.v1.UserSerializer. Consider renaming one.

Viewsets: viewsets.py

  • MembershipViewSet: Failed to obtain model due to missing queryset. Fix by setting queryset = Model.objects.none() or using @extend_schema.

7. /rdmo/questions/

Serializers: serializers/v1/catalog.py

  • CatalogViewSet > CatalogSerializer: Identical component names "Catalog" found in rdmo.questions.serializers.v1.catalog.CatalogSerializer and rdmo.projects.serializers.v1.overview.CatalogSerializer. Consider renaming one.
  • Unable to resolve type hints for get_model, title, help, and get_warning.

Viewsets: viewsets.py

  • CatalogViewSet, PageViewSet, QuestionViewSet, QuestionSetViewSet, SectionViewSet: Path parameter "var" could not be derived due to missing fields.

8. /rdmo/tasks/

Serializers: serializers/v1.py

  • TaskViewSet > TaskSerializer: Unable to resolve type hints for get_model, title, text, get_warning, and get_condition_uris.

Viewsets: viewsets.py

  • TaskViewSet: Path parameter "var" could not be derived due to missing field.

9. /rdmo/views/

Serializers: serializers/v1.py

  • ViewViewSet > ViewSerializer: Unable to resolve type hints for get_model, title, help, and get_warning.

Viewsets: viewsets.py

  • ViewViewSet: Path parameter "var" could not be derived due to missing field.

10. General API Operation ID Collisions

  • Fix operation ID collisions in endpoints related to:
    • conditions_conditions_export_retrieve
    • domain_attributes_export_retrieve
    • options_options_export_retrieve
    • options_optionsets_export_retrieve
    • questions_catalogs_export_retrieve
    • questions_pages_export_retrieve
    • questions_questions_export_retrieve
    • questions_questionsets_export_retrieve
    • questions_sections_export_retrieve
    • tasks_tasks_export_retrieve
    • views_views_export_retrieve

Types of Changes

Mix between bug fix (deprecated library is replaced), new feature (schema is enhanced) and maybe breaking change (schema is different from before).

Checklist

  • I have read the contributor guide.
  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Refs

@MyPyDavid MyPyDavid changed the base branch from main to dependency-updates February 21, 2025 08:27
@MyPyDavid
Copy link
Member Author

Something was discussed about the urlpatterns:
#1050 (comment)

@MyPyDavid MyPyDavid added type:maintenance status:work-in-progress dependencies Pull requests that update a dependency file labels Feb 21, 2025
@jochenklar jochenklar added this to the RDMO 2.3.0 milestone Feb 21, 2025
@jochenklar
Copy link
Member

About the url patterns: I think they are fine, I would move the schema generation to /api/v1/openapi/ and leave the root /api/v1 empty or add a minimal template. With the 3 links.

@jochenklar
Copy link
Member

I managed to fix a lot of warnings by adding return value type hints to (a) model properties and (b) SerializerMethodField methods. I am still not a fan, but I can live with those. I think its better than to inlcude the decorator from drf-spectacular. It could even be an optional dependency. For the remaining problems this could be the way: https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-5-extensions

I think all of this should be done after at least the release candidate and we live with the warnings for now.

@MyPyDavid MyPyDavid changed the base branch from dependency-updates to 2.3.0 February 24, 2025 09:59
@MyPyDavid MyPyDavid removed this from the RDMO 2.3.0 milestone Feb 26, 2025
@MyPyDavid MyPyDavid changed the title feat: replace django-rest-swagger with drf-spectacular drf-spectacular: replace django-rest-swagger with drf-spectacular Mar 11, 2025
@MyPyDavid MyPyDavid changed the title drf-spectacular: replace django-rest-swagger with drf-spectacular drf-spectacular: replace django-rest-swagger Mar 11, 2025
@MyPyDavid MyPyDavid added this to the RDMO 2.3.0 milestone Mar 18, 2025
@jochenklar
Copy link
Member

Ok, Update:

  • I moved the whole thing to an optional dependencies, to work you need to add
    INSTALLED_APPS += [
        'drf_spectacular'
    ]
    
    REST_FRAMEWORK.update({
        'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
        'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning'
    })
    
    to the settings.
  • I added type hints to remove some of the warnings.
  • I fixed the regexes for the export and detail_export actions
  • I added an API landing page:
    image
  • I renamed some serializers and used ref_name on others (to remove some warnings)
  • I added a schema.py with some overrides to fix the other things drf-spectacular cannot figuer out on its own.

Besides ref_name (and the hints) nothing is in the regular rdmo source code, which was important to me.

@jochenklar
Copy link
Member

@MyPyDavid I disabled the tests for the now optional openapi endpoint for now. We need to discuss if and how we include this in the tests.

@jochenklar jochenklar marked this pull request as ready for review March 25, 2025 20:44
@jochenklar
Copy link
Member

The settings for the app are now:

INSTALLED_APPS += [
    'drf_spectacular'
]

REST_FRAMEWORK.update({
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
    'DEFAULT_VERSION': 'v1',
    'ALLOWED_VERSIONS': ('v1', ),
})

path('schema/', login_required(SpectacularAPIView.as_view()), name='schema'),
path('swagger/', login_required(SpectacularSwaggerView.as_view(url_name='v1-openapi:schema')), name='swagger'),
path('redoc/', login_required(SpectacularRedocView.as_view(url_name='v1-openapi:schema')), name='redoc'),
path('', RedirectView.as_view(pattern_name='api', permanent=False))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I include in my rdmo-app

urlpatterns += [
    path('api/v1/', include('rdmo.core.urls.v1.openapi', namespace='v1')),
]

I get an error for http://localhost:8000/api/v1/

Reverse for 'api' not found. 'api' is not a valid view function or pattern name.

Where is this landing page supposed to be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from django.contrib import admin
from django.urls import include, path

from rdmo.core.views import about, api, home

urlpatterns = [
    path('', home, name='home'),
    path('about/', about, name='about'),
    path('api/', api, name='api'),

    path('', include('rdmo.core.urls')),
    path('api/v1/', include('rdmo.core.urls.v1')),
    path('api/v1/', include('rdmo.core.urls.v1.openapi')),

    path('admin/', admin.site.urls)
]

@MyPyDavid
Copy link
Member Author

is ready but will be merged after (or at the end of) 2.3.0-more-fixes (still needs to be rebased)

@jochenklar jochenklar merged commit 9ecb250 into 2.3.0 Mar 28, 2025
8 of 12 checks passed
@jochenklar jochenklar deleted the drf-spectacular branch March 28, 2025 10:11
@afuetterer
Copy link
Member

The schema file django-rest-swagger.yaml was generated for testing purposes only, it was meant to be deleted after demonstrating drf-spectular's capacity. It has no purpose being in the root dir anymore.

@afuetterer
Copy link
Member

@afuetterer
Copy link
Member

I think this can really be deleted, archived or otherwise stated as outdated: https://github.com/rdmorganiser/rdmo-openapi.

@jochenklar
Copy link
Member

What about these tests? https://github.com/rdmorganiser/rdmo/blob/2.3.0/rdmo/core/tests/test_openapi.py

Delete?

I addressed this in #1298.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file type:maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants