Skip to content

feat: nullable field#54

Merged
j03-dev merged 2 commits intomainfrom
feat/ser/nullable
Jul 7, 2025
Merged

feat: nullable field#54
j03-dev merged 2 commits intomainfrom
feat/ser/nullable

Conversation

@j03-dev
Copy link
Copy Markdown
Owner

@j03-dev j03-dev commented Jul 7, 2025

Summary by CodeRabbit

  • New Features

    • Added support for nullable fields and arrays in serializers and generated JSON schemas.
    • Fields can now be explicitly marked as nullable, allowing for more flexible data validation and schema generation.
  • Bug Fixes

    • Corrected attribute usage in tests to reference validated_data instead of the outdated validate_data.
  • Tests

    • Updated and extended tests to cover nullable fields and nested nullable arrays in serializers.

j03-dev added 2 commits July 7, 2025 09:50
title
description
minimum
maximum

they are needed
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 7, 2025

Walkthrough

The changes introduce explicit support for nullable fields in the serializer system by adding a nullable flag to field definitions and schema generation. Descriptive metadata and numeric constraints are removed from fields. Method and attribute names are updated for consistency, and test cases are adjusted to verify the handling of nullable fields and arrays.

Changes

File(s) Change Summary
src/serializer/fields.rs Added nullable to Field struct; removed minimum, maximum, title, description; updated constructors and schema logic for nullable support.
src/serializer/mod.rs Renamed validate_data to validated_data; added nullable to constructors and schema; updated method signatures for create, save, update, and schema generation; removed title/description handling.
tests/test.py Updated tests to check validated_data; added nullable list field to Dog; made dog field in User nullable; updated expected schema and test data for null handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Serializer
    participant Field

    User->>Serializer: Define serializer with fields (nullable or not)
    Serializer->>Field: Create Field(nullable=...)
    User->>Serializer: Call validate(data)
    Serializer->>Field: Validate each field (consider nullable)
    Field-->>Serializer: Return validation result
    Serializer-->>User: Store in validated_data
    User->>Serializer: Request JSON schema
    Serializer->>Field: Generate schema (type or [type, "null"])
    Field-->>Serializer: Return schema part
    Serializer-->>User: Return full JSON schema
Loading

Poem

In fields where data hops and plays,
A nullable breeze now comes our way.
No more minimums, titles, or tales—
Just pure types, with null in their sails.
Tests now cheer for what’s allowed,
As rabbits hop through schema clouds!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Clippy (1.86.0)
Updating crates.io index

warning: failed to write cache, path: /usr/local/registry/index/index.crates.io-1949cf8c6b5b557f/.cache/ah/as/ahash, error: Permission denied (os error 13)
Downloading crates ...
Downloaded zerofrom v0.1.6
error: failed to create directory /usr/local/registry/cache/index.crates.io-1949cf8c6b5b557f

Caused by:
Permission denied (os error 13)


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7c248c4 and 06f69b2.

📒 Files selected for processing (3)
  • src/serializer/fields.rs (5 hunks)
  • src/serializer/mod.rs (7 hunks)
  • tests/test.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: musllinux (ubuntu-22.04, x86_64)
  • GitHub Check: windows (windows-latest, x64)
  • GitHub Check: windows (windows-latest, x86)
  • GitHub Check: linux (ubuntu-22.04, x86_64)
  • GitHub Check: musllinux (ubuntu-22.04, armv7)
  • GitHub Check: musllinux (ubuntu-22.04, aarch64)
  • GitHub Check: linux (ubuntu-22.04, aarch64)
  • GitHub Check: linux (ubuntu-22.04, armv7)
  • GitHub Check: macos (macos-14, aarch64)
  • GitHub Check: linux (ubuntu-22.04, s390x)
  • GitHub Check: linux (ubuntu-22.04, ppc64le)
  • GitHub Check: macos (macos-13, x86_64)
  • GitHub Check: linux (ubuntu-22.04, x86)
  • GitHub Check: musllinux (ubuntu-22.04, x86)
🔇 Additional comments (8)
tests/test.py (2)

25-26: LGTM! Correct attribute name update.

The change from validate_data to validated_data aligns with the updated attribute name in the Serializer implementation.


36-67: Well-structured test for nullable field support.

The test correctly validates:

  • Nullable array fields with toys = serializer.CharField(many=True, nullable=True)
  • Nullable nested serializers with dog = Dog(nullable=True)
  • Proper JSON schema generation with nullable types using the array format ["type", "null"]

The schema assertions properly verify that nullable fields generate the expected JSON Schema draft-07 compliant output.

src/serializer/fields.rs (3)

6-64: Clean refactoring to support nullable fields.

The changes effectively:

  • Remove unused metadata fields (minimum, maximum, title, description)
  • Add the nullable field with proper PyO3 getter annotation
  • Update the constructor with a sensible default (nullable = false)

The simplified structure focuses on essential field validation properties.


68-122: Correct implementation of nullable type in JSON Schema.

The schema generation properly handles nullable fields by:

  • Using the JSON Schema array format ["type", "null"] when nullable is true
  • Maintaining backward compatibility with non-nullable fields
  • Applying nullable logic consistently to both base types and array types

The capacity calculation is correctly updated to exclude the removed fields.


124-184: Consistent nullable support across all field types.

The macro updates ensure all field types (IntegerField, CharField, etc.) support the nullable parameter with the same default behavior.

src/serializer/mod.rs (3)

36-98: Improved naming consistency and nullable support.

The changes correctly:

  • Rename validate_data to validated_data for better clarity
  • Add nullable parameter to the constructor with appropriate default
  • Update all references to use the new attribute name

161-197: Improved API design with instance returns.

The changes enhance the API by:

  • Returning created/saved/updated instances for better method chaining
  • Using HashMap<String, PyObject> for validated_data in update method for type safety
  • Maintaining consistent return patterns across CRUD operations

These improvements align with common ORM patterns and enhance developer experience.


204-287: Comprehensive nullable support in schema generation.

The implementation correctly:

  • Propagates nullable flag recursively through nested serializers
  • Handles nullable arrays with proper JSON Schema format ["array", "null"]
  • Handles nullable objects with format ["object", "null"]
  • Maintains backward compatibility for non-nullable fields

The caching mechanism is preserved and the implementation follows JSON Schema draft-07 conventions.

✨ Finishing Touches
  • 📝 Docstrings were successfully generated. (🔄 Check again to generate docstrings again)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai bot added a commit that referenced this pull request Jul 7, 2025
Docstrings generation was requested by @j03-dev.

* #54 (comment)

The following files were modified:

* `src/serializer/fields.rs`
* `src/serializer/mod.rs`
* `tests/test.py`
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 7, 2025

Note

Generated docstrings for this pull request at #55

@j03-dev j03-dev merged commit ac4cae5 into main Jul 7, 2025
17 checks passed
@j03-dev j03-dev linked an issue Jul 7, 2025 that may be closed by this pull request
@j03-dev j03-dev deleted the feat/ser/nullable branch July 7, 2025 07:08
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.

Nullable field in serializer

1 participant