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
6 changes: 3 additions & 3 deletions Samples/OpenApi.Sample/OpenApi.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3"/>
<PackageReference Include="Scalar.AspNetCore" Version="2.7.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1"/>
<PackageReference Include="Scalar.AspNetCore" Version="2.11.6" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace></RootNamespace>
<NoWarn>CS0649;CS0169;CA1050;CA1822;CA2211;IDE1006</NoWarn>
<NukeRootDirectory>..</NukeRootDirectory>
Expand All @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nuke.Common" Version="9.0.4" />
<PackageReference Include="Nuke.Common" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="*" />
<PackageReference Include="IDisposableAnalyzers" Version="[4.0.8]" PrivateAssets="all"/>
<PackageReference Include="SecurityCodeScan.VS2019" Version="[5.6.7]" PrivateAssets="all"/>
<PackageReference Include="SonarAnalyzer.CSharp" Version="[10.15.0.120848]" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="[9.0.0]" PrivateAssets="all"/>
<PackageReference Include="SonarAnalyzer.CSharp" Version="[10.16.1.129956]" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="[10.0.101]" PrivateAssets="all"/>
</ItemGroup>

<ItemGroup>
Expand Down
51 changes: 20 additions & 31 deletions src/OpenApi.Extensions/Extensions/OpenApiOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;
using OpenApi.Extensions.Options;
using OpenApi.Extensions.Schemas;
using OpenApi.Extensions.Transformers;
Expand Down Expand Up @@ -138,6 +137,7 @@ public static OpenApiOptions AddSecurityScheme(this OpenApiOptions options,
var securityScheme = new OpenApiSecurityScheme();
await asyncFactory(securityScheme, context.ApplicationServices, cancellationToken);
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
document.Components.SecuritySchemes.Add(schemeName, securityScheme);
});

Expand All @@ -146,11 +146,7 @@ public static OpenApiOptions AddSecurityScheme(this OpenApiOptions options,
var hasAuthorization = await context.HasAuthorizationAsync();
if (hasAuthorization)
{
var referenceScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{ Id = schemeName, Type = ReferenceType.SecurityScheme }
};
var referenceScheme = new OpenApiSecuritySchemeReference(schemeName, new OpenApiDocument());
operation.Security ??= [];
operation.Security.Add(new OpenApiSecurityRequirement
{
Expand All @@ -171,6 +167,7 @@ public static OpenApiOptions AddServer(this OpenApiOptions options, string serve
{
options.AddDocumentTransformer((document, _, _) =>
{
document.Servers ??= new List<OpenApiServer>();
document.Servers.Add(new OpenApiServer
{
Url = server,
Expand Down Expand Up @@ -225,8 +222,7 @@ public static OpenApiOptions AddType<TConcrete>(this OpenApiOptions options, Jso
}

schema.Type = GetTypeName<TConcrete>();
schema.Example = new OpenApiString(FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions));
schema.Annotations.Clear();
schema.Example = FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions);

return Task.CompletedTask;
});
Expand Down Expand Up @@ -254,8 +250,7 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio
}

schema.Type = GetTypeName<TType>();
schema.Example = new OpenApiString(FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions));
schema.Annotations.Clear();
schema.Example = FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions);

return Task.CompletedTask;
});
Expand Down Expand Up @@ -284,8 +279,7 @@ public static OpenApiOptions AddType<TConcrete>(this OpenApiOptions options, str

schema.Type = GetTypeName<TConcrete>();
schema.Format = format;
schema.Example = new OpenApiString(FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions));
schema.Annotations.Clear();
schema.Example = FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions);

return Task.CompletedTask;
});
Expand Down Expand Up @@ -315,8 +309,7 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio

schema.Type = GetTypeName<TType>();
schema.Format = format;
schema.Example = new OpenApiString(FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions));
schema.Annotations.Clear();
schema.Example = FormatJson(Activator.CreateInstance<TConcrete>(), jsonSerializerOptions);

return Task.CompletedTask;
});
Expand Down Expand Up @@ -349,8 +342,7 @@ public static OpenApiOptions AddType<TConcrete>(this OpenApiOptions options,

schema.Type = GetTypeName<TConcrete>();
schema.Format = format;
schema.Example = new OpenApiString(FormatJson(example, jsonSerializerOptions));
schema.Annotations.Clear();
schema.Example = FormatJson(example, jsonSerializerOptions);

return Task.CompletedTask;
});
Expand Down Expand Up @@ -384,7 +376,7 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio

schema.Type = GetTypeName<TType>();
schema.Format = format;
schema.Example = new OpenApiString(FormatJson(example, jsonSerializerOptions));
schema.Example = FormatJson(example, jsonSerializerOptions);

return Task.CompletedTask;
});
Expand All @@ -408,7 +400,7 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio
string format,
TConcrete example,
string? description = null,
IDictionary<string, OpenApiSchema>? properties = null,
IDictionary<string, IOpenApiSchema>? properties = null,
JsonSerializerOptions? jsonSerializerOptions = null)
{
jsonSerializerOptions ??= new JsonSerializerOptions();
Expand All @@ -422,10 +414,9 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio

schema.Type = GetTypeName<TType>();
schema.Format = format;
schema.Example = new OpenApiString(FormatJson(example, jsonSerializerOptions));
schema.Example = FormatJson(example, jsonSerializerOptions);
schema.Description = description;
schema.Properties = properties ?? new Dictionary<string, OpenApiSchema>();
schema.Annotations.Clear();
schema.Properties = properties ?? new Dictionary<string, IOpenApiSchema>();

return Task.CompletedTask;
});
Expand Down Expand Up @@ -456,8 +447,7 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio
}

schema.Type = GetTypeName<TType>();
schema.Example = new OpenApiString(FormatJson(example, jsonSerializerOptions));
schema.Annotations.Clear();
schema.Example = FormatJson(example, jsonSerializerOptions);

return Task.CompletedTask;
});
Expand All @@ -477,7 +467,7 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio
/// <returns>The updated <see cref="OpenApiOptions"/> instance.</returns>
public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions options,
TConcrete example,
IDictionary<string, OpenApiSchema>? properties = null,
IDictionary<string, IOpenApiSchema>? properties = null,
JsonSerializerOptions? jsonSerializerOptions = null)
{
jsonSerializerOptions ??= new JsonSerializerOptions();
Expand All @@ -490,9 +480,8 @@ public static OpenApiOptions AddType<TConcrete, TType>(this OpenApiOptions optio
}

schema.Type = GetTypeName<TType>();
schema.Example = new OpenApiString(FormatJson(example, jsonSerializerOptions));
schema.Properties = properties ?? new Dictionary<string, OpenApiSchema>();
schema.Annotations.Clear();
schema.Example = FormatJson(example, jsonSerializerOptions);
schema.Properties = properties ?? new Dictionary<string, IOpenApiSchema>();

return Task.CompletedTask;
});
Expand Down Expand Up @@ -522,14 +511,14 @@ private static string FormatJson<T>(T obj, JsonSerializerOptions? jsonSerializer
return formatToJson;
}

private static string GetTypeName<T>()
private static JsonSchemaType GetTypeName<T>()
{
if (typeof(T) == typeof(string))
{
return "string";
return JsonSchemaType.String;
}

return typeof(T).Name;
return JsonSchemaType.Object;
}

private static bool ShouldTransform<T>(OpenApiSchemaTransformerContext context)
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi.Extensions/OpenApi.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
<NoWarn>$(NoWarn)</NoWarn>
Expand All @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="[9.0.0,)"/>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="[10.0.0,)"/>
</ItemGroup>

</Project>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;

namespace OpenApi.Extensions.Schemas;

Expand All @@ -14,12 +15,13 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext
{
if (context.JsonTypeInfo.Type is { IsEnum: true })
{
schema.Enum ??= new List<JsonNode>();
schema.Enum.Clear();
Enum.GetNames(context.JsonTypeInfo.Type)
.ToList()
.ForEach(name => schema.Enum.Add(new OpenApiString($"{name}")));
.ForEach(name => schema.Enum.Add($"{name}"));

schema.Type = "string";
schema.Type = JsonSchemaType.String;
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;

namespace OpenApi.Extensions.Transformers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;

namespace OpenApi.Extensions.Transformers.Documents;

Expand Down Expand Up @@ -31,6 +32,7 @@ public OAuth2ImplicitFlowDocumentTransformation(string securityScheme,
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
document.Components.SecuritySchemes[_securityScheme] =
new OpenApiSecurityScheme
{
Expand All @@ -49,22 +51,24 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
}
};

document.SecurityRequirements.Add(
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType
.SecurityScheme,
Id = _securityScheme,
}
},
_scopes
}
});
// document.SecurityRequirements.Add(
// new OpenApiSecurityRequirement
#pragma warning disable S125
// {
#pragma warning restore S125
// {
// new OpenApiSecurityScheme
// {
// Reference = new OpenApiReference
// {
// Type = ReferenceType
// .SecurityScheme,
// Id = _securityScheme,
// }
// },
// _scopes
// }
// });

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;

namespace OpenApi.Extensions.Transformers.Operations;

Expand All @@ -22,6 +22,7 @@ public StatusCodeOperationTransform(HttpStatusCode statusCode, string? descripti
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
{
var statusCode = Convert.ToInt32(_statusCode);
operation.Responses ??= new OpenApiResponses();
if (operation.Responses.ContainsKey(statusCode.ToString()))
{
return Task.CompletedTask;
Expand All @@ -46,6 +47,7 @@ public StatusCodeOperationTransform(HttpStatusCode statusCode, string? descripti
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
{
var statusCode = Convert.ToInt32(_statusCode);
operation.Responses ??= new OpenApiResponses();
if (operation.Responses.ContainsKey(statusCode.ToString()))
{
return Task.CompletedTask;
Expand All @@ -56,7 +58,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
{
Description = _description,
Content = new Dictionary<string, OpenApiMediaType>
{ { "application/Json", new OpenApiMediaType { Schema = new OpenApiSchema { Type = typeof(T).Name } } } }
{ { "application/Json", new OpenApiMediaType { Schema = new OpenApiSchema { Description = typeof(T).Name } } } }
});
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;

namespace OpenApi.Extensions.Transformers;

Expand Down
Loading