Skip to content

Commit 6829d54

Browse files
committed
refactor
1 parent 5481873 commit 6829d54

16 files changed

+183
-95
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using System;
2+
3+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4+
public class SwaggerExcludeAttribute : Attribute
5+
{
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using System;
2+
3+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
4+
public class SwaggerIgnoreInheritPropsAttribute : Attribute
5+
{
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
/// <summary>
4+
/// give json examples for http requests in swagger
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Method)]
7+
public class SwaggerRequestExampleAttribute : Attribute
8+
{
9+
public string Name { get; set; }
10+
public string Json { get; set; }
11+
public SwaggerRequestExampleAttribute(string name, string json)
12+
{
13+
Name = name;
14+
Json = json;
15+
}
16+
public SwaggerRequestExampleAttribute(string name, Type swaggerRequestExampleGetterType)
17+
{
18+
var instance = Activator.CreateInstance(swaggerRequestExampleGetterType) as ISwaggerExample;
19+
Name = name;
20+
Json = instance!.GetExample();
21+
}
22+
}
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
/// <summary>
4+
/// give json examples for http requests in swagger
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Method)]
7+
public class SwaggerResponseExampleAttribute : Attribute
8+
{
9+
public string Name { get; set; }
10+
public string Json { get; set; }
11+
public SwaggerResponseExampleAttribute(string name, string json)
12+
{
13+
Name = name;
14+
Json = json;
15+
}
16+
public SwaggerResponseExampleAttribute(string name, Type swaggerRequestExampleGetterType)
17+
{
18+
var instance = Activator.CreateInstance(swaggerRequestExampleGetterType) as ISwaggerExample;
19+
Name = name;
20+
Json = instance!.GetExample();
21+
}
22+
}
23+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
/// <summary>
4+
/// allow only unique items in list
5+
/// </summary>
6+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
7+
public class SwaggerUniqueItemsAttribute : Attribute
8+
{
9+
}

SwaggerGenerator.csproj renamed to CSharpExtensions.OpenSource.Swagger.csproj

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1;;netcoreapp3.1;net5;net6</TargetFrameworks>
55
<Nullable>enable</Nullable>
66
<LangVersion>9.0</LangVersion>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8-
<RepositoryUrl>https://github.com/barnuri/swagger-generator-csharp</RepositoryUrl>
98
<RepositoryType>git</RepositoryType>
10-
<PackageProjectUrl>https://github.com/barnuri/swagger-generator-csharp</PackageProjectUrl>
119
<PackageTags>swagger, openapi, swagger generator, openapi generator</PackageTags>
1210
<Copyright>Bar Nuri</Copyright>
1311
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1412
<Authors>Bar Nuri</Authors>
1513
<PackageIcon>icon.png</PackageIcon>
1614
<PackageIconUrl />
1715
<Description>OpenAPI/Swagger API toolchain base on NSwag</Description>
18-
<PackageId>SwaggerGenerator.Core</PackageId>
1916
</PropertyGroup>
2017

