Skip to content

Fix/metadata serialization - #29

Merged
metaforx merged 3 commits into
mainfrom
fix/metadata-serialization
May 5, 2026
Merged

Fix/metadata serialization#29
metaforx merged 3 commits into
mainfrom
fix/metadata-serialization

Conversation

@metaforx

@metaforx metaforx commented May 5, 2026

Copy link
Copy Markdown
Owner

Summary by Sourcery

Expose additional form field metadata through the API and normalize initial values for JSON responses.

New Features:

  • Include placeholder metadata in the form fields API response when defined.
  • Include serialized initial values in the form fields API response for fields with an initial value.

Enhancements:

  • Normalize initial values (including callables and date/datetime instances) into JSON-friendly representations for API responses.

Tests:

  • Add API tests to verify placeholders and initial values are correctly returned in form field metadata.

@sourcery-ai

sourcery-ai Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds serialization of placeholder and initial metadata for form fields in the API, including proper JSON-safe handling of callable and date/datetime initial values, and introduces tests to cover these behaviors.

Sequence diagram for the form fields API metadata serialization

sequenceDiagram
    actor Client
    participant APIView as FormFieldsAPIView
    participant Forms as get_form_fields
    participant Serializer as _serialize_initial

    Client->>APIView: GET /api/forms/{slug}/fields
    APIView->>Forms: get_form_fields(request, slug)
    Forms->>Forms: build fields dict from form
    loop For each field
        Forms->>Forms: widget_attrs = field.widget.attrs
        Forms->>Forms: field_info base attributes
        Forms->>Forms: placeholder = widget_attrs.get(placeholder)
        alt placeholder is not None or empty
            Forms->>Forms: field_info.placeholder = placeholder
        end
        Forms->>Serializer: _serialize_initial(field.initial)
        Serializer->>Serializer: if callable(value) then value()
        Serializer->>Serializer: if value is None return None
        Serializer->>Serializer: if value is date/datetime return isoformat()
        Serializer-->>Forms: initial_value
        alt initial_value is not None
            Forms->>Forms: field_info.initial = initial_value
        end
        Forms->>Forms: add choices if present
    end
    Forms-->>APIView: list of field_info
    APIView-->>Client: JSON response with placeholder and initial metadata
Loading

File-Level Changes

Change Details Files
Expose placeholder and initial metadata for form fields in the form-fields API response.
  • Retrieve widget attrs defensively from each field to avoid attribute errors.
  • Include non-empty placeholder attribute from the field widget in the serialized field info.
  • Include a serialized initial value for each field when present, skipping when None.
src/unfold_fobi/api/views.py
Ensure initial values are serialized safely for JSON responses, including callables and date-like objects.
  • Introduce helper function to normalize/serialize initial values from fields.
  • Handle callable initial values by invoking them before serialization.
  • Convert date and datetime initial values to ISO 8601 strings; otherwise return the value as-is.
src/unfold_fobi/api/views.py
Add tests and fixtures to verify placeholder and initial metadata are present in the API output.
  • Add pytest fixture that builds a form with a text field with placeholder and a date field with an initial value stored in plugin data.
  • Ensure DB form handler is attached so the form is valid for API use.
  • Add a test that fetches the form fields via the API helper and asserts placeholder and initial are correctly exposed.
tests/api/test_form_fields.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@metaforx
metaforx merged commit e5fc794 into main May 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant