Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ private static void ApplyDataTypeAttribute(OpenApiSchema schema, DataTypeAttribu
{
schema.Format = format;
}
else if (dataTypeAttribute.DataType == AnnotationsDataType.Custom)
{
schema.Format = dataTypeAttribute.CustomDataType;
}
}

private static void ApplyMinLengthAttribute(OpenApiSchema schema, MinLengthAttribute minLengthAttribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public void ActionWithParameterWithBindNeverAttribute([BindNever] string param)
public void ActionWithParameterWithRequiredAttribute([Required] string param)
{ }

public void ActionWithParameterWithCustomDataTypeAttribute([DataType("uuid")] string param)
{ }

public void ActionWithParameterWithBindRequiredAttribute([BindRequired] string param)
{ }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ public static void ApplyValidationAttributes_Handles_Invalid_RangeAttribute_Valu
Assert.Null(schema.Maximum);
}

[Fact]
public static void ApplyValidationAttributes_Handles_DataTypeAttribute_CustomDataType_Correctly()
{
// Arrange
string customDataType = "uuid";
var dataTypeAttribute = new DataTypeAttribute(customDataType);
var schema = new OpenApiSchema();

// Act
schema.ApplyValidationAttributes([dataTypeAttribute]);

// Assert
Assert.Equal(customDataType, schema.Format);
}

private sealed class CultureSwitcher : IDisposable
{
private readonly CultureInfo _previous;
Expand Down
27 changes: 27 additions & 0 deletions test/Swashbuckle.AspNetCore.SwaggerGen.Test/VerifyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,33 @@ public async Task ApiParameterIsBoundToPath()
await Verify(document);
}

[Fact]
public async Task ActionWithCustomDataTypeQueryParameter()
{
var subject = Subject(
apiDescriptions:
[
ApiDescriptionFactory.Create(
methodInfo: typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameterWithCustomDataTypeAttribute)),
groupName: "v1",
httpMethod: "POST",
relativePath: "resource",
parameterDescriptions:
[
new ApiParameterDescription
{
Name = "param",
Source = BindingSource.Query
}
])
]
);

var document = subject.GetSwagger("v1");

await Verify(document);
}

[Theory]
[InlineData(nameof(FakeController.ActionWithParameterWithRequiredAttribute))]
[InlineData(nameof(FakeController.ActionWithParameterWithBindRequiredAttribute))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.0.4",
"info": {
"title": "Test API",
"version": "V1"
},
"paths": {
"/resource": {
"post": {
"tags": [
"Fake"
],
"parameters": [
{
"name": "param",
"in": "query",
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": { },
"tags": [
{
"name": "Fake"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.0.4",
"info": {
"title": "Test API",
"version": "V1"
},
"paths": {
"/resource": {
"post": {
"tags": [
"Fake"
],
"parameters": [
{
"name": "param",
"in": "query",
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": { },
"tags": [
{
"name": "Fake"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"openapi": "3.0.4",
"info": {
"title": "Test API",
"version": "V1"
},
"paths": {
"/resource": {
"post": {
"tags": [
"Fake"
],
"parameters": [
{
"name": "param",
"in": "query",
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": { },
"tags": [
{
"name": "Fake"
}
]
}
Loading