Skip to content

Commit 84066cc

Browse files
authored
Merge pull request #2 from tennisi/lucasteles-swagger_schema
Lucasteles swagger schema
2 parents 3abb1cd + 5654495 commit 84066cc

File tree

1,561 files changed

+170786
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,561 files changed

+170786
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ To use the the [StronglyTypedId NuGet package](https://www.nuget.org/packages/St
5454
* [System.Text.Json](https://www.nuget.org/packages/System.Text.Json/) (optional, only required if [generating a System.Text `JsonConverter`](https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-2/#creating-a-custom-jsonconverter)). Note that in .NET Core apps, you will likely already reference this project via transitive dependencies.
5555
* [Dapper](https://www.nuget.org/packages/Dapper/) (optional, only required if [generating a type mapper](https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-3/#interfacing-with-external-system-using-strongly-typed-ids))
5656
* [EF Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) (optional, only required if [generating an EF Core ValueConverter](https://andrewlock.net/strongly-typed-ids-in-ef-core-using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-4/))
57+
* [Swagger Annotations](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Annotations) (optional, only required if [generating an Swagger Schema Filter](#openapiswagger-specification)
58+
5759

5860
To install the packages, add the references to your _csproj_ file, for example by running
5961

@@ -160,6 +162,18 @@ public partial struct OrderId { }
160162
public partial struct UserId { }
161163
```
162164

165+
## OpenApi/Swagger Specification
166+
167+
If you wish to use an ID in your Swagger models and want to have schema and model sample reflecting the ID backingfield type you will need:
168+
- Install [Swagger Annotations](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Annotations) `>=5.0.0`
169+
- Enable annotation in swagger gen with `services.AddSwaggerGen(c => c.EnableAnnotations());`
170+
- Use the converter flag `StronglyTypedIdConverter.SwaggerSchemaFilter` on the ID decorator. eg:
171+
```csharp
172+
[StronglyTypedId(
173+
backingType: StronglyTypedIdBackingType.Int,
174+
converters: StronglyTypedIdConverter.SwaggerSchemaFilter | StronglyTypedIdConverter.SystemTextJson)]
175+
public partial struct UserId { }
176+
```
163177

164178
## Embedding the attributes in your project
165179

@@ -253,4 +267,4 @@ The `struct`s you decorate with the `StronglyTypedId` attribute must be marked `
253267

254268
`StronglyTypedId` wouldn't work if not for [AArnott's CodeGeneration.Roslyn](https://github.com/AArnott/CodeGeneration.Roslyn) library.
255269
256-
The build process and general design of the library was modelled on the [RecordGenerator](https://github.com/amis92/RecordGenerator/blob/master/README.md) project, which is similar to this project, but can be used to generate immutable Record types.
270+
The build process and general design of the library was modelled on the [RecordGenerator](https://github.com/amis92/RecordGenerator/blob/master/README.md) project, which is similar to this project, but can be used to generate immutable Record types.

src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ public enum StronglyTypedIdConverter
4646
/// Creates a Dapper TypeHandler for converting to and from the type
4747
/// </summary>
4848
DapperTypeHandler = 32,
49+
50+
/// <summary>
51+
/// Creates a Swagger SchemaFilter for OpenApi documentation
52+
/// </summary>
53+
SwaggerSchemaFilter = 64,
54+
4955
}
5056
}

src/StronglyTypedIds/EmbeddedSources.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal static class EmbeddedSources
2525
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_EfCoreValueConverter.cs"),
2626
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"),
2727
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"),
28+
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"),
2829
false
2930
);
3031

@@ -37,6 +38,7 @@ internal static class EmbeddedSources
3738
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_EfCoreValueConverter.cs"),
3839
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"),
3940
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"),
41+
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"),
4042
false
4143
);
4244

@@ -49,6 +51,7 @@ internal static class EmbeddedSources
4951
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_EfCoreValueConverter.cs"),
5052
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"),
5153
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"),
54+
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"),
5255
false
5356
);
5457

@@ -61,6 +64,7 @@ internal static class EmbeddedSources
6164
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_EfCoreValueConverter.cs"),
6265
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"),
6366
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"),
67+
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"),
6468
false
6569
);
6670

@@ -73,6 +77,7 @@ internal static class EmbeddedSources
7377
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_EfCoreValueConverter.cs"),
7478
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"),
7579
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"),
80+
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"),
7681
true
7782
);
7883

@@ -85,12 +90,14 @@ internal static class EmbeddedSources
8590
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"),
8691
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
8792
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
93+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.cs"),
8894
false
8995
);
9096

9197
internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]";
9298
internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]";
9399
internal const string SystemTextJsonAttributeSource = " [System.Text.Json.Serialization.JsonConverter(typeof(TESTIDSystemTextJsonConverter))]";
100+
internal const string SwaggerSchemaFilterAttributeSource = " [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(TESTIDSchemaFilter))]";
94101

