Skip to content

Commit c264a3b

Browse files
authored
Support custom data type for DataTypeAttribute (#3868)
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.
1 parent 265e280 commit c264a3b

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/OpenApiSchemaExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ private static void ApplyDataTypeAttribute(OpenApiSchema schema, DataTypeAttribu
173173
{
174174
schema.Format = format;
175175
}
176+
else if (dataTypeAttribute.DataType == AnnotationsDataType.Custom)
177+
{
178+
schema.Format = dataTypeAttribute.CustomDataType;
179+
}
176180
}
177181

178182
private static void ApplyMinLengthAttribute(OpenApiSchema schema, MinLengthAttribute minLengthAttribute)

test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void ActionWithParameterWithBindNeverAttribute([BindNever] string param)
3434
public void ActionWithParameterWithRequiredAttribute([Required] string param)
3535
{ }
3636

37+
public void ActionWithParameterWithCustomDataTypeAttribute([DataType("uuid")] string param)
38+
{ }
39+
3740
public void ActionWithParameterWithBindRequiredAttribute([BindRequired] string param)
3841
{ }
3942

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/OpenApiSchemaExtensionsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ public static void ApplyValidationAttributes_Handles_Invalid_RangeAttribute_Valu
134134
Assert.Null(schema.Maximum);
135135
}
136136

137+
[Fact]
138+
public static void ApplyValidationAttributes_Handles_DataTypeAttribute_CustomDataType_Correctly()
139+
{
140+
// Arrange
141+
string customDataType = "uuid";
142+
var dataTypeAttribute = new DataTypeAttribute(customDataType);
143+
var schema = new OpenApiSchema();
144+
145+
// Act
146+
schema.ApplyValidationAttributes([dataTypeAttribute]);
147+
148+
// Assert
149+
Assert.Equal(customDataType, schema.Format);
150+
}
151+
137152
private sealed class CultureSwitcher : IDisposable
138153
{
139154
private readonly CultureInfo _previous;

test/Swashbuckle.AspNetCore.SwaggerGen.Test/VerifyTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,33 @@ public async Task ApiParameterIsBoundToPath()
622622
await Verify(document);
623623
}
624624

625+
[Fact]
626+
public async Task ActionWithCustomDataTypeQueryParameter()
627+
{
628+
var subject = Subject(
629+
apiDescriptions:
630+
[
631+
ApiDescriptionFactory.Create(
632+
methodInfo: typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameterWithCustomDataTypeAttribute)),
633+
groupName: "v1",
634+
httpMethod: "POST",
635+
relativePath: "resource",
636+
parameterDescriptions:
637+
[
638+
new ApiParameterDescription
639+
{
640+
Name = "param",
641+
Source = BindingSource.Query
642+
}
643+
])
644+
]
645+
);
646+
647+
var document = subject.GetSwagger("v1");
648+
649+
await Verify(document);
650+
}
651+
625652
[Theory]
626653
[InlineData(nameof(FakeController.ActionWithParameterWithRequiredAttribute))]
627654
[InlineData(nameof(FakeController.ActionWithParameterWithBindRequiredAttribute))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Test API",
5+
"version": "V1"
6+
},
7+
"paths": {
8+
"/resource": {
9+
"post": {
10+
"tags": [
11+
"Fake"
12+
],
13+
"parameters": [
14+
{
15+
"name": "param",
16+
"in": "query",
17+
"schema": {
18+
"type": "string",
19+
"format": "uuid"
20+
}
21+
}
22+
],
23+
"responses": {
24+
"200": {
25+
"description": "OK"
26+
}
27+
}
28+
}
29+
}
30+
},
31+
"components": { },
32+
"tags": [
33+
{
34+
"name": "Fake"
35+
}
36+
]
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Test API",
5+
"version": "V1"
6+
},
7+
"paths": {
8+
"/resource": {
9+
"post": {
10+
"tags": [
11+
"Fake"
12+
],
13+
"parameters": [
14+
{
15+
"name": "param",
16+
"in": "query",
17+
"schema": {
18+
"type": "string",
19+
"format": "uuid"
20+
}
21+
}
22+
],
23+
"responses": {
24+
"200": {
25+
"description": "OK"
26+
}
27+
}
28+
}
29+
}
30+
},
31+
"components": { },
32+
"tags": [
33+
{
34+
"name": "Fake"
35+
}
36+
]
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Test API",
5+
"version": "V1"
6+
},
7+
"paths": {
8+
"/resource": {
9+
"post": {
10+
"tags": [
11+
"Fake"
12+
],
13+
"parameters": [
14+
{
15+
"name": "param",
16+
"in": "query",
17+
"schema": {
18+
"type": "string",
19+
"format": "uuid"
20+
}
21+
}
22+
],
23+
"responses": {
24+
"200": {
25+
"description": "OK"
26+
}
27+
}
28+
}
29+
}
30+
},
31+
"components": { },
32+
"tags": [
33+
{
34+
"name": "Fake"
35+
}
36+
]
37+
}

0 commit comments

Comments
 (0)