21-
<ItemGroup>
22-
<Compile Remove="deploy\**" />
23-
<EmbeddedResource Remove="deploy\**" />
24-
<None Remove="deploy\**" />
25-
</ItemGroup>
26-
27-
<ItemGroup>
28-
<None Remove="apiKey.txt" />
29-
</ItemGroup>
30-
3118
<ItemGroup>
3219
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
3320
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
3421
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
35-
<PackageReference Include="MongoDB.Bson" Version="2.12.4" />
3622
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
3723
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
3824
<PackageReference Include="NSwag.AspNetCore" Version="13.11.3" />
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.31320.298
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32728.150
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SwaggerGenerator", "SwaggerGenerator.csproj", "{A6D1D9F0-7A2B-4440-9588-6475A654F4FD}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpExtensions.OpenSource.Swagger", "CSharpExtensions.OpenSource.Swagger.csproj", "{E6E9CA4F-0CAE-4C56-A585-3F917CDF8B08}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{A6D1D9F0-7A2B-4440-9588-6475A654F4FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{A6D1D9F0-7A2B-4440-9588-6475A654F4FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{A6D1D9F0-7A2B-4440-9588-6475A654F4FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{A6D1D9F0-7A2B-4440-9588-6475A654F4FD}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{E6E9CA4F-0CAE-4C56-A585-3F917CDF8B08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E6E9CA4F-0CAE-4C56-A585-3F917CDF8B08}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E6E9CA4F-0CAE-4C56-A585-3F917CDF8B08}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E6E9CA4F-0CAE-4C56-A585-3F917CDF8B08}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
2222
GlobalSection(ExtensibilityGlobals) = postSolution
23-
SolutionGuid = {798CC95B-001E-471A-8FEE-69A8D90F7D71}
23+
SolutionGuid = {2153D3F3-2679-45CB-BFC6-46E7BA4435D1}
2424
EndGlobalSection
2525
EndGlobal

SwaggerExtendDP.cs renamed to DocumentProcessor/SwaggerExtendDP.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using MongoDB.Bson.Serialization.Attributes;
21
using Newtonsoft.Json;
32
using NJsonSchema;
43
using NSwag.Generation.Processors;
@@ -32,22 +31,16 @@ public void Process(DocumentProcessorContext context)
3231
typeof(InversePropertyAttribute),
3332
typeof(ForeignKeyAttribute),
3433
typeof(JsonIgnoreAttribute),
35-
typeof(BsonIgnoreAttribute),
3634
};
37-
foreach (var att in excludeAttributes)
38-
{
39-
excludedProperties.AddRange(PowerfulGetProperties(type).Where(t => t.GetCustomAttribute(att) != null || t.PropertyType == typeof(MongoDB.Bson.ObjectId)).Select(x => x.Name.ToLower()));
40-
excludedProperties.AddRange(PowerfulGetFields(type).Where(t => t.GetCustomAttribute(att) != null || t.FieldType == typeof(MongoDB.Bson.ObjectId)).Select(x => x.Name.ToLower()));
41-
}
4235
excludedProperties = excludedProperties.Distinct().ToList();
4336

4437
var uniqueItemsProperties = new List<string>();
45-
uniqueItemsProperties.AddRange(PowerfulGetProperties(type).Where(t => t.GetCustomAttribute(typeof(SwaggerUniqueItems)) != null).Select(t => t.Name.ToLower()));
46-
uniqueItemsProperties.AddRange(PowerfulGetFields(type).Where(t => t.GetCustomAttribute(typeof(SwaggerUniqueItems)) != null).Select(t => t.Name.ToLower()));
38+
uniqueItemsProperties.AddRange(PowerfulGetProperties(type).Where(t => t.GetCustomAttribute(typeof(SwaggerUniqueItemsAttribute)) != null).Select(t => t.Name.ToLower()));
39+
uniqueItemsProperties.AddRange(PowerfulGetFields(type).Where(t => t.GetCustomAttribute(typeof(SwaggerUniqueItemsAttribute)) != null).Select(t => t.Name.ToLower()));
4740

4841
var ignoreInheritProps = new List<string>();
49-
ignoreInheritProps.AddRange(PowerfulGetProperties(type).Where(t => t.GetCustomAttribute(typeof(SwaggerIgnoreInheritProps)) != null).Select(t => t.Name.ToLower()));
50-
ignoreInheritProps.AddRange(PowerfulGetFields(type).Where(t => t.GetCustomAttribute(typeof(SwaggerIgnoreInheritProps)) != null).Select(t => t.Name.ToLower()));
42+
ignoreInheritProps.AddRange(PowerfulGetProperties(type).Where(t => t.GetCustomAttribute(typeof(SwaggerIgnoreInheritPropsAttribute)) != null).Select(t => t.Name.ToLower()));
43+
ignoreInheritProps.AddRange(PowerfulGetFields(type).Where(t => t.GetCustomAttribute(typeof(SwaggerIgnoreInheritPropsAttribute)) != null).Select(t => t.Name.ToLower()));
5144

5245
var customDefaultValsProperties = new List<(string name, object? initVal, object? defaultVal)>();
5346
if (instance != null && !type.IsEnum)
@@ -84,18 +77,3 @@ public FieldInfo[] PowerfulGetFields(Type type)
8477
.Where(x => !x.Name.Contains("k__BackingField"))
8578
.ToArray();
8679
}
87-
88-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
89-
public class SwaggerExcludeAttribute : Attribute
90-
{
91-
}
92-
93-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
94-
public class SwaggerUniqueItems : Attribute
95-
{
96-
}
97-
98-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
99-
public class SwaggerIgnoreInheritProps : Attribute
100-
{
101-
}
File renamed without changes.

Interfaces/ISwaggerExample.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface ISwaggerExample
2+
{
3+
string GetExample();
4+
}

0 commit comments

Comments
 (0)