95102
internal static string LoadEmbeddedResource(string resourceName)
96103
{
@@ -108,6 +115,7 @@ internal static string LoadEmbeddedResource(string resourceName)
108115

109116
public readonly struct ResourceCollection
110117
{
118+
public string SwaggerSchemaFilter { get; }
111119
public string Header { get; }
112120
public bool NullableEnable { get; }
113121
public string BaseId { get; }
@@ -127,8 +135,10 @@ public ResourceCollection(
127135
string efCoreValueConverter,
128136
string dapperTypeHandler,
129137
string comparable,
138+
string swaggerSchemaFilter,
130139
bool nullableEnable)
131140
{
141+
SwaggerSchemaFilter = swaggerSchemaFilter;
132142
BaseId = baseId;
133143
Newtonsoft = newtonsoft;
134144
SystemTextJson = systemTextJson;

src/StronglyTypedIds/SourceGenerationHelper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static string CreateId(
6464

6565
var hasNamespace = !string.IsNullOrEmpty(idNamespace);
6666

67+
var useSchemaFilter = converters.IsSet(StronglyTypedIdConverter.SwaggerSchemaFilter);
6768
var useTypeConverter = converters.IsSet(StronglyTypedIdConverter.TypeConverter);
6869
var useNewtonsoftJson = converters.IsSet(StronglyTypedIdConverter.NewtonsoftJson);
6970
var useSystemTextJson = converters.IsSet(StronglyTypedIdConverter.SystemTextJson);
@@ -122,6 +123,12 @@ static string CreateId(
122123
sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource);
123124
}
124125

126+
if (useSchemaFilter)
127+
{
128+
sb.AppendLine(EmbeddedSources.SwaggerSchemaFilterAttributeSource);
129+
}
130+
131+
125132
sb.Append(resources.BaseId);
126133
ReplaceInterfaces(sb, useIEquatable, useIComparable);
127134

@@ -157,6 +164,11 @@ static string CreateId(
157164
sb.AppendLine(resources.SystemTextJson);
158165
}
159166

167+
if (useSchemaFilter)
168+
{
169+
sb.AppendLine(resources.SwaggerSchemaFilter);
170+
}
171+
160172
sb.Replace("TESTID", idName);
161173
sb.AppendLine(@" }");
162174

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
3+
{
4+
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
5+
{
6+
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
7+
schema.Type = idSchema.Type;
8+
schema.Format = idSchema.Format;
9+
schema.Example = idSchema.Example;
10+
schema.Default = idSchema.Default;
11+
schema.Properties = idSchema.Properties;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
3+
{
4+
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
5+
{
6+
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
7+
schema.Type = idSchema.Type;
8+
schema.Format = idSchema.Format;
9+
schema.Example = idSchema.Example;
10+
schema.Default = idSchema.Default;
11+
schema.Properties = idSchema.Properties;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
3+
{
4+
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
5+
{
6+
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
7+
schema.Type = idSchema.Type;
8+
schema.Format = idSchema.Format;
9+
schema.Example = idSchema.Example;
10+
schema.Default = idSchema.Default;
11+
schema.Properties = idSchema.Properties;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
3+
{
4+
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
5+
{
6+
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
7+
schema.Type = idSchema.Type;
8+
schema.Format = idSchema.Format;
9+
schema.Example = idSchema.Example;
10+
schema.Default = idSchema.Default;
11+
schema.Properties = idSchema.Properties;
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+

2+
class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
3+
{
4+
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
5+
{
6+
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
7+
schema.Type = idSchema.Type;
8+
schema.Format = idSchema.Format;
9+
schema.Example = idSchema.Example;
10+
schema.Default = idSchema.Default;
11+
schema.Properties = idSchema.Properties;
12+
schema.Nullable = true;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+

2+
class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
3+
{
4+
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
5+
{
6+
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
7+
schema.Type = idSchema.Type;
8+
schema.Format = idSchema.Format;
9+
schema.Example = idSchema.Example;
10+
schema.Default = idSchema.Default;
11+
schema.Properties = idSchema.Properties;
12+
schema.Nullable = false;
13+
}
14+
}

0 commit comments

Comments
 (0)