diff --git a/.github/workflows/BuildAndPack.yml b/.github/workflows/BuildAndPack.yml
index cdd141ece..d01eb547b 100644
--- a/.github/workflows/BuildAndPack.yml
+++ b/.github/workflows/BuildAndPack.yml
@@ -58,63 +58,63 @@ jobs:
with:
name: artifacts
path: artifacts
- windows-latest:
- name: windows-latest
- runs-on: windows-latest
- steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-dotnet@v3
- with:
- dotnet-version: |
- 7.0.x
- 6.0.x
- 5.0.x
- 3.1.x
- - name: Cache .nuke/temp, ~/.nuget/packages
- uses: actions/cache@v2
- with:
- path: |
- .nuke/temp
- ~/.nuget/packages
- !~/.nuget/packages/stronglytypeid
- key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
- - name: Run './build.cmd Clean Test TestPackages PushToNuGet'
- run: ./build.cmd Clean Test TestPackages PushToNuGet
- env:
- GithubToken: ${{ secrets.GITHUB_TOKEN }}
- NuGetToken: ${{ secrets.NUGET_TOKEN }}
- MSBuildEnableWorkloadResolver: false
- - uses: actions/upload-artifact@v1
- with:
- name: artifacts
- path: artifacts
- macOS-latest:
- name: macOS-latest
- runs-on: macOS-latest
- steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-dotnet@v3
- with:
- dotnet-version: |
- 7.0.x
- 6.0.x
- 5.0.x
- 3.1.x
- - name: Cache .nuke/temp, ~/.nuget/packages
- uses: actions/cache@v2
- with:
- path: |
- .nuke/temp
- ~/.nuget/packages
- !~/.nuget/packages/stronglytypeid
- key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
- - name: Run './build.cmd Clean Test TestPackages PushToNuGet'
- run: ./build.cmd Clean Test TestPackages PushToNuGet
- env:
- GithubToken: ${{ secrets.GITHUB_TOKEN }}
- NuGetToken: ${{ secrets.NUGET_TOKEN }}
- MSBuildEnableWorkloadResolver: false
- - uses: actions/upload-artifact@v1
- with:
- name: artifacts
- path: artifacts
+# windows-latest:
+# name: windows-latest
+# runs-on: windows-latest
+# steps:
+# - uses: actions/checkout@v3
+# - uses: actions/setup-dotnet@v3
+# with:
+# dotnet-version: |
+# 7.0.x
+# 6.0.x
+# 5.0.x
+# 3.1.x
+# - name: Cache .nuke/temp, ~/.nuget/packages
+# uses: actions/cache@v2
+# with:
+# path: |
+# .nuke/temp
+# ~/.nuget/packages
+# !~/.nuget/packages/stronglytypeid
+# key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
+# - name: Run './build.cmd Clean Test TestPackages PushToNuGet'
+# run: ./build.cmd Clean Test TestPackages PushToNuGet
+# env:
+# GithubToken: ${{ secrets.GITHUB_TOKEN }}
+# NuGetToken: ${{ secrets.NUGET_TOKEN }}
+# MSBuildEnableWorkloadResolver: false
+# - uses: actions/upload-artifact@v1
+# with:
+# name: artifacts
+# path: artifacts
+# macOS-latest:
+# name: macOS-latest
+# runs-on: macOS-latest
+# steps:
+# - uses: actions/checkout@v3
+# - uses: actions/setup-dotnet@v3
+# with:
+# dotnet-version: |
+# 7.0.x
+# 6.0.x
+# 5.0.x
+# 3.1.x
+# - name: Cache .nuke/temp, ~/.nuget/packages
+# uses: actions/cache@v2
+# with:
+# path: |
+# .nuke/temp
+# ~/.nuget/packages
+# !~/.nuget/packages/stronglytypeid
+# key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }}
+# - name: Run './build.cmd Clean Test TestPackages PushToNuGet'
+# run: ./build.cmd Clean Test TestPackages PushToNuGet
+# env:
+# GithubToken: ${{ secrets.GITHUB_TOKEN }}
+# NuGetToken: ${{ secrets.NUGET_TOKEN }}
+# MSBuildEnableWorkloadResolver: false
+# - uses: actions/upload-artifact@v1
+# with:
+# name: artifacts
+# path: artifacts
diff --git a/Directory.Build.props b/Directory.Build.props
index 54c03fcd5..a879488ad 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -10,9 +10,9 @@
false
MIT
logo.png
- https://github.com/andrewlock/StronglyTypedId
+ https://github.com/tennisi/StronglyTypedId
stronglytypedid attribute generator generation codegen codegenerator codegeneration netescapades
- https://github.com/andrewlock/StronglyTypedId
+ https://github.com/tennisi/StronglyTypedId
git
false
@@ -42,4 +42,4 @@
-
\ No newline at end of file
+
diff --git a/README.md b/README.md
index 094fdf587..dff946975 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,8 @@ To use the the [StronglyTypedId NuGet package](https://www.nuget.org/packages/St
* [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.
* [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))
* [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/))
+* [Swagger Annotations](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Annotations) (optional, only required if [generating an Swagger Schema Filter](#openapiswagger-specification)
+
To install the packages, add the references to your _csproj_ file, for example by running
@@ -160,6 +162,18 @@ public partial struct OrderId { }
public partial struct UserId { }
```
+## OpenApi/Swagger Specification
+
+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:
+- Install [Swagger Annotations](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Annotations) `>=5.0.0`
+- Enable annotation in swagger gen with `services.AddSwaggerGen(c => c.EnableAnnotations());`
+- Use the converter flag `StronglyTypedIdConverter.SwaggerSchemaFilter` on the ID decorator. eg:
+ ```csharp
+ [StronglyTypedId(
+ backingType: StronglyTypedIdBackingType.Int,
+ converters: StronglyTypedIdConverter.SwaggerSchemaFilter | StronglyTypedIdConverter.SystemTextJson)]
+ public partial struct UserId { }
+ ```
## Embedding the attributes in your project
@@ -253,4 +267,4 @@ The `struct`s you decorate with the `StronglyTypedId` attribute must be marked `
`StronglyTypedId` wouldn't work if not for [AArnott's CodeGeneration.Roslyn](https://github.com/AArnott/CodeGeneration.Roslyn) library.
-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.
\ No newline at end of file
+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.
diff --git a/StronglyTypedId.sln b/StronglyTypedId.sln
index cd44309c6..39ac8adc9 100644
--- a/StronglyTypedId.sln
+++ b/StronglyTypedId.sln
@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio Version 16
-VisualStudioVersion = 16.0.28803.352
+# Visual Studio Version 17
+VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EE1258BD-3422-4F55-B9CF-B4D6C95DAD68}"
EndProject
@@ -16,21 +16,26 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
releasenotes.props = releasenotes.props
version.props = version.props
+ .github\workflows\BuildAndPack.yml = .github\workflows\BuildAndPack.yml
+ CHANGELOG.md = CHANGELOG.md
+ build.sh = build.sh
+ NuGet.integration-tests.config = NuGet.integration-tests.config
+ global.json = global.json
EndProjectSection
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{E13FB452-2D47-4719-8BAA-7B695D79AF3A}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -42,8 +47,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -80,6 +83,16 @@ Global
{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x64.Build.0 = Release|Any CPU
{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.ActiveCfg = Release|Any CPU
{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.Build.0 = Release|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.Build.0 = Debug|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.Build.0 = Debug|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.ActiveCfg = Release|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.Build.0 = Release|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.ActiveCfg = Release|Any CPU
+ {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.Build.0 = Release|Any CPU
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|x64.ActiveCfg = Debug|Any CPU
diff --git a/build/Build.cs b/build/Build.cs
index 39bb2c919..43c63a6c0 100644
--- a/build/Build.cs
+++ b/build/Build.cs
@@ -146,7 +146,7 @@ class Build : NukeBuild
Target PushToNuGet => _ => _
.DependsOn(Compile)
- .OnlyWhenStatic(() => IsTag && IsServerBuild && IsWin)
+ .OnlyWhenStatic(() => true || ( IsTag && IsServerBuild && IsWin ))
.Requires(() => NuGetToken)
.After(Pack)
.Executes(() =>
diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
index 599a51409..a74f243e5 100644
--- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
+++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
@@ -46,5 +46,16 @@ public enum StronglyTypedIdConverter
/// Creates a Dapper TypeHandler for converting to and from the type
///
DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
+ ///
+ /// Creates a AutoMapper bidirectional converters
+ ///
+ AutoMapper = 128,
+
}
-}
\ No newline at end of file
+}
diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs
index d0900fc4f..5d145ccb1 100644
--- a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs
+++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs
@@ -8,7 +8,7 @@ namespace StronglyTypedIds
[Flags]
public enum StronglyTypedIdImplementations
{
- // Used with HasFlag, so needs to be 1, 2, 4 etc
+ // Used with HasFlag, so needs to be 1, 2, 4, 8 etc
///
/// Don't implement any additional members for the strongly typed ID
@@ -33,5 +33,23 @@ public enum StronglyTypedIdImplementations
/// Implement the interface
///
IComparable = 4,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IParsable = 8,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IConvertible = 16,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IStronglyTypedId = 32
}
-}
\ No newline at end of file
+}
diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj b/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj
index 5fd0485d3..415ff92f1 100644
--- a/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj
+++ b/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj
@@ -2,8 +2,8 @@
netstandard2.0
- enable
StronglyTypedIds
+ enable
false
diff --git a/src/StronglyTypedIds/Constants.cs b/src/StronglyTypedIds/Constants.cs
index 5cb590a6b..62911130a 100644
--- a/src/StronglyTypedIds/Constants.cs
+++ b/src/StronglyTypedIds/Constants.cs
@@ -1,7 +1,9 @@
+using System;
+
namespace StronglyTypedIds
{
internal static class Constants
{
public const string Usage = nameof(Usage);
}
-}
\ No newline at end of file
+}
diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs
index e1f229779..3640444a8 100644
--- a/src/StronglyTypedIds/EmbeddedSources.cs
+++ b/src/StronglyTypedIds/EmbeddedSources.cs
@@ -25,6 +25,11 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Parsable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Convertible.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_StronglyTypedId.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_AutoMapperTypeConverter.cs"),
false
);
@@ -37,6 +42,11 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Parsable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Convertible.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_StronglyTypedId.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_AutoMapperTypeConverter.cs"),
false
);
@@ -49,6 +59,11 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Parsable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Convertible.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_StronglyTypedId.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_AutoMapperTypeConverter.cs"),
false
);
@@ -61,6 +76,11 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Parsable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Convertible.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_StronglyTypedId.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_AutoMapperTypeConverter.cs"),
false
);
@@ -73,6 +93,11 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Parsable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Convertible.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_StronglyTypedId.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_AutoMapperTypeConverter.cs"),
true
);
@@ -85,12 +110,18 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Parsable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Convertible.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_StronglyTypedId.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_AutoMapperTypeConverter.cs"),
false
);
internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]";
internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]";
internal const string SystemTextJsonAttributeSource = " [System.Text.Json.Serialization.JsonConverter(typeof(TESTIDSystemTextJsonConverter))]";
+ internal const string SwaggerSchemaFilterAttributeSource = " [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(TESTIDSchemaFilter))]";
internal static string LoadEmbeddedResource(string resourceName)
{
@@ -108,6 +139,7 @@ internal static string LoadEmbeddedResource(string resourceName)
public readonly struct ResourceCollection
{
+ public string SwaggerSchemaFilter { get; }
public string Header { get; }
public bool NullableEnable { get; }
public string BaseId { get; }
@@ -116,7 +148,12 @@ public readonly struct ResourceCollection
public string TypeConverter { get; }
public string EfCoreValueConverter { get; }
public string DapperTypeHandler { get; }
+ public string AutoMapperTypeHandler { get; }
public string Comparable { get; }
+ public string Parsable { get; }
+ public string Convertible { get; }
+
+ public string StronglyTypedId { get; }
public ResourceCollection(
string header,
@@ -127,16 +164,26 @@ public ResourceCollection(
string efCoreValueConverter,
string dapperTypeHandler,
string comparable,
+ string parsable,
+ string convertible,
+ string stronglyTypedId,
+ string swaggerSchemaFilter,
+ string autoMapperTypeHandler,
bool nullableEnable)
{
+ SwaggerSchemaFilter = swaggerSchemaFilter;
BaseId = baseId;
Newtonsoft = newtonsoft;
SystemTextJson = systemTextJson;
TypeConverter = typeConverter;
EfCoreValueConverter = efCoreValueConverter;
DapperTypeHandler = dapperTypeHandler;
+ AutoMapperTypeHandler = autoMapperTypeHandler;
Comparable = comparable;
+ Parsable = parsable;
+ Convertible = convertible;
NullableEnable = nullableEnable;
+ StronglyTypedId = stronglyTypedId;
Header = header;
}
}
@@ -153,4 +200,4 @@ internal static string LoadTemplateForEmitting(string resourceName)
#endif";
}
}
-}
\ No newline at end of file
+}
diff --git a/src/StronglyTypedIds/ParentClass.cs b/src/StronglyTypedIds/ParentClass.cs
index 012c41274..093c7c0fb 100644
--- a/src/StronglyTypedIds/ParentClass.cs
+++ b/src/StronglyTypedIds/ParentClass.cs
@@ -1,3 +1,5 @@
+using System;
+
namespace StronglyTypedIds;
internal class ParentClass
@@ -14,4 +16,4 @@ public ParentClass(string keyword, string name, string constraints, ParentClass?
public string Keyword { get; }
public string Name { get; }
public string Constraints { get; }
-}
\ No newline at end of file
+}
diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs
index e33515310..8702cb641 100644
--- a/src/StronglyTypedIds/SourceGenerationHelper.cs
+++ b/src/StronglyTypedIds/SourceGenerationHelper.cs
@@ -35,7 +35,7 @@ public static string CreateId(
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
};
- return CreateId(idNamespace, idName, parentClass, converters, implementations, resources, sb);
+ return CreateId(idNamespace, idName, parentClass, converters, implementations, resources,backingType, sb);
}
static string CreateId(
@@ -45,6 +45,7 @@ static string CreateId(
StronglyTypedIdConverter converters,
StronglyTypedIdImplementations implementations,
EmbeddedSources.ResourceCollection resources,
+ StronglyTypedIdBackingType backingType,
StringBuilder? sb)
{
if (string.IsNullOrEmpty(idName))
@@ -64,14 +65,19 @@ static string CreateId(
var hasNamespace = !string.IsNullOrEmpty(idNamespace);
+ var useSchemaFilter = converters.IsSet(StronglyTypedIdConverter.SwaggerSchemaFilter);
var useTypeConverter = converters.IsSet(StronglyTypedIdConverter.TypeConverter);
var useNewtonsoftJson = converters.IsSet(StronglyTypedIdConverter.NewtonsoftJson);
var useSystemTextJson = converters.IsSet(StronglyTypedIdConverter.SystemTextJson);
var useEfCoreValueConverter = converters.IsSet(StronglyTypedIdConverter.EfCoreValueConverter);
var useDapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.DapperTypeHandler);
+ var useAutoMapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.AutoMapper);
var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable);
var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable);
+ var useIParsable = implementations.IsSet(StronglyTypedIdImplementations.IParsable);
+ var useIConvertible = implementations.IsSet(StronglyTypedIdImplementations.IConvertible);
+ var useIStronglyTypedId = implementations.IsSet(StronglyTypedIdImplementations.IStronglyTypedId);
var parentsCount = 0;
@@ -122,8 +128,14 @@ static string CreateId(
sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource);
}
+ if (useSchemaFilter)
+ {
+ sb.AppendLine(EmbeddedSources.SwaggerSchemaFilterAttributeSource);
+ }
+
+
sb.Append(resources.BaseId);
- ReplaceInterfaces(sb, useIEquatable, useIComparable);
+ ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable, useIConvertible, useIStronglyTypedId, backingType);
// IEquatable is already implemented whether or not the interface is implemented
@@ -132,6 +144,21 @@ static string CreateId(
sb.AppendLine(resources.Comparable);
}
+ if (useIParsable)
+ {
+ sb.AppendLine(resources.Parsable);
+ }
+
+ if (useIConvertible)
+ {
+ sb.AppendLine(resources.Convertible);
+ }
+
+ if (useIStronglyTypedId)
+ {
+ sb.AppendLine(resources.StronglyTypedId);
+ }
+
if (useEfCoreValueConverter)
{
sb.AppendLine(resources.EfCoreValueConverter);
@@ -142,6 +169,11 @@ static string CreateId(
sb.AppendLine(resources.DapperTypeHandler);
}
+ if (useAutoMapperTypeHandler)
+ {
+ sb.AppendLine(resources.AutoMapperTypeHandler);
+ }
+
if (useTypeConverter)
{
sb.AppendLine(resources.TypeConverter);
@@ -157,6 +189,11 @@ static string CreateId(
sb.AppendLine(resources.SystemTextJson);
}
+ if (useSchemaFilter)
+ {
+ sb.AppendLine(resources.SwaggerSchemaFilter);
+ }
+
sb.Replace("TESTID", idName);
sb.AppendLine(@" }");
@@ -173,7 +210,23 @@ static string CreateId(
return sb.ToString();
}
- private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable)
+
+ private static string BackingType(StronglyTypedIdBackingType backingType)
+ {
+ var resources = backingType switch
+ {
+ StronglyTypedIdBackingType.Guid => "System.Guid",
+ StronglyTypedIdBackingType.Int => "int",
+ StronglyTypedIdBackingType.Long => "long",
+ StronglyTypedIdBackingType.String => "string",
+ StronglyTypedIdBackingType.NullableString => "string?",
+ StronglyTypedIdBackingType.MassTransitNewId => "MassTransit.NewId",
+ _ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
+ };
+ return resources;
+ }
+
+ private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable, bool useIConvertible, bool useIStronglyTypedId, StronglyTypedIdBackingType backingType)
{
var interfaces = new List();
@@ -187,6 +240,21 @@ private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool
interfaces.Add("System.IEquatable");
}
+ if (useIParsable)
+ {
+ interfaces.Add("System.IParsable");
+ }
+
+ if (useIConvertible)
+ {
+ interfaces.Add("System.IConvertible");
+ }
+
+ if (useIStronglyTypedId)
+ {
+ interfaces.Add($"IStronglyTypedId<{BackingType(backingType)}>");
+ }
+
if (interfaces.Count > 0)
{
sb.Replace("INTERFACES", string.Join(", ", interfaces));
diff --git a/src/StronglyTypedIds/StronglyTypedIds.csproj b/src/StronglyTypedIds/StronglyTypedIds.csproj
index 64f63c39f..b3cd8e94d 100644
--- a/src/StronglyTypedIds/StronglyTypedIds.csproj
+++ b/src/StronglyTypedIds/StronglyTypedIds.csproj
@@ -4,7 +4,7 @@
netstandard2.0
false
enable
- StronglyTypedId
+ Tennisi.StronglyTypedId
A source generator for creating strongly-typed IDs by decorating with a [StronglyTypedId] attribute
@@ -26,4 +26,4 @@
-
\ No newline at end of file
+
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/Guid/Guid_AutoMapperTypeConverter.cs
new file mode 100644
index 000000000..88662e3ea
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_AutoMapperTypeConverter.cs
@@ -0,0 +1,10 @@
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public System.Guid Convert(TESTID source, System.Guid destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs
index 200147a15..324fda87a 100644
--- a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs
@@ -16,7 +16,6 @@ public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
return obj is TESTID other && Equals(other);
}
-
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs
new file mode 100644
index 000000000..56e2b009d
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs
@@ -0,0 +1,89 @@
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs
index 7d94a235e..b81be3fa9 100644
--- a/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs
@@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Guid;
}
public override TESTID Parse(object value)
@@ -15,4 +16,12 @@ public override TESTID Parse(object value)
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
- }
\ No newline at end of file
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs
new file mode 100644
index 000000000..85f7095a7
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs
@@ -0,0 +1,15 @@
+
+#nullable enable
+ public static TESTID Parse(string s, System.IFormatProvider? provider)
+ {
+ return new TESTID(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new TESTID(res);
+ return ok;
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs
new file mode 100644
index 000000000..5f234d181
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs
@@ -0,0 +1,5 @@
+
+ public static IStronglyTypedId Create(System.Guid value)
+ {
+ return new TESTID(value);
+ }
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/Guid/Guid_SwaggerSchemaFilter.cs
new file mode 100644
index 000000000..de06e10c0
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_SwaggerSchemaFilter.cs
@@ -0,0 +1,13 @@
+
+ class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/Int/Int_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/Int/Int_AutoMapperTypeConverter.cs
new file mode 100644
index 000000000..1dd7017e0
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Int/Int_AutoMapperTypeConverter.cs
@@ -0,0 +1,10 @@
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public int Convert(TESTID source, int destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
diff --git a/src/StronglyTypedIds/Templates/Int/Int_Base.cs b/src/StronglyTypedIds/Templates/Int/Int_Base.cs
index 08ce1fca6..71535f568 100644
--- a/src/StronglyTypedIds/Templates/Int/Int_Base.cs
+++ b/src/StronglyTypedIds/Templates/Int/Int_Base.cs
@@ -19,5 +19,6 @@ public override bool Equals(object obj)
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
+
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
diff --git a/src/StronglyTypedIds/Templates/Int/Int_Convertible.cs b/src/StronglyTypedIds/Templates/Int/Int_Convertible.cs
new file mode 100644
index 000000000..699cd94e7
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Int/Int_Convertible.cs
@@ -0,0 +1,89 @@
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs
index 8e3dc42c9..31c7dec28 100644
--- a/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs
+++ b/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs
@@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int32;
}
public override TESTID Parse(object value)
@@ -16,4 +17,12 @@ public override TESTID Parse(object value)
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
- }
\ No newline at end of file
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
diff --git a/src/StronglyTypedIds/Templates/Int/Int_Parsable.cs b/src/StronglyTypedIds/Templates/Int/Int_Parsable.cs
new file mode 100644
index 000000000..f5893b0ca
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Int/Int_Parsable.cs
@@ -0,0 +1,15 @@
+
+#nullable enable
+ public static TESTID Parse(string s, System.IFormatProvider? provider)
+ {
+ return new TESTID(int.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
+ {
+ int res = 0;
+ var ok = int.TryParse(s, provider, out res);
+ result = new TESTID(res);
+ return ok;
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/Int/Int_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/Int/Int_StronglyTypedId.cs
new file mode 100644
index 000000000..ba09088b8
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Int/Int_StronglyTypedId.cs
@@ -0,0 +1,5 @@
+
+ public static IStronglyTypedId Create(int value)
+ {
+ return new TESTID(value);
+ }
diff --git a/src/StronglyTypedIds/Templates/Int/Int_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/Int/Int_SwaggerSchemaFilter.cs
new file mode 100644
index 000000000..34b0d8919
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Int/Int_SwaggerSchemaFilter.cs
@@ -0,0 +1,13 @@
+
+ class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/Long/Long_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/Long/Long_AutoMapperTypeConverter.cs
new file mode 100644
index 000000000..30ce72e23
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Long/Long_AutoMapperTypeConverter.cs
@@ -0,0 +1,10 @@
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public long Convert(TESTID source, long destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
diff --git a/src/StronglyTypedIds/Templates/Long/Long_Base.cs b/src/StronglyTypedIds/Templates/Long/Long_Base.cs
index 38d9fc5a6..772634aba 100644
--- a/src/StronglyTypedIds/Templates/Long/Long_Base.cs
+++ b/src/StronglyTypedIds/Templates/Long/Long_Base.cs
@@ -19,5 +19,6 @@ public override bool Equals(object obj)
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
+
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
diff --git a/src/StronglyTypedIds/Templates/Long/Long_Convertible.cs b/src/StronglyTypedIds/Templates/Long/Long_Convertible.cs
new file mode 100644
index 000000000..5b176b826
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Long/Long_Convertible.cs
@@ -0,0 +1,89 @@
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs
index 42dac85c9..12575aa72 100644
--- a/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs
+++ b/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs
@@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int64;
}
public override TESTID Parse(object value)
@@ -12,9 +13,18 @@ public override TESTID Parse(object value)
{
long longValue => new TESTID(longValue),
int intValue => new TESTID(intValue),
+ decimal decimalValue => new TESTID((long)decimalValue),
short shortValue => new TESTID(shortValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new TESTID(result),
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
- }
\ No newline at end of file
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
diff --git a/src/StronglyTypedIds/Templates/Long/Long_Parsable.cs b/src/StronglyTypedIds/Templates/Long/Long_Parsable.cs
new file mode 100644
index 000000000..3a63073f9
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Long/Long_Parsable.cs
@@ -0,0 +1,15 @@
+
+#nullable enable
+ public static TESTID Parse(string s, System.IFormatProvider? provider)
+ {
+ return new TESTID(long.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
+ {
+ long res = 0;
+ var ok = long.TryParse(s, provider, out res);
+ result = new TESTID(res);
+ return ok;
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/Long/Long_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/Long/Long_StronglyTypedId.cs
new file mode 100644
index 000000000..e306b02be
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Long/Long_StronglyTypedId.cs
@@ -0,0 +1,5 @@
+
+ public static IStronglyTypedId Create(long value)
+ {
+ return new TESTID(value);
+ }
diff --git a/src/StronglyTypedIds/Templates/Long/Long_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/Long/Long_SwaggerSchemaFilter.cs
new file mode 100644
index 000000000..0d3508292
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Long/Long_SwaggerSchemaFilter.cs
@@ -0,0 +1,13 @@
+
+ class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/NewId/NewId_AutoMapperTypeConverter.cs
new file mode 100644
index 000000000..c30881e0e
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_AutoMapperTypeConverter.cs
@@ -0,0 +1,10 @@
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(TESTID source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs
index 854cd477a..e7f3d668d 100644
--- a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs
@@ -20,5 +20,6 @@ public override bool Equals(object obj)
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
+
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Convertible.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Convertible.cs
new file mode 100644
index 000000000..56e2b009d
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_Convertible.cs
@@ -0,0 +1,89 @@
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs
index 8d4af7b02..a5a3dc3a6 100644
--- a/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs
@@ -15,4 +15,13 @@ public override TESTID Parse(object value)
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
- }
\ No newline at end of file
+ }
+
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Parsable.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Parsable.cs
new file mode 100644
index 000000000..2faf43e0d
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_Parsable.cs
@@ -0,0 +1,13 @@
+
+ public static TESTID Parse(string s, IFormatProvider? provider)
+ {
+ return new TESTID(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new TESTID(res);
+ return ok;
+ }
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/NewId/NewId_StronglyTypedId.cs
new file mode 100644
index 000000000..050a6d892
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_StronglyTypedId.cs
@@ -0,0 +1,5 @@
+
+ public static IStronglyTypedId Create(MassTransit.NewId value)
+ {
+ return new TESTID(value);
+ }
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/NewId/NewId_SwaggerSchemaFilter.cs
new file mode 100644
index 000000000..de06e10c0
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_SwaggerSchemaFilter.cs
@@ -0,0 +1,13 @@
+
+ class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_AutoMapperTypeConverter.cs
new file mode 100644
index 000000000..6b4ebc97e
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_AutoMapperTypeConverter.cs
@@ -0,0 +1,10 @@
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string? Convert(TESTID source, string? destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs
index 94a6eaffb..7c91eed8e 100644
--- a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs
@@ -27,5 +27,6 @@ public override bool Equals(object? obj)
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
public override string? ToString() => Value;
+
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Convertible.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Convertible.cs
new file mode 100644
index 000000000..56e2b009d
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Convertible.cs
@@ -0,0 +1,89 @@
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs
index 1524a13b1..93064febd 100644
--- a/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs
@@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.AnsiString;
}
public override TESTID Parse(object value)
@@ -16,4 +17,12 @@ public override TESTID Parse(object value)
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
- }
\ No newline at end of file
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Parsable.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Parsable.cs
new file mode 100644
index 000000000..1cd9b15e1
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Parsable.cs
@@ -0,0 +1,11 @@
+
+ public static TESTID Parse(string s, IFormatProvider? provider)
+ {
+ return new TESTID(s);
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result)
+ {
+ result = new TESTID(s);
+ return true;
+ }
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_StronglyTypedId.cs
new file mode 100644
index 000000000..a347d496f
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_StronglyTypedId.cs
@@ -0,0 +1,5 @@
+
+ public static IStronglyTypedId Create(string? value)
+ {
+ return new TESTID(value);
+ }
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_SwaggerSchemaFilter.cs
new file mode 100644
index 000000000..daea45b8d
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_SwaggerSchemaFilter.cs
@@ -0,0 +1,14 @@
+
+ class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ schema.Nullable = true;
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/String/String_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/String/String_AutoMapperTypeConverter.cs
new file mode 100644
index 000000000..f3ee1e17a
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/String/String_AutoMapperTypeConverter.cs
@@ -0,0 +1,10 @@
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(TESTID source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
diff --git a/src/StronglyTypedIds/Templates/String/String_Convertible.cs b/src/StronglyTypedIds/Templates/String/String_Convertible.cs
new file mode 100644
index 000000000..56e2b009d
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/String/String_Convertible.cs
@@ -0,0 +1,89 @@
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
diff --git a/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs
index 93d6ff7bf..3fe8cdcff 100644
--- a/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs
+++ b/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs
@@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.AnsiString;
}
public override TESTID Parse(object value)
@@ -14,4 +15,12 @@ public override TESTID Parse(object value)
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
- }
\ No newline at end of file
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
diff --git a/src/StronglyTypedIds/Templates/String/String_Parsable.cs b/src/StronglyTypedIds/Templates/String/String_Parsable.cs
new file mode 100644
index 000000000..769bd1bb2
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/String/String_Parsable.cs
@@ -0,0 +1,11 @@
+
+ public static TESTID Parse(string s, IFormatProvider? provider)
+ {
+ throw new NotImplementedException();
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result)
+ {
+ result = new TESTID(s);
+ return true;
+ }
diff --git a/src/StronglyTypedIds/Templates/String/String_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/String/String_StronglyTypedId.cs
new file mode 100644
index 000000000..763971d65
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/String/String_StronglyTypedId.cs
@@ -0,0 +1,5 @@
+
+ public static IStronglyTypedId Create(string value)
+ {
+ return new TESTID(value);
+ }
diff --git a/src/StronglyTypedIds/Templates/String/String_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/String/String_SwaggerSchemaFilter.cs
new file mode 100644
index 000000000..1073bbf27
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/String/String_SwaggerSchemaFilter.cs
@@ -0,0 +1,14 @@
+
+ class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ schema.Nullable = false;
+ }
+ }
\ No newline at end of file
diff --git a/test/IntegrationLibraries.props b/test/IntegrationLibraries.props
index 8ba7bf0b0..f4fd1dc04 100644
--- a/test/IntegrationLibraries.props
+++ b/test/IntegrationLibraries.props
@@ -3,12 +3,14 @@
+
+
diff --git a/test/StronglyTypedIds.IntegrationTests/AutoMapper.cs b/test/StronglyTypedIds.IntegrationTests/AutoMapper.cs
new file mode 100644
index 000000000..58071d2e6
--- /dev/null
+++ b/test/StronglyTypedIds.IntegrationTests/AutoMapper.cs
@@ -0,0 +1,13 @@
+namespace AutoMapper;
+
+//FAKE STUB
+public interface ITypeConverter
+{
+
+}
+
+//FAKE STUB
+public class ResolutionContext
+{
+
+}
diff --git a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
index 2905f70e6..2ce11869c 100644
--- a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
@@ -325,6 +325,28 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
#endif
+#if NET5_0_OR_GREATER
+ [Fact]
+ public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter()
+ {
+ var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator(
+ new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(),
+ new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver(
+ new System.Text.Json.JsonSerializerOptions()));
+ var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(
+ new Microsoft.Extensions.DependencyInjection.ServiceCollection());
+ var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider);
+ var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository();
+
+ var idType = typeof(SwaggerGuidId);
+ var schema = schemaGenerator.GenerateSchema(idType, schemaRepository);
+ schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository));
+
+ Assert.Equal("string", schema.Type);
+ Assert.Equal("uuid", schema.Format);
+ }
+#endif
+
public class TestDbContext : DbContext
{
public DbSet Entities { get; set; }
@@ -344,6 +366,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.ValueGeneratedNever();
});
}
+
}
public class TestEntity
diff --git a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
index e2c48be64..90000025c 100644
--- a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
@@ -318,6 +318,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
#endif
+#if NET5_0_OR_GREATER
+ [Fact]
+ public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter()
+ {
+ var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator(
+ new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(),
+ new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver(
+ new System.Text.Json.JsonSerializerOptions()));
+ var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(
+ new Microsoft.Extensions.DependencyInjection.ServiceCollection());
+ var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider);
+ var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository();
+
+ var idType = typeof(SwaggerIntId);
+ var schema = schemaGenerator.GenerateSchema(idType, schemaRepository);
+ schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository));
+
+ Assert.Equal("integer", schema.Type);
+ Assert.Equal("int32", schema.Format);
+ }
+#endif
public class TestDbContext : DbContext
{
public DbSet Entities { get; set; }
diff --git a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
index b2f70dbcb..1f7ad4064 100644
--- a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
@@ -319,6 +319,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
#endif
+#if NET5_0_OR_GREATER
+ [Fact]
+ public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter()
+ {
+ var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator(
+ new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(),
+ new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver(
+ new System.Text.Json.JsonSerializerOptions()));
+ var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(
+ new Microsoft.Extensions.DependencyInjection.ServiceCollection());
+ var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider);
+ var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository();
+
+ var idType = typeof(SwaggerLongId);
+ var schema = schemaGenerator.GenerateSchema(idType, schemaRepository);
+ schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository));
+
+ Assert.Equal("integer", schema.Type);
+ Assert.Equal("int64", schema.Format);
+ }
+#endif
public class TestDbContext : DbContext
{
public DbSet Entities { get; set; }
@@ -350,4 +371,4 @@ public class EntityWithNullableId
public NewtonsoftJsonLongId? Id { get; set; }
}
}
-}
\ No newline at end of file
+}
diff --git a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
index e6dce6d74..ef1e75f8d 100644
--- a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
@@ -327,6 +327,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
#endif
+#if NET5_0_OR_GREATER
+ [Fact]
+ public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter()
+ {
+ var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator(
+ new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(),
+ new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver(
+ new System.Text.Json.JsonSerializerOptions()));
+ var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(
+ new Microsoft.Extensions.DependencyInjection.ServiceCollection());
+ var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider);
+ var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository();
+
+ var idType = typeof(SwaggerNewIdId);
+ var schema = schemaGenerator.GenerateSchema(idType, schemaRepository);
+ schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository));
+
+ Assert.Equal("string", schema.Type);
+ Assert.Equal("uuid", schema.Format);
+ }
+#endif
public class TestDbContext : DbContext
{
public DbSet Entities { get; set; }
diff --git a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
index c6835af82..671cfceb6 100644
--- a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
@@ -395,6 +395,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
#endif
+#if NET5_0_OR_GREATER
+ [Fact]
+ public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter()
+ {
+ var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator(
+ new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(),
+ new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver(
+ new System.Text.Json.JsonSerializerOptions()));
+ var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(
+ new Microsoft.Extensions.DependencyInjection.ServiceCollection());
+ var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider);
+ var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository();
+
+ var idType = typeof(SwaggerNullableStringId);
+ var schema = schemaGenerator.GenerateSchema(idType, schemaRepository);
+ schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository));
+
+ Assert.Equal("string", schema.Type);
+ Assert.Equal("", schema.Format);
+ }
+#endif
public class TestDbContext : DbContext
{
public DbSet Entities { get; set; }
diff --git a/test/StronglyTypedIds.IntegrationTests/ParsableTests.cs b/test/StronglyTypedIds.IntegrationTests/ParsableTests.cs
new file mode 100644
index 000000000..403fccc2f
--- /dev/null
+++ b/test/StronglyTypedIds.IntegrationTests/ParsableTests.cs
@@ -0,0 +1,6 @@
+namespace StronglyTypedIds.IntegrationTests;
+
+public class ParsableTests
+{
+
+}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
index 0cc76b44d..f4057089b 100644
--- a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
@@ -329,6 +329,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
#endif
+#if NET5_0_OR_GREATER
+ [Fact]
+ public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter()
+ {
+ var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator(
+ new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(),
+ new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver(
+ new System.Text.Json.JsonSerializerOptions()));
+ var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(
+ new Microsoft.Extensions.DependencyInjection.ServiceCollection());
+ var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider);
+ var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository();
+
+ var idType = typeof(SwaggerStringId);
+ var schema = schemaGenerator.GenerateSchema(idType, schemaRepository);
+ schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository));
+
+ Assert.Equal("string", schema.Type);
+ Assert.Equal("", schema.Format);
+ }
+#endif
public class TestDbContext : DbContext
{
public DbSet Entities { get; set; }
diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedId.cs b/test/StronglyTypedIds.IntegrationTests/StronglyTypedId.cs
new file mode 100644
index 000000000..0fc83ff50
--- /dev/null
+++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedId.cs
@@ -0,0 +1,3 @@
+//FAKE STUB
+public interface IStronglyTypedId {
+}
diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
index 2800167d3..0efc8da6b 100644
--- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
@@ -2,15 +2,29 @@
netcoreapp3.1;net6.0;net7.0
- net48;$(TargetFrameworks)
+ $(TargetFrameworks)
false
+ disable
true
+
+ TRACE;FAKE_CODE
+
+
+
+ TRACE;FAKE_CODE
+
+
+
+
+
+
+
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
index 9ad651d72..bfefdc655 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
@@ -29,6 +29,11 @@ public partial struct EfCoreGuidId { }
[StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler)]
public partial struct DapperGuidId { }
+#if NET5_0_OR_GREATER
+ [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter)]
+ public partial struct SwaggerGuidId { }
+#endif
+
[StronglyTypedId(implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
public partial struct BothGuidId { }
@@ -37,4 +42,4 @@ public partial struct EquatableGuidId { }
[StronglyTypedId(implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableGuidId { }
-}
\ No newline at end of file
+}
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs
index 15316b98a..809335237 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs
@@ -26,6 +26,11 @@ public partial struct EfCoreIntId { }
[StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.Int)]
public partial struct DapperIntId { }
+#if NET5_0_OR_GREATER
+ [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.Int)]
+ public partial struct SwaggerIntId { }
+#endif
+
[StronglyTypedId(backingType: StronglyTypedIdBackingType.Int, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
public partial struct BothIntId { }
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
index 5a1081bb8..78cbbb998 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
@@ -1,4 +1,4 @@
-using StronglyTypedIds;
+using AutoMapper;
namespace StronglyTypedIds.IntegrationTests.Types
{
@@ -26,6 +26,11 @@ public partial struct EfCoreLongId { }
[StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.Long)]
public partial struct DapperLongId { }
+#if NET5_0_OR_GREATER
+ [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.Long)]
+ public partial struct SwaggerLongId { }
+#endif
+
[StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
public partial struct BothLongId { }
@@ -34,4 +39,11 @@ public partial struct EquatableLongId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableLongId { }
-}
\ No newline at end of file
+
+ [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IStronglyTypedId)]
+ public partial struct StronglyTypedIdLongId { }
+
+
+ [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, converters : StronglyTypedIdConverter.AutoMapper)]
+ public partial struct AutoMappedLongId { }
+}
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs
index 0be4ce4c9..6e6f5b75e 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs
@@ -29,6 +29,11 @@ public partial struct EfCoreNewIdId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, converters: StronglyTypedIdConverter.DapperTypeHandler)]
public partial struct DapperNewIdId { }
+#if NET5_0_OR_GREATER
+ [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, converters: StronglyTypedIdConverter.SwaggerSchemaFilter)]
+ public partial struct SwaggerNewIdId { }
+#endif
+
[StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
public partial struct BothNewIdId { }
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs
index d51e901a1..b98962246 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs
@@ -26,6 +26,11 @@ public partial struct EfCoreNullableStringId { }
[StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.NullableString)]
public partial struct DapperNullableStringId { }
+#if NET5_0_OR_GREATER
+ [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.NullableString)]
+ public partial struct SwaggerNullableStringId { }
+#endif
+
[StronglyTypedId(backingType: StronglyTypedIdBackingType.NullableString, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
public partial struct BothNullableStringId { }
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs
index 5e7cdfa57..bf8a81afa 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs
@@ -26,6 +26,11 @@ public partial struct EfCoreStringId { }
[StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.String)]
public partial struct DapperStringId { }
+#if NET5_0_OR_GREATER
+ [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.String)]
+ public partial struct SwaggerStringId { }
+#endif
+
[StronglyTypedId(backingType: StronglyTypedIdBackingType.String, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
public partial struct BothStringId { }
diff --git a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj
index 16fb810fc..17a628451 100644
--- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj
@@ -17,7 +17,7 @@
-
+
diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj
index 38b3c43f8..4747ca652 100644
--- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj
@@ -16,7 +16,7 @@
-
+
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt
new file mode 100644
index 000000000..58a2c4794
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt
@@ -0,0 +1,62 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdAttribute : Attribute
+ {
+ ///
+ /// Make the struct a strongly typed ID
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// Converters to create for serializing/deserializing the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// If not set, uses , which defaults to
+ /// and
+ public StronglyTypedIdAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The to use to store the strongly-typed ID value
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// JSON library used to serialize/deserialize strongly-typed ID value
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt
new file mode 100644
index 000000000..58a2c4794
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt
@@ -0,0 +1,62 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdAttribute : Attribute
+ {
+ ///
+ /// Make the struct a strongly typed ID
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// Converters to create for serializing/deserializing the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// If not set, uses , which defaults to
+ /// and
+ public StronglyTypedIdAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The to use to store the strongly-typed ID value
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// JSON library used to serialize/deserialize strongly-typed ID value
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt
new file mode 100644
index 000000000..58a2c4794
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt
@@ -0,0 +1,62 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdAttribute : Attribute
+ {
+ ///
+ /// Make the struct a strongly typed ID
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// Converters to create for serializing/deserializing the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// If not set, uses , which defaults to
+ /// and
+ public StronglyTypedIdAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The to use to store the strongly-typed ID value
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// JSON library used to serialize/deserialize strongly-typed ID value
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt
new file mode 100644
index 000000000..58a2c4794
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt
@@ -0,0 +1,62 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdAttribute : Attribute
+ {
+ ///
+ /// Make the struct a strongly typed ID
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// Converters to create for serializing/deserializing the strongly-typed ID value.
+ /// If not set, uses , which defaults to
+ /// and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// If not set, uses , which defaults to
+ /// and
+ public StronglyTypedIdAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The to use to store the strongly-typed ID value
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// JSON library used to serialize/deserialize strongly-typed ID value
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt
new file mode 100644
index 000000000..4ea1be319
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt
@@ -0,0 +1,36 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// The to use to store the value of a strongly-typed ID
+ ///
+ internal enum StronglyTypedIdBackingType
+ {
+ ///
+ /// Use the default backing type (either the globally configured default, or Guid)
+ ///
+ Default = 0,
+
+ Guid = 1,
+ Int = 2,
+ String = 3,
+ Long = 4,
+ NullableString = 5,
+ MassTransitNewId = 6,
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt
new file mode 100644
index 000000000..4ea1be319
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt
@@ -0,0 +1,36 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// The to use to store the value of a strongly-typed ID
+ ///
+ internal enum StronglyTypedIdBackingType
+ {
+ ///
+ /// Use the default backing type (either the globally configured default, or Guid)
+ ///
+ Default = 0,
+
+ Guid = 1,
+ Int = 2,
+ String = 3,
+ Long = 4,
+ NullableString = 5,
+ MassTransitNewId = 6,
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt
new file mode 100644
index 000000000..4ea1be319
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt
@@ -0,0 +1,36 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// The to use to store the value of a strongly-typed ID
+ ///
+ internal enum StronglyTypedIdBackingType
+ {
+ ///
+ /// Use the default backing type (either the globally configured default, or Guid)
+ ///
+ Default = 0,
+
+ Guid = 1,
+ Int = 2,
+ String = 3,
+ Long = 4,
+ NullableString = 5,
+ MassTransitNewId = 6,
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt
new file mode 100644
index 000000000..4ea1be319
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt
@@ -0,0 +1,36 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// The to use to store the value of a strongly-typed ID
+ ///
+ internal enum StronglyTypedIdBackingType
+ {
+ ///
+ /// Use the default backing type (either the globally configured default, or Guid)
+ ///
+ Default = 0,
+
+ Guid = 1,
+ Int = 2,
+ String = 3,
+ Long = 4,
+ NullableString = 5,
+ MassTransitNewId = 6,
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt
new file mode 100644
index 000000000..ac13b4a2e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt
@@ -0,0 +1,76 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Converters used to to serialize/deserialize strongly-typed ID values
+ ///
+ [Flags]
+ internal enum StronglyTypedIdConverter
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4 etc
+
+ ///
+ /// Don't create any converters for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default converters for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ ///
+ /// Creates a for converting from the strongly typed ID to and from a string
+ ///
+ TypeConverter = 2,
+
+ ///
+ /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ NewtonsoftJson = 4,
+
+ ///
+ /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ SystemTextJson = 8,
+
+ ///
+ /// Creates an EF Core Value Converter for extracting the primitive value
+ ///
+ EfCoreValueConverter = 16,
+
+ ///
+ /// Creates a Dapper TypeHandler for converting to and from the type
+ ///
+ DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
+ ///
+ /// Creates a AutoMapper bidirectional converters
+ ///
+ AutoMapper = 128,
+
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt
new file mode 100644
index 000000000..ac13b4a2e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt
@@ -0,0 +1,76 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Converters used to to serialize/deserialize strongly-typed ID values
+ ///
+ [Flags]
+ internal enum StronglyTypedIdConverter
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4 etc
+
+ ///
+ /// Don't create any converters for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default converters for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ ///
+ /// Creates a for converting from the strongly typed ID to and from a string
+ ///
+ TypeConverter = 2,
+
+ ///
+ /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ NewtonsoftJson = 4,
+
+ ///
+ /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ SystemTextJson = 8,
+
+ ///
+ /// Creates an EF Core Value Converter for extracting the primitive value
+ ///
+ EfCoreValueConverter = 16,
+
+ ///
+ /// Creates a Dapper TypeHandler for converting to and from the type
+ ///
+ DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
+ ///
+ /// Creates a AutoMapper bidirectional converters
+ ///
+ AutoMapper = 128,
+
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt
new file mode 100644
index 000000000..ac13b4a2e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt
@@ -0,0 +1,76 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Converters used to to serialize/deserialize strongly-typed ID values
+ ///
+ [Flags]
+ internal enum StronglyTypedIdConverter
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4 etc
+
+ ///
+ /// Don't create any converters for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default converters for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ ///
+ /// Creates a for converting from the strongly typed ID to and from a string
+ ///
+ TypeConverter = 2,
+
+ ///
+ /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ NewtonsoftJson = 4,
+
+ ///
+ /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ SystemTextJson = 8,
+
+ ///
+ /// Creates an EF Core Value Converter for extracting the primitive value
+ ///
+ EfCoreValueConverter = 16,
+
+ ///
+ /// Creates a Dapper TypeHandler for converting to and from the type
+ ///
+ DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
+ ///
+ /// Creates a AutoMapper bidirectional converters
+ ///
+ AutoMapper = 128,
+
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt
new file mode 100644
index 000000000..ac13b4a2e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt
@@ -0,0 +1,76 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Converters used to to serialize/deserialize strongly-typed ID values
+ ///
+ [Flags]
+ internal enum StronglyTypedIdConverter
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4 etc
+
+ ///
+ /// Don't create any converters for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default converters for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ ///
+ /// Creates a for converting from the strongly typed ID to and from a string
+ ///
+ TypeConverter = 2,
+
+ ///
+ /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ NewtonsoftJson = 4,
+
+ ///
+ /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value
+ ///
+ SystemTextJson = 8,
+
+ ///
+ /// Creates an EF Core Value Converter for extracting the primitive value
+ ///
+ EfCoreValueConverter = 16,
+
+ ///
+ /// Creates a Dapper TypeHandler for converting to and from the type
+ ///
+ DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
+ ///
+ /// Creates a AutoMapper bidirectional converters
+ ///
+ AutoMapper = 128,
+
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
index 32b324616..ac13b4a2e 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
@@ -59,6 +59,18 @@ namespace StronglyTypedIds
/// Creates a Dapper TypeHandler for converting to and from the type
///
DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
+ ///
+ /// Creates a AutoMapper bidirectional converters
+ ///
+ AutoMapper = 128,
+
}
}
+
#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt
new file mode 100644
index 000000000..3df9e2e31
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt
@@ -0,0 +1,60 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Used to control the default Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdDefaultsAttribute : Attribute
+ {
+ ///
+ /// Set the default values used for strongly typed ids
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// Defaults to
+ /// JSON library used to serialize/deserialize strongly-typed ID value.
+ /// Defaults to and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// Defaults to and
+ public StronglyTypedIdDefaultsAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The default to use to store the strongly-typed ID values.
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// The default converters to create for serializing/deserializing strongly-typed ID values.
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt
new file mode 100644
index 000000000..3df9e2e31
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt
@@ -0,0 +1,60 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Used to control the default Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdDefaultsAttribute : Attribute
+ {
+ ///
+ /// Set the default values used for strongly typed ids
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// Defaults to
+ /// JSON library used to serialize/deserialize strongly-typed ID value.
+ /// Defaults to and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// Defaults to and
+ public StronglyTypedIdDefaultsAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The default to use to store the strongly-typed ID values.
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// The default converters to create for serializing/deserializing strongly-typed ID values.
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt
new file mode 100644
index 000000000..3df9e2e31
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt
@@ -0,0 +1,60 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Used to control the default Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdDefaultsAttribute : Attribute
+ {
+ ///
+ /// Set the default values used for strongly typed ids
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// Defaults to
+ /// JSON library used to serialize/deserialize strongly-typed ID value.
+ /// Defaults to and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// Defaults to and
+ public StronglyTypedIdDefaultsAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The default to use to store the strongly-typed ID values.
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// The default converters to create for serializing/deserializing strongly-typed ID values.
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt
new file mode 100644
index 000000000..3df9e2e31
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt
@@ -0,0 +1,60 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Used to control the default Place on partial structs to make the type a strongly-typed ID
+ ///
+ [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
+ [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")]
+ internal sealed class StronglyTypedIdDefaultsAttribute : Attribute
+ {
+ ///
+ /// Set the default values used for strongly typed ids
+ ///
+ /// The to use to store the strongly-typed ID value.
+ /// Defaults to
+ /// JSON library used to serialize/deserialize strongly-typed ID value.
+ /// Defaults to and
+ /// Interfaces and patterns the strongly typed id should implement
+ /// Defaults to and
+ public StronglyTypedIdDefaultsAttribute(
+ StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default,
+ StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default,
+ StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default)
+ {
+ BackingType = backingType;
+ Converters = converters;
+ Implementations = implementations;
+ }
+
+ ///
+ /// The default to use to store the strongly-typed ID values.
+ ///
+ public StronglyTypedIdBackingType BackingType { get; }
+
+ ///
+ /// The default converters to create for serializing/deserializing strongly-typed ID values.
+ ///
+ public StronglyTypedIdConverter Converters { get; }
+
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ public StronglyTypedIdImplementations Implementations { get; }
+ }
+}
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt
new file mode 100644
index 000000000..485917d76
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt
@@ -0,0 +1,70 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ [Flags]
+ internal enum StronglyTypedIdImplementations
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4, 8 etc
+
+ ///
+ /// Don't implement any additional members for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default implementations for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IEquatable = 2,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IComparable = 4,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IParsable = 8,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IConvertible = 16,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IStronglyTypedId = 32
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt
new file mode 100644
index 000000000..485917d76
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt
@@ -0,0 +1,70 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ [Flags]
+ internal enum StronglyTypedIdImplementations
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4, 8 etc
+
+ ///
+ /// Don't implement any additional members for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default implementations for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IEquatable = 2,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IComparable = 4,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IParsable = 8,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IConvertible = 16,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IStronglyTypedId = 32
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt
new file mode 100644
index 000000000..485917d76
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt
@@ -0,0 +1,70 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ [Flags]
+ internal enum StronglyTypedIdImplementations
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4, 8 etc
+
+ ///
+ /// Don't implement any additional members for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default implementations for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IEquatable = 2,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IComparable = 4,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IParsable = 8,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IConvertible = 16,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IStronglyTypedId = 32
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt
new file mode 100644
index 000000000..485917d76
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt
@@ -0,0 +1,70 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES
+
+using System;
+
+namespace StronglyTypedIds
+{
+ ///
+ /// Interfaces and patterns the strongly typed id should implement
+ ///
+ [Flags]
+ internal enum StronglyTypedIdImplementations
+ {
+ // Used with HasFlag, so needs to be 1, 2, 4, 8 etc
+
+ ///
+ /// Don't implement any additional members for the strongly typed ID
+ ///
+ None = 0,
+
+ ///
+ /// Use the default implementations for the strongly typed Id.
+ /// This will be the value provided in the , which falls back to
+ /// and
+ ///
+ Default = 1,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IEquatable = 2,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IComparable = 4,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IParsable = 8,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IConvertible = 16,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IStronglyTypedId = 32
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt
index 76835ffec..485917d76 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt
@@ -21,7 +21,7 @@ namespace StronglyTypedIds
[Flags]
internal enum StronglyTypedIdImplementations
{
- // Used with HasFlag, so needs to be 1, 2, 4 etc
+ // Used with HasFlag, so needs to be 1, 2, 4, 8 etc
///
/// Don't implement any additional members for the strongly typed ID
@@ -46,6 +46,25 @@ namespace StronglyTypedIds
/// Implement the interface
///
IComparable = 4,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IParsable = 8,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IConvertible = 16,
+
+ // ReSharper disable once InconsistentNaming
+ ///
+ /// Implement the interface
+ ///
+ IStronglyTypedId = 32
}
}
+
#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt
new file mode 100644
index 000000000..1f599b78e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt
@@ -0,0 +1,289 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(System.Guid value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Guid;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(System.Guid.Parse(reader.GetString()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt
new file mode 100644
index 000000000..1f599b78e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt
@@ -0,0 +1,289 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(System.Guid value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Guid;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(System.Guid.Parse(reader.GetString()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt
new file mode 100644
index 000000000..1f599b78e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt
@@ -0,0 +1,289 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(System.Guid value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Guid;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(System.Guid.Parse(reader.GetString()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt
new file mode 100644
index 000000000..1f599b78e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt
@@ -0,0 +1,289 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(System.Guid value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Guid;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(System.Guid.Parse(reader.GetString()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt
index d7a3ed3e7..1f599b78e 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt
@@ -12,7 +12,8 @@
[Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
[System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
[System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
- readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
{
public System.Guid Value { get; }
@@ -30,7 +31,6 @@
if (ReferenceEquals(null, obj)) return false;
return obj is MyTestId other && Equals(other);
}
-
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
@@ -38,6 +38,118 @@
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(System.Guid value)
+ {
+ return new MyTestId(value);
+ }
+
+
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
{
public EfCoreValueConverter() : this(null) { }
@@ -54,6 +166,7 @@
public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Guid;
}
public override MyTestId Parse(object value)
@@ -67,6 +180,26 @@
}
}
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
@@ -140,4 +273,17 @@
writer.WriteStringValue(value.Value);
}
}
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt
new file mode 100644
index 000000000..f678704f5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt
@@ -0,0 +1,291 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(int.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ int res = 0;
+ var ok = int.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(int value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int32;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt32());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt
new file mode 100644
index 000000000..f678704f5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt
@@ -0,0 +1,291 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(int.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ int res = 0;
+ var ok = int.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(int value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int32;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt32());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt
new file mode 100644
index 000000000..f678704f5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt
@@ -0,0 +1,291 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(int.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ int res = 0;
+ var ok = int.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(int value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int32;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt32());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt
new file mode 100644
index 000000000..f678704f5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt
@@ -0,0 +1,291 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(int.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ int res = 0;
+ var ok = int.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(int value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int32;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt32());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt
index e7affbacb..f678704f5 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt
@@ -12,7 +12,8 @@
[Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
[System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
[System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
- readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
{
public int Value { get; }
@@ -33,10 +34,123 @@
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
+
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(int.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ int res = 0;
+ var ok = int.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(int value)
+ {
+ return new MyTestId(value);
+ }
+
+
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
{
public EfCoreValueConverter() : this(null) { }
@@ -53,6 +167,7 @@
public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int32;
}
public override MyTestId Parse(object value)
@@ -67,6 +182,26 @@
}
}
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
@@ -140,4 +275,17 @@
writer.WriteNumberValue(value.Value);
}
}
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt
new file mode 100644
index 000000000..1f1b957f7
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt
@@ -0,0 +1,295 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(long.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = long.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(long value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int64;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ decimal decimalValue => new MyTestId((long)decimalValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt64());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt
new file mode 100644
index 000000000..1f1b957f7
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt
@@ -0,0 +1,295 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(long.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = long.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(long value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int64;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ decimal decimalValue => new MyTestId((long)decimalValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt64());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt
new file mode 100644
index 000000000..1f1b957f7
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt
@@ -0,0 +1,295 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(long.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = long.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(long value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int64;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ decimal decimalValue => new MyTestId((long)decimalValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt64());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt
new file mode 100644
index 000000000..1f1b957f7
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt
@@ -0,0 +1,295 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(long.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = long.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(long value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int64;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ decimal decimalValue => new MyTestId((long)decimalValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetInt64());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteNumberValue(value.Value);
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt
index 0a464e952..1f1b957f7 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt
@@ -12,7 +12,8 @@
[Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
[System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
[System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
- readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
{
public long Value { get; }
@@ -33,10 +34,123 @@
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
+
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+#nullable enable
+ public static MyTestId Parse(string s, System.IFormatProvider? provider)
+ {
+ return new MyTestId(long.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = long.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+#nullable disable
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ return Value;
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(long value)
+ {
+ return new MyTestId(value);
+ }
+
+
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
{
public EfCoreValueConverter() : this(null) { }
@@ -53,6 +167,7 @@
public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
{
parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.Int64;
}
public override MyTestId Parse(object value)
@@ -61,6 +176,7 @@
{
long longValue => new MyTestId(longValue),
int intValue => new MyTestId(intValue),
+ decimal decimalValue => new MyTestId((long)decimalValue),
short shortValue => new MyTestId(shortValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
@@ -68,6 +184,26 @@
}
}
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
@@ -143,4 +279,17 @@
writer.WriteNumberValue(value.Value);
}
}
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt
new file mode 100644
index 000000000..e82a6bc07
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt
@@ -0,0 +1,297 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(MassTransit.NewId value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value.ToGuid(),
+ value => new MyTestId(MassTransit.NewId.FromGuid(value)),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value.ToGuid());
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt
new file mode 100644
index 000000000..e82a6bc07
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt
@@ -0,0 +1,297 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(MassTransit.NewId value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value.ToGuid(),
+ value => new MyTestId(MassTransit.NewId.FromGuid(value)),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value.ToGuid());
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt
new file mode 100644
index 000000000..e82a6bc07
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt
@@ -0,0 +1,297 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(MassTransit.NewId value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value.ToGuid(),
+ value => new MyTestId(MassTransit.NewId.FromGuid(value)),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value.ToGuid());
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt
new file mode 100644
index 000000000..e82a6bc07
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt
@@ -0,0 +1,297 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(MassTransit.NewId value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value.ToGuid(),
+ value => new MyTestId(MassTransit.NewId.FromGuid(value)),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ writer.WriteStringValue(value.Value.ToGuid());
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt
index c2c044246..e82a6bc07 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt
@@ -12,7 +12,8 @@
[Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
[System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
[System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
- readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
{
public MassTransit.NewId Value { get; }
@@ -34,10 +35,121 @@
public override int GetHashCode() => Value.GetHashCode();
public override string ToString() => Value.ToString();
+
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(Guid.Parse(s, provider));
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ long res = 0;
+ var ok = Guid.TryParse(s, provider, out res);
+ result = new MyTestId(res);
+ return ok;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(MassTransit.NewId value)
+ {
+ return new MyTestId(value);
+ }
+
+
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
{
public EfCoreValueConverter() : this(null) { }
@@ -67,6 +179,27 @@
}
}
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
@@ -148,4 +281,17 @@
writer.WriteStringValue(value.Value.ToGuid());
}
}
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ }
+ }
}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt
new file mode 100644
index 000000000..604eb7a60
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt
@@ -0,0 +1,320 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(s);
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ result = new MyTestId(s);
+ return true;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(string? value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null)
+ : base(
+ id => id.Value!,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.AnsiString;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetString());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ if (value.Value is null)
+ {
+ writer.WriteNullValue();
+ }
+ else
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ schema.Nullable = true;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt
new file mode 100644
index 000000000..604eb7a60
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt
@@ -0,0 +1,320 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(s);
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ result = new MyTestId(s);
+ return true;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(string? value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null)
+ : base(
+ id => id.Value!,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.AnsiString;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetString());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ if (value.Value is null)
+ {
+ writer.WriteNullValue();
+ }
+ else
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ schema.Nullable = true;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt
new file mode 100644
index 000000000..604eb7a60
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt
@@ -0,0 +1,320 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(s);
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ result = new MyTestId(s);
+ return true;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(string? value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null)
+ : base(
+ id => id.Value!,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.AnsiString;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new MyTestId(reader.GetString());
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options)
+ {
+ if (value.Value is null)
+ {
+ writer.WriteNullValue();
+ }
+ else
+ {
+ writer.WriteStringValue(value.Value);
+ }
+ }
+ }
+
+ class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
+ {
+ public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
+ {
+ var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""};
+ schema.Type = idSchema.Type;
+ schema.Format = idSchema.Format;
+ schema.Example = idSchema.Example;
+ schema.Default = idSchema.Default;
+ schema.Properties = idSchema.Properties;
+ schema.Nullable = true;
+ }
+ }
+ }
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt
new file mode 100644
index 000000000..604eb7a60
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt
@@ -0,0 +1,320 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public static MyTestId Parse(string s, IFormatProvider? provider)
+ {
+ return new MyTestId(s);
+ }
+
+ public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
+ {
+ result = new MyTestId(s);
+ return true;
+ }
+
+
+//ICONVERTIBLE
+#nullable enable
+
+ public System.TypeCode GetTypeCode()
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public bool ToBoolean(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public byte ToByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public char ToChar(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public System.DateTime ToDateTime(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public decimal ToDecimal(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public double ToDouble(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public short ToInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public int ToInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public long ToInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public sbyte ToSByte(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public float ToSingle(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public string ToString(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public object ToType(System.Type conversionType, System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ushort ToUInt16(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public uint ToUInt32(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public ulong ToUInt64(System.IFormatProvider? provider)
+ {
+ throw new System.NotImplementedException();
+ }
+#nullable disable
+
+
+ public static IStronglyTypedId Create(string? value)
+ {
+ return new MyTestId(value);
+ }
+
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null)
+ : base(
+ id => id.Value!,
+ value => new MyTestId(value),
+ mappingHints
+ ) { }
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ parameter.DbType = System.Data.DbType.AnsiString;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+#pragma warning disable CA2255
+ [System.Runtime.CompilerServices.ModuleInitializerAttribute]
+ public static void AddTypeHandler()
+ {
+ Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler());
+ }
+#pragma warning restore CA2255
+
+
+#if !FAKE_CODE
+ public class AutoMapperTypeConverter : AutoMapper.ITypeConverter
+ {
+ public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context)
+ {
+ return source.Value;
+ }
+ }
+#endif
+
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize