Skip to content

Commit

Permalink
Update NJS/R
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Oct 31, 2023
1 parent d5c97de commit 8738fd3
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/NSwag.AspNet.WebApi/NSwag.AspNet.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.3" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0-preview006" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="11.0.0-preview006" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0-preview006" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.TypeScript" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.CodeGeneration.TypeScript" Version="11.0.0-preview006" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration/NSwag.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="11.0.0-preview006" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Core.Yaml/NSwag.Core.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema.Yaml" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.Yaml" Version="11.0.0-preview006" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Core/NSwag.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema" Version="11.0.0-preview006" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ private OpenApiParameter CreatePrimitiveParameter(
bool enforceNotNull = false)
{
var contextualParameterType =
extendedApiParameter.ParameterInfo?.ToContextualParameter() as ContextualType ??
extendedApiParameter.ParameterInfo?.ToContextualParameter()?.ParameterType ??
extendedApiParameter.PropertyInfo?.ToContextualProperty()?.PropertyType ??
extendedApiParameter.ParameterType.ToContextualType(extendedApiParameter.Attributes);

Expand Down
3 changes: 1 addition & 2 deletions src/NSwag.Generation.Tests/OpenApiDocumentGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace NSwag.Generation.Tests
{
public class OpenApiDocumentGeneratorTests
{

public class TestController
{
public void HasArrayParameter(string[] foo)
Expand All @@ -34,7 +33,7 @@ private OpenApiParameter GetParameter(SchemaType schemaType)
.Methods
.Single(m => m.Name == "HasArrayParameter");

return generator.CreatePrimitiveParameter("foo", "bar", methodInfo.Parameters.First());
return generator.CreatePrimitiveParameter("foo", "bar", methodInfo.Parameters.First().ParameterType);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ public bool Process(OperationProcessorContext context)

var position = 1;
foreach (var contextualParameter in parameters.Select(p => p.ToContextualParameter())
.Where(p => p.Type != typeof(CancellationToken) &&
!p.ContextAttributes.GetAssignableToTypeName("SwaggerIgnoreAttribute", TypeNameStyle.Name).Any() &&
!p.ContextAttributes.GetAssignableToTypeName("FromServicesAttribute", TypeNameStyle.Name).Any() &&
!p.ContextAttributes.GetAssignableToTypeName("BindNeverAttribute", TypeNameStyle.Name).Any()))
.Where(p => p.ParameterType.Type != typeof(CancellationToken) &&
!p.GetAttributes(true).GetAssignableToTypeName("SwaggerIgnoreAttribute", TypeNameStyle.Name).Any() &&
!p.GetAttributes(true).GetAssignableToTypeName("FromServicesAttribute", TypeNameStyle.Name).Any() &&
!p.GetAttributes(true).GetAssignableToTypeName("BindNeverAttribute", TypeNameStyle.Name).Any()))
{
var parameterName = contextualParameter.Name;

dynamic fromRouteAttribute = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("Microsoft.AspNetCore.Mvc.FromRouteAttribute");
dynamic fromHeaderAttribute = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("Microsoft.AspNetCore.Mvc.FromHeaderAttribute");
dynamic fromFormAttribute = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("Microsoft.AspNetCore.Mvc.FromFormAttribute");
dynamic fromRouteAttribute = contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("Microsoft.AspNetCore.Mvc.FromRouteAttribute");
dynamic fromHeaderAttribute = contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("Microsoft.AspNetCore.Mvc.FromHeaderAttribute");
dynamic fromFormAttribute = contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("Microsoft.AspNetCore.Mvc.FromFormAttribute");

var fromBodyAttribute = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("FromBodyAttribute", TypeNameStyle.Name);
var fromUriAttribute = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("FromUriAttribute", TypeNameStyle.Name) ??
contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("FromQueryAttribute", TypeNameStyle.Name);
var fromBodyAttribute = contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("FromBodyAttribute", TypeNameStyle.Name);
var fromUriAttribute = contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("FromUriAttribute", TypeNameStyle.Name) ??
contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("FromQueryAttribute", TypeNameStyle.Name);

string bodyParameterName = fromBodyAttribute.TryGetPropertyValue<string>("Name") ?? parameterName;
string uriParameterName = fromUriAttribute.TryGetPropertyValue<string>("Name") ?? parameterName;
Expand All @@ -81,7 +81,7 @@ public bool Process(OperationProcessorContext context)
}
else
{
var parameterInfo = _settings.SchemaSettings.ReflectionService.GetDescription(contextualParameter, _settings.SchemaSettings);
var parameterInfo = _settings.SchemaSettings.ReflectionService.GetDescription(contextualParameter.ParameterType, _settings.SchemaSettings);

operationParameter = TryAddFileParameter(context, parameterInfo, contextualParameter);
if (operationParameter == null)
Expand Down Expand Up @@ -118,7 +118,7 @@ public bool Process(OperationProcessorContext context)
if (parameterInfo.IsComplexType)
{
// Check for a custom ParameterBindingAttribute (OWIN/WebAPI only)
var parameterBindingAttribute = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("ParameterBindingAttribute", TypeNameStyle.Name);
var parameterBindingAttribute = contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("ParameterBindingAttribute", TypeNameStyle.Name);
if (parameterBindingAttribute != null && fromBodyAttribute == null && fromUriAttribute == null && !_settings.IsAspNetCore)
{
// If binding attribute is defined in GET method context, it cannot be body parameter and path and form attributes were checked earlier
Expand All @@ -129,7 +129,7 @@ public bool Process(OperationProcessorContext context)
else
{
// Try to find a [WillReadBody] attribute on either the action parameter or the bindingAttribute's class
var willReadBodyAttribute = contextualParameter.ContextAttributes.Concat(parameterBindingAttribute.GetType().GetTypeInfo().GetCustomAttributes())
var willReadBodyAttribute = contextualParameter.GetAttributes(true).Concat(parameterBindingAttribute.GetType().GetTypeInfo().GetCustomAttributes())
.FirstAssignableToTypeNameOrDefault("WillReadBodyAttribute", TypeNameStyle.Name);

if (willReadBodyAttribute == null)
Expand Down Expand Up @@ -302,8 +302,8 @@ private void RemoveUnusedPathParameters(OpenApiOperationDescription operationDes
private OpenApiParameter TryAddFileParameter(
OperationProcessorContext context, JsonTypeDescription typeInfo, ContextualParameterInfo contextualParameter)
{
var isFileArray = IsFileArray(contextualParameter.Type, typeInfo);
var hasSwaggerFileAttribute = contextualParameter.Attributes
var isFileArray = IsFileArray(contextualParameter.ParameterType.Type, typeInfo);
var hasSwaggerFileAttribute = contextualParameter.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("SwaggerFileAttribute", TypeNameStyle.Name) != null;

if (typeInfo.Type == JsonObjectType.File ||
Expand All @@ -322,7 +322,7 @@ private OpenApiParameter AddFileParameter(OperationProcessorContext context, Con
// TODO: Check if there is a way to control the property name
var parameterDocumentation = contextualParameter.GetDescription(_settings.SchemaSettings);
var operationParameter = context.DocumentGenerator.CreatePrimitiveParameter(
contextualParameter.Name, parameterDocumentation, contextualParameter);
contextualParameter.Name, parameterDocumentation, contextualParameter.ParameterType);

InitializeFileParameter(operationParameter, isFileArray);
context.OperationDescription.Operation.Parameters.Add(operationParameter);
Expand Down Expand Up @@ -354,12 +354,12 @@ private OpenApiParameter AddBodyParameter(OperationProcessorContext context, str
{
OpenApiParameter operationParameter;

var typeDescription = _settings.SchemaSettings.ReflectionService.GetDescription(contextualParameter, _settings.SchemaSettings);
var isRequired = _settings.AllowNullableBodyParameters == false || contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("RequiredAttribute", TypeNameStyle.Name) != null;
var typeDescription = _settings.SchemaSettings.ReflectionService.GetDescription(contextualParameter.ParameterType, _settings.SchemaSettings);
var isRequired = _settings.AllowNullableBodyParameters == false || contextualParameter.GetAttributes(true).FirstAssignableToTypeNameOrDefault("RequiredAttribute", TypeNameStyle.Name) != null;
var isNullable = _settings.AllowNullableBodyParameters && (typeDescription.IsNullable && !isRequired);

var operation = context.OperationDescription.Operation;
if (contextualParameter.TypeName == "XmlDocument" || contextualParameter.Type.InheritsFromTypeName("XmlDocument", TypeNameStyle.Name))
if (contextualParameter.ParameterType.Name == "XmlDocument" || contextualParameter.ParameterType.Type.InheritsFromTypeName("XmlDocument", TypeNameStyle.Name))
{
operation.TryAddConsumes("application/xml");
operationParameter = new OpenApiParameter
Expand All @@ -377,7 +377,7 @@ private OpenApiParameter AddBodyParameter(OperationProcessorContext context, str
};
operation.Parameters.Add(operationParameter);
}
else if (contextualParameter.Type.IsAssignableToTypeName("System.IO.Stream", TypeNameStyle.FullName))
else if (contextualParameter.ParameterType.Type.IsAssignableToTypeName("System.IO.Stream", TypeNameStyle.FullName))
{
operation.TryAddConsumes("application/octet-stream");
operationParameter = new OpenApiParameter
Expand Down Expand Up @@ -406,7 +406,7 @@ private OpenApiParameter AddBodyParameter(OperationProcessorContext context, str
IsNullableRaw = isNullable,
Description = contextualParameter.GetDescription(_settings.SchemaSettings),
Schema = context.SchemaGenerator.GenerateWithReferenceAndNullability<JsonSchema>(
contextualParameter, isNullable, schemaResolver: context.SchemaResolver)
contextualParameter.ParameterType, isNullable, schemaResolver: context.SchemaResolver)
};
operation.Parameters.Add(operationParameter);
}
Expand All @@ -423,31 +423,31 @@ private OpenApiParameter AddPrimitiveParametersFromUri(
{
var parameterDocumentation = contextualParameter.GetDescription(_settings.SchemaSettings);
var operationParameter = context.DocumentGenerator.CreatePrimitiveParameter(
name, parameterDocumentation, contextualParameter);
name, parameterDocumentation, contextualParameter.ParameterType);

operationParameter.Kind = OpenApiParameterKind.Query;
operation.Parameters.Add(operationParameter);
return operationParameter;
}
else
{
foreach (var contextualProperty in contextualParameter.Type.GetContextualProperties())
foreach (var contextualProperty in contextualParameter.ParameterType.Type.GetContextualProperties())
{
if (contextualProperty.ContextAttributes.Select(a => a.GetType()).All(a =>
if (contextualProperty.GetAttributes(true).Select(a => a.GetType()).All(a =>
!a.IsAssignableToTypeName("SwaggerIgnoreAttribute", TypeNameStyle.Name) &&
!a.IsAssignableToTypeName("JsonIgnoreAttribute", TypeNameStyle.Name)))
{
var fromQueryAttribute = contextualProperty.ContextAttributes.SingleOrDefault(a => a.GetType().Name == "FromQueryAttribute");
var fromQueryAttribute = contextualProperty.GetAttributes(true).SingleOrDefault(a => a.GetType().Name == "FromQueryAttribute");
var propertyName = fromQueryAttribute.TryGetPropertyValue<string>("Name") ??
_settings.SchemaSettings.ReflectionService.GetPropertyName(contextualProperty, _settings.SchemaSettings);

dynamic fromRouteAttribute = contextualProperty.ContextAttributes.SingleOrDefault(a => a.GetType().FullName == "Microsoft.AspNetCore.Mvc.FromRouteAttribute");
dynamic fromRouteAttribute = contextualProperty.GetAttributes(true).SingleOrDefault(a => a.GetType().FullName == "Microsoft.AspNetCore.Mvc.FromRouteAttribute");
if (fromRouteAttribute != null && !string.IsNullOrEmpty(fromRouteAttribute?.Name))
{
propertyName = fromRouteAttribute?.Name;
}

dynamic fromHeaderAttribute = contextualProperty.ContextAttributes.SingleOrDefault(a => a.GetType().FullName == "Microsoft.AspNetCore.Mvc.FromHeaderAttribute");
dynamic fromHeaderAttribute = contextualProperty.GetAttributes(true).SingleOrDefault(a => a.GetType().FullName == "Microsoft.AspNetCore.Mvc.FromHeaderAttribute");
if (fromHeaderAttribute != null && !string.IsNullOrEmpty(fromHeaderAttribute?.Name))
{
propertyName = fromHeaderAttribute?.Name;
Expand Down Expand Up @@ -500,7 +500,7 @@ private OpenApiParameter AddPrimitiveParameter(
if (contextualParameter.ParameterInfo.HasDefaultValue)
{
var defaultValue = context.SchemaGenerator.ConvertDefaultValue(
contextualParameter, contextualParameter.ParameterInfo.DefaultValue);
contextualParameter.ParameterType, contextualParameter.ParameterInfo.DefaultValue);

if (_settings.SchemaSettings.SchemaType == SchemaType.Swagger2)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Generation/NSwag.Generation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0-preview005" />
<PackageReference Include="NJsonSchema.NewtonsoftJson" Version="11.0.0-preview006" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Generation/OpenApiDocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public OpenApiParameter CreateUntypedPathParameter(string parameterName, string
public OpenApiParameter CreatePrimitiveParameter(string name, ContextualParameterInfo contextualParameter)
{
var documentation = contextualParameter.GetDescription(_settings.SchemaSettings);
return CreatePrimitiveParameter(name, documentation, contextualParameter);
return CreatePrimitiveParameter(name, documentation, contextualParameter.ParameterType);
}

/// <summary>Creates a primitive parameter for the given parameter information reflection object.</summary>
Expand All @@ -102,7 +102,7 @@ public OpenApiParameter CreatePrimitiveParameter(string name, string description
: CreatePrimitiveOpenApiParameter(contextualParameter, typeDescription);

operationParameter.Name = name;
operationParameter.IsRequired = contextualParameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("RequiredAttribute", TypeNameStyle.Name) != null;
operationParameter.IsRequired = contextualParameter.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("RequiredAttribute", TypeNameStyle.Name) != null;
operationParameter.IsNullableRaw = typeDescription.IsNullable;

if (description != string.Empty)
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Generation/OpenApiSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override TSchemaType GenerateWithReferenceAndNullability<TSchemaType>(
JsonSchemaResolver schemaResolver, Action<TSchemaType, JsonSchema> transformation = null)
{
contextualType = GenericResultWrapperTypes.RemoveGenericWrapperTypes(
contextualType, t => t.TypeName, t => t.OriginalGenericArguments[0]);
contextualType, t => t.Name, t => t.OriginalGenericArguments[0]);

if (IsFileResponse(contextualType))
{
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Generation/Processors/DocumentTagsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static void ProcessTagsAttribute(OpenApiDocument document, Type controll
{
dynamic tagsAttribute = controllerType
.ToCachedType()
.InheritedAttributes
.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("SwaggerTagsAttribute", TypeNameStyle.Name);

if (tagsAttribute != null)
Expand Down Expand Up @@ -64,7 +64,7 @@ private static void ProcessTagAttributes(OpenApiDocument document, Type controll
{
var tagAttributes = controllerType
.ToCachedType()
.InheritedAttributes
.GetAttributes(true)
.GetAssignableToTypeName("SwaggerTagAttribute", TypeNameStyle.Name)
.Select(a => (dynamic)a)
.ToArray();
Expand Down
Loading

0 comments on commit 8738fd3

Please sign in to comment.