Skip to content

Support custom data type for DataTypeAttribute#3868

Merged
martincostello merged 2 commits intodomaindrivendev:masterfrom
tayfunyuksel:feat/custom-data-type
Mar 25, 2026
Merged

Support custom data type for DataTypeAttribute#3868
martincostello merged 2 commits intodomaindrivendev:masterfrom
tayfunyuksel:feat/custom-data-type

Conversation

@tayfunyuksel
Copy link
Contributor

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:

[DataType("uuid")]
public string Id { get; set; }

Which will correctly generate the following OpenAPI schema:

{
  "type": "string",
  "format": "uuid"
}

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.
Copy link
Collaborator

@martincostello martincostello left a comment

Choose a reason for hiding this comment

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

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?

- 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 martincostello added this to the v10.1.7 milestone Mar 25, 2026
@codecov
Copy link

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.01%. Comparing base (265e280) to head (722bf25).
⚠️ Report is 1 commits behind head on master.

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           
Flag Coverage Δ
Linux 95.01% <100.00%> (+<0.01%) ⬆️
Windows 95.01% <100.00%> (+<0.01%) ⬆️
macOS 95.01% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@martincostello martincostello merged commit c264a3b into domaindrivendev:master Mar 25, 2026
13 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.

2 participants