Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Feb 11, 2024
1 parent 22d1e92 commit d4421c8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
if (apiParameter is not null)
{
parameter.Schema = GenerateSchema(
apiParameter.ModelMetadata.ModelType,
apiParameter.Type,
schemaRepository,
apiParameter.PropertyInfo(),
apiParameter.ParameterInfo(),
Expand All @@ -293,7 +293,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
if (requestParameter is not null)
{
content.Schema = GenerateSchema(
requestParameter.ModelMetadata.ModelType,
requestParameter.Type,
schemaRepository,
requestParameter.PropertyInfo(),
requestParameter.ParameterInfo());
Expand Down Expand Up @@ -359,7 +359,7 @@ private IList<OpenApiParameter> GenerateParameters(ApiDescription apiDescription

var schema = (apiParameter.ModelMetadata != null)
? GenerateSchema(
apiParameter.ModelMetadata.ModelType,
apiParameter.Type,
schemaRepository,
apiParameter.PropertyInfo(),
apiParameter.ParameterInfo(),
Expand Down Expand Up @@ -463,7 +463,7 @@ private IList<OpenApiParameter> GenerateParameters(ApiDescription apiDescription
var isRequired = bodyParameter.IsRequiredParameter();

var schema = GenerateSchema(
bodyParameter.ModelMetadata.ModelType,
bodyParameter.Type,
schemaRepository,
bodyParameter.PropertyInfo(),
bodyParameter.ParameterInfo());
Expand Down Expand Up @@ -542,7 +542,7 @@ private IEnumerable<string> InferRequestContentTypes(ApiDescription apiDescripti

var schema = (formParameter.ModelMetadata != null)
? GenerateSchema(
formParameter.ModelMetadata.ModelType,
formParameter.Type,
schemaRepository,
formParameter.PropertyInfo(),
formParameter.ParameterInfo())
Expand Down Expand Up @@ -595,7 +595,7 @@ private IEnumerable<string> InferRequestContentTypes(ApiDescription apiDescripti
Description = description,
Content = responseContentTypes.ToDictionary(
contentType => contentType,
contentType => CreateResponseMediaType(apiResponseType.ModelMetadata, schemaRepository)
contentType => CreateResponseMediaType(apiResponseType.Type, schemaRepository)
)
};
}
Expand All @@ -620,11 +620,11 @@ private IEnumerable<string> InferResponseContentTypes(ApiDescription apiDescript
return Enumerable.Empty<string>();
}

private OpenApiMediaType CreateResponseMediaType(ModelMetadata modelMetadata, SchemaRepository schemaRespository)
private OpenApiMediaType CreateResponseMediaType(Type type, SchemaRepository schemaRespository)
{
return new OpenApiMediaType
{
Schema = GenerateSchema(modelMetadata.ModelType, schemaRespository)
Schema = GenerateSchema(type, schemaRespository)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ public void Apply_EnrichesRequestBodyMetadata_IfEndpointParameterDecoratedWithSw
public void Apply_EnrichesParameterMetadata_IfPropertyDecoratedWithSwaggerRequestBodyAttribute()
{
var requestBody = new OpenApiRequestBody();
var modelMetaData = ModelMetadataFactory.CreateForProperty(typeof(SwaggerAnnotatedType),
nameof(SwaggerAnnotatedType.StringWithSwaggerRequestBodyAttribute));
var bodyParameterDescription = new ApiParameterDescription
{
ModelMetadata = ModelMetadataFactory.CreateForProperty(typeof(SwaggerAnnotatedType), nameof(SwaggerAnnotatedType.StringWithSwaggerRequestBodyAttribute))
ModelMetadata = modelMetaData,
Type = modelMetaData.ModelType,
};
var context = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,11 @@ public int ActionWithProducesAttribute()
{
throw new NotImplementedException();
}

[Produces("application/someMediaType")]
public void VoidActionWithProducesAttribute()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Xunit;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;

namespace DotSwashbuckle.AspNetCore.SwaggerGen.Test
{
Expand Down Expand Up @@ -185,7 +186,7 @@ public void GetSwagger_UseProvidedOpenApiOperation_IfExistsInMetadata()
[Fact]
public void GetSwagger_GenerateProducesSchemas_ForProvidedOpenApiOperation()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithProducesAttribute));
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.VoidActionWithProducesAttribute));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata = new List<object>()
Expand All @@ -210,6 +211,7 @@ public void GetSwagger_GenerateProducesSchemas_ForProvidedOpenApiOperation()
["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty)
}
};

var subject = Subject(
apiDescriptions: new[]
{
Expand All @@ -219,14 +221,8 @@ public void GetSwagger_GenerateProducesSchemas_ForProvidedOpenApiOperation()
groupName: "v1",
httpMethod: "POST",
relativePath: "resource",
supportedResponseTypes: new[]
{
new ApiResponseType()
{
StatusCode = 200,
Type = typeof(TestDto)
}
}),
null
)
}
);

Expand All @@ -235,9 +231,7 @@ public void GetSwagger_GenerateProducesSchemas_ForProvidedOpenApiOperation()
Assert.Equal("OperationIdSetInMetadata", document.Paths["/resource"].Operations[OperationType.Post].OperationId);
var content = Assert.Single(document.Paths["/resource"].Operations[OperationType.Post].Responses["200"].Content);
Assert.Equal("application/someMediaType", content.Key);
Assert.Null(content.Value.Schema.Type);
Assert.NotNull(content.Value.Schema.Reference);
Assert.Equal("TestDto", content.Value.Schema.Reference.Id);
Assert.Null(content.Value.Schema);
}

[Fact]
Expand Down Expand Up @@ -280,7 +274,8 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperation()
{
Name = "param",
Source = BindingSource.Body,
ModelMetadata = ModelMetadataFactory.CreateForType(typeof(TestDto))
ModelMetadata = ModelMetadataFactory.CreateForType(typeof(TestDto)),
Type = typeof(TestDto)
}
}),
}
Expand Down Expand Up @@ -335,7 +330,8 @@ public void GetSwagger_GenerateParametersSchemas_ForProvidedOpenApiOperation()
new ApiParameterDescription
{
Name = "ParameterInMetadata",
ModelMetadata = ModelMetadataFactory.CreateForType(typeof(string))
ModelMetadata = ModelMetadataFactory.CreateForType(typeof(string)),
Type = typeof(string)
}
}),
}
Expand Down Expand Up @@ -799,7 +795,7 @@ public void GetSwagger_GeneratesResponses_ForSupportedResponseTypes()
new ApiResponseType
{
ApiResponseFormats = new [] { new ApiResponseFormat { MediaType = "application/json" } },
IsDefaultResponse = true
IsDefaultResponse = true,
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ public void Apply_SetsDescriptionAndExample_FromPropertySummaryAndExampleTags()
["application/json"] = new OpenApiMediaType { Schema = new OpenApiSchema { Type = "string" } }
}
};
var modelMetaData = ModelMetadataFactory.CreateForProperty(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty));
var bodyParameterDescription = new ApiParameterDescription
{
ModelMetadata = ModelMetadataFactory.CreateForProperty(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty))
ModelMetadata = modelMetaData,
Type = modelMetaData.ModelType
};
var filterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

