Support custom data type for DataTypeAttribute#3868
Merged
martincostello merged 2 commits intodomaindrivendev:masterfrom Mar 25, 2026
Merged
Support custom data type for DataTypeAttribute#3868martincostello merged 2 commits intodomaindrivendev:masterfrom
martincostello merged 2 commits intodomaindrivendev:masterfrom
Conversation
Adds support for the DataTypeAttribute when a custom data type string is provided (e.g., [DataType("uuid")]). This will set the format property on the corresponding OpenAPI schema.
A new unit test is included to verify this behavior.
Collaborator
martincostello
left a comment
There was a problem hiding this comment.
Could you add-to/update the integration tests so we have at least one snapshot that verifies that the custom format is serialized to the OpenAPI document?
src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs
Outdated
Show resolved
Hide resolved
- Adds a test case to verify that the `[DataType]` attribute correctly sets the schema format for query parameters. - Removes an unnecessary blank line in `OpenApiSchemaExtensions.cs`. - Includes verified snapshots for .NET 8.0, 9.0, and 10.0.
martincostello
approved these changes
Mar 25, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3868 +/- ##
=======================================
Coverage 95.01% 95.01%
=======================================
Files 111 111
Lines 3910 3912 +2
Branches 788 789 +1
=======================================
+ Hits 3715 3717 +2
Misses 195 195
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds support for the DataTypeAttribute when a custom data type string is provided (e.g., [DataType("uuid")]). This will set the format property on the corresponding OpenAPI schema.
A new unit test is included to verify this behavior.
Pull Request
Adds support for mapping custom DataType attributes to the format property of an OpenAPI schema.
The issue or feature being addressed
Currently, Swashbuckle.AspNetCore maps well-known System.ComponentModel.DataAnnotations.DataType enum values (like DateTime, Password, EmailAddress) to their respective OpenAPI formats. However, it did not support custom string-based DataType values.
This PR checks if DataTypeAttribute.DataType is AnnotationsDataType.Custom. If it is, it assigns the value of DataTypeAttribute.CustomDataType to the schema.Format.
According to the OpenAPI 3.0.0 Specification - Data Types, the format property is an open string-valued property that can have any value. The spec explicitly notes:
OpenAPI Specification - Data Types
"Primitives have an optional modifier property: format. OAS uses several known formats to define in fine detail the data type being used. However, to support documentation needs, the format property is an open string-valued property, and can have any value. Formats such as "email", "uuid", and so on, MAY be used even though undefined by this specification. Types that are not accompanied by a format property follow the type definition in the JSON Schema. Tools that do not recognize a specific format MAY default back to the type alone, as if the format is not specified."
Details on the issue fix or feature implementation
With this change, developers can easily add custom formats to their properties like so:
Which will correctly generate the following OpenAPI schema:
{ "type": "string", "format": "uuid" }