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

feat: treat encoding.TextUnmarshaler as string in schema #550

Merged
merged 1 commit into from
Aug 19, 2024

Conversation

danielgtaylor
Copy link
Owner

@danielgtaylor danielgtaylor commented Aug 19, 2024

This PR causes the JSON Schema generation/validation functionality in Huma to treat custom types which implement encoding.TextUnmarshaler as a string type by default. This makes it easier to wrap known types and do things like create your own custom date/time type.

This behavior can be escaped by implementing huma.SchemaProvider for your type.

Fixes #471

Summary by CodeRabbit

  • New Features

    • Introduced support for types that implement the TextUnmarshaler interface, allowing more efficient schema handling for specific data types.
    • Added a custom date type, MyDate, which can parse RFC3339 formatted date strings.
  • Bug Fixes

    • Enhanced the schema generation process to correctly treat MyDate as a string type when unmarshalling JSON data.
  • Tests

    • Implemented tests to verify correct unmarshalling of JSON data into structures containing the new MyDate type.

Copy link

coderabbitai bot commented Aug 19, 2024

Walkthrough

The recent changes introduce support for types that implement the encoding.TextUnmarshaler interface, enhancing schema generation and JSON handling for custom types. Notably, a new date type, MyDate, has been added, allowing for seamless parsing of date strings in RFC3339 format. This update improves type handling in model validation and schema generation, addressing previous issues encountered with custom time types.

Changes

Files Change Summary
registry.go, schema.go Added support for encoding.TextUnmarshaler in the Schema method and schemaFromType function, improving handling of certain types in schema generation.
schema_test.go Introduced MyDate type with UnmarshalText method for RFC3339 date parsing, including tests to validate its integration with JSON operations.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Registry
    participant SchemaHandler

    Client->>Server: Request with custom type
    Server->>Registry: Handle request
    Registry->>SchemaHandler: Generate schema
    SchemaHandler-->>Registry: Return schema
    Registry-->>Server: Return response
    Server-->>Client: Send response with data
Loading

Assessment against linked issues

Objective Addressed Explanation
Panic on validation phase of custom time.Time type (#471)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @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.

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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Copy link

codecov bot commented Aug 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.76%. Comparing base (853cd70) to head (a63c9cb).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #550   +/-   ##
=======================================
  Coverage   92.75%   92.76%           
=======================================
  Files          22       22           
  Lines        3879     3883    +4     
=======================================
+ Hits         3598     3602    +4     
  Misses        236      236           
  Partials       45       45           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 853cd70 and a63c9cb.

Files selected for processing (3)
  • registry.go (2 hunks)
  • schema.go (3 hunks)
  • schema_test.go (2 hunks)
Additional comments not posted (5)
registry.go (1)

96-100: LGTM: Handle encoding.TextUnmarshaler types as strings.

The addition correctly identifies types implementing encoding.TextUnmarshaler and treats them as strings, aligning with the PR objectives to simplify schema handling for custom types.

schema.go (1)

704-710: LGTM: Treat encoding.TextUnmarshaler as strings in schemaFromType.

The implementation correctly treats types implementing encoding.TextUnmarshaler as strings, enhancing the schema's ability to handle diverse data types.

schema_test.go (3)

1176-1176: LGTM: Define custom date type MyDate.

The definition of MyDate as a type alias for time.Time is appropriate for implementing custom unmarshalling behavior.


1178-1185: LGTM: Implement UnmarshalText for MyDate.

The method correctly parses RFC3339 date strings and assigns them to MyDate, ensuring seamless integration with encoding.TextUnmarshaler.


1189-1202: LGTM: Test MyDate integration with schema.

The test successfully verifies that MyDate can be unmarshalled from JSON and is represented as a string in the schema, confirming the intended behavior.

@danielgtaylor danielgtaylor merged commit e1ffc73 into main Aug 19, 2024
7 checks passed
@danielgtaylor danielgtaylor deleted the text-unmarshaler-string-type branch August 19, 2024 15:47
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.

Panic on Validation Phase of custom time.Time Type
1 participant