Expand All @@ -99,9 +101,11 @@ public void Apply_SetsDescriptionAndExample_FromUriTypePropertySummaryAndExample
["application/json"] = new OpenApiMediaType { Schema = new OpenApiSchema { Type = "string" } }
}
};
var modelMetaData = ModelMetadataFactory.CreateForProperty(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithUri));
var bodyParameterDescription = new ApiParameterDescription
{
ModelMetadata = ModelMetadataFactory.CreateForProperty(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithUri))
ModelMetadata = modelMetaData,
Type = modelMetaData.ModelType
};
var filterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
Expand Down Expand Up @@ -45,6 +46,7 @@ public static class ApiDescriptionFactory
if (parameterDescriptorWithParameterInfo != null)
{
parameter.ModelMetadata = ModelMetadataFactory.CreateForParameter(parameterDescriptorWithParameterInfo.ParameterInfo);
parameter.Type = parameter.ModelMetadata.ModelType;
}

apiDescription.ParameterDescriptions.Add(parameter);
Expand All @@ -67,6 +69,7 @@ public static class ApiDescriptionFactory
if (methodInfo.ReturnType != null && responseType.StatusCode/100 == 2)
{
responseType.ModelMetadata = ModelMetadataFactory.CreateForType(methodInfo.ReturnType);
responseType.Type = responseType.ModelMetadata.ModelType;
}

apiDescription.SupportedResponseTypes.Add(responseType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;

namespace DotSwashbuckle.AspNetCore.TestSupport
Expand Down

0 comments on commit d4421c8

Please sign in to comment.