Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly set up constant fields in API responses #3035

Merged
merged 2 commits into from
Sep 20, 2023

Conversation

obulat
Copy link
Contributor

@obulat obulat commented Sep 19, 2023

Fixes

Fixes #3034 by @obulat

Description

This PR replaces ReadOnlyFields with SerializerMethodFields that return constant values for values that never change and are not derived from models:
OEmbedSerializer: version, type
ProviderSerializer: logo_url

This makes the documentation a little bit clearer and fixes the warnings in logs.

Currently, logo_url type is given as string, but it always returns null. The updated version shows string or null. I tried setting it to "only null", but spectacular still throws a warning that it couldn't get the type of the value. string or null is more accurate than string.
Provider serializer logo url type

OEmbed serializer

Currently, the documentation says that these values are "default", making it seem like a different value is also possible.
OEmbed Serializer current

In this PR, the documentation says "Value: 1.0", making it clear that it's only a single value.
OEmbed Serializer updated

Testing Instructions

If you run the app on main (just up), and open the documentation (e.g. http://0.0.0.0:50280/v1/#tag/audio/operation/audio_stats), you should see the warnings mentioned in the related issue in the Docker logs.
These warnings should not be visible in this PR. The documentation for these fields should be updated to match snapshots in the PR description.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@obulat obulat requested a review from a team as a code owner September 19, 2023 06:03
@github-actions github-actions bot added the 🧱 stack: api Related to the Django API label Sep 19, 2023
@openverse-bot openverse-bot added 🟩 priority: low Low priority and doesn't need to be rushed ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels Sep 19, 2023
Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

While the change to the documentation is good, something feels off about using a "method" field to generate a constant value. One alternative might be to create a custom field but that seems like over-engineering.

Approving because I don't know a better way.

@obulat
Copy link
Contributor Author

obulat commented Sep 19, 2023

Added args that are discarded (*args) to the get_type and get_version functions to fix TypeError: OembedSerializer.get_version() takes 0 positional arguments but 1 was given error that's causing error in the oembed endpoint: 490095d

@obulat obulat force-pushed the fix/constant_serializer_fields branch from 490095d to 242d72f Compare September 19, 2023 07:04
@obulat obulat force-pushed the fix/constant_serializer_fields branch from 242d72f to d66de3a Compare September 19, 2023 14:27
@sarayourfriend
Copy link
Collaborator

sarayourfriend commented Sep 19, 2023

The swagger docs say, regarding "null" (TIL):

Note that there is no null type; instead, the nullable attribute is used as a modifier of the base type.

So a field cannot only be null, it seems, and within spec.

For the static fields, I wrote this custom field that seems to work:

class StaticField(serializers.Field):
    def __new__(cls, value, **kwargs):
        decorated = extend_schema_field({"type": "string", "nullable": True} if value is None else type(value))(cls)
        return super().__new__(decorated, value, **kwargs)

    def __init__(self, value, **kwargs):
        self.value = value
        kwargs["source"] = "*"
        kwargs["read_only"] = True

        super().__init__(**kwargs)

    def to_representation(self):
        return self.value

The __new__ method is necessary to pull the type from the passed value (or set it to a nullable string, if the value is None). extend_schema_field comes from drf_spectacular.utils. Not sure whether it's necessary, the method fields work just fine, but if you want something a little more "magical", there you go 🙂

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

LGTM!

@obulat
Copy link
Contributor Author

obulat commented Sep 20, 2023

but if you want something a little more "magical", there you go 🙂

Thank you for the implementation and sharing the info on nullability 😄 I think we can go with what we have here, and change if needed later, if this starts to cause problems

@obulat obulat merged commit d576df6 into main Sep 20, 2023
@obulat obulat deleted the fix/constant_serializer_fields branch September 20, 2023 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: api Related to the Django API
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Fix the API documentation set up for the constant fields
4 participants