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

Allow specifying type aliases to provide schema definitions #330

Merged
merged 1 commit into from
Mar 25, 2024

Conversation

lsdch
Copy link
Contributor

@lsdch lsdch commented Mar 23, 2024

This is meant to allow registering type aliases to be used when generating Schema. It is an alternative to implementing the SchemaProvider interface, which is not always very convenient (e.g. when dealing with types from external packages which can not be directly extended).

Example usage:

registry := huma.NewMapRegistry("#/components/schemas", huma.DefaultSchemaNamer)

registry.RegisterTypeAlias(reflect.TypeFor[some_external_pkg.OptionalStr](), reflect.TypeFor[string]())
registry.RegisterTypeAlias(reflect.TypeFor[some_external_pkg.OptionalDateTime](), reflect.TypeFor[time.Time]())

Inspired from a similar feature provided in Go swag

@lsdch lsdch force-pushed the main branch 2 times, most recently from 0e87b84 to 86ccbb3 Compare March 23, 2024 17:46
@lsdch
Copy link
Contributor Author

lsdch commented Mar 23, 2024

Sorry, the test was using reflect.TypeFor which was only introduced in Go 1.22

@lsdch
Copy link
Contributor Author

lsdch commented Mar 24, 2024

I made sure the linter is happy this time, my local setup was using the --fast flag and did not detect this error

Copy link

codecov bot commented Mar 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.29%. Comparing base (7b6dc40) to head (3b9f6e7).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #330   +/-   ##
=======================================
  Coverage   95.28%   95.29%           
=======================================
  Files          19       19           
  Lines        2822     2827    +5     
=======================================
+ Hits         2689     2694    +5     
  Misses         97       97           
  Partials       36       36           

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

@danielgtaylor danielgtaylor merged commit 38cf30c into danielgtaylor:main Mar 25, 2024
3 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.

None yet

2 participants