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/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
index 599a51409..4dad18ae1 100644
--- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
+++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
@@ -46,5 +46,11 @@ 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,
+
}
}
\ No newline at end of file
diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs
index e1f229779..1c6d7e0b5 100644
--- a/src/StronglyTypedIds/EmbeddedSources.cs
+++ b/src/StronglyTypedIds/EmbeddedSources.cs
@@ -25,6 +25,7 @@ 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_SwaggerSchemaFilter.cs"),
false
);
@@ -37,6 +38,7 @@ 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_SwaggerSchemaFilter.cs"),
false
);
@@ -49,6 +51,7 @@ 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_SwaggerSchemaFilter.cs"),
false
);
@@ -61,6 +64,7 @@ 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_SwaggerSchemaFilter.cs"),
false
);
@@ -73,6 +77,7 @@ 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_SwaggerSchemaFilter.cs"),
true
);
@@ -85,12 +90,14 @@ 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_SwaggerSchemaFilter.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 +115,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; }
@@ -127,8 +135,10 @@ public ResourceCollection(
string efCoreValueConverter,
string dapperTypeHandler,
string comparable,
+ string swaggerSchemaFilter,
bool nullableEnable)
{
+ SwaggerSchemaFilter = swaggerSchemaFilter;
BaseId = baseId;
Newtonsoft = newtonsoft;
SystemTextJson = systemTextJson;
diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs
index e33515310..5ed4a79d4 100644
--- a/src/StronglyTypedIds/SourceGenerationHelper.cs
+++ b/src/StronglyTypedIds/SourceGenerationHelper.cs
@@ -64,6 +64,7 @@ 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);
@@ -122,6 +123,12 @@ static string CreateId(
sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource);
}
+ if (useSchemaFilter)
+ {
+ sb.AppendLine(EmbeddedSources.SwaggerSchemaFilterAttributeSource);
+ }
+
+
sb.Append(resources.BaseId);
ReplaceInterfaces(sb, useIEquatable, useIComparable);
@@ -157,6 +164,11 @@ static string CreateId(
sb.AppendLine(resources.SystemTextJson);
}
+ if (useSchemaFilter)
+ {
+ sb.AppendLine(resources.SwaggerSchemaFilter);
+ }
+
sb.Replace("TESTID", idName);
sb.AppendLine(@" }");
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_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_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_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_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_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/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..404ca9257 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; }
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 877d3e4d1..fbc71fff0 100644
--- a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
@@ -381,6 +381,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/StringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
index 70508222c..76b9bdd3e 100644
--- a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
@@ -317,6 +317,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/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
index 82530749f..a6c3bf1f9 100644
--- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
@@ -20,13 +20,14 @@
-
+
+
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
index 9ad651d72..dcafd4207 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 { }
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..b3fcf5667 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
@@ -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 { }
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.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
index 32b324616..f9fffe68a 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,12 @@ namespace StronglyTypedIds
/// Creates a Dapper TypeHandler for converting to and from the type
///
DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Swagger SchemaFilter for OpenApi documentation
+ ///
+ SwaggerSchemaFilter = 64,
+
}
}
#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt
new file mode 100644
index 000000000..229ebb021
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt
new file mode 100644
index 000000000..0f97932a7
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt
new file mode 100644
index 000000000..ea3ade611
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt
new file mode 100644
index 000000000..f14ab3877
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt
new file mode 100644
index 000000000..0e69fb94b
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt
new file mode 100644
index 000000000..1b4e03230
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt
new file mode 100644
index 000000000..59db11881
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt
new file mode 100644
index 000000000..4c1d4947a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt
new file mode 100644
index 000000000..9ce5d9bae
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt
new file mode 100644
index 000000000..711654ffc
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt
new file mode 100644
index 000000000..fce195165
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt
@@ -0,0 +1,94 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt
new file mode 100644
index 000000000..fed90704a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt
@@ -0,0 +1,94 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt
new file mode 100644
index 000000000..7d7ee092c
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt
new file mode 100644
index 000000000..90fa44401
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt
new file mode 100644
index 000000000..2fd79a2b4
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt
new file mode 100644
index 000000000..6d82a6b07
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt
new file mode 100644
index 000000000..4fabc3182
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt
@@ -0,0 +1,108 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt
new file mode 100644
index 000000000..858205dda
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt
@@ -0,0 +1,108 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt
new file mode 100644
index 000000000..34c44711e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt
@@ -0,0 +1,118 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt
new file mode 100644
index 000000000..400a376b4
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt
@@ -0,0 +1,118 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt
new file mode 100644
index 000000000..0606930d2
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt
@@ -0,0 +1,99 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt
new file mode 100644
index 000000000..88ed2aa52
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt
@@ -0,0 +1,99 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt
new file mode 100644
index 000000000..9c2dfd631
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt
@@ -0,0 +1,109 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt
new file mode 100644
index 000000000..1eb3f4c2f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt
@@ -0,0 +1,109 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt
new file mode 100644
index 000000000..c1d784b3a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt
new file mode 100644
index 000000000..50aa16327
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt
new file mode 100644
index 000000000..7abca0a14
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt
@@ -0,0 +1,135 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt
new file mode 100644
index 000000000..771380c14
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt
@@ -0,0 +1,135 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt
new file mode 100644
index 000000000..9ab559bb0
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt
new file mode 100644
index 000000000..e86a12a94
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt
new file mode 100644
index 000000000..3202f6008
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt
@@ -0,0 +1,135 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt
new file mode 100644
index 000000000..5e26639ca
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt
@@ -0,0 +1,135 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt
new file mode 100644
index 000000000..faf5c1973
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt
@@ -0,0 +1,137 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt
new file mode 100644
index 000000000..7760b7b34
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt
@@ -0,0 +1,137 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt
new file mode 100644
index 000000000..254b1ea77
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt
@@ -0,0 +1,138 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt
new file mode 100644
index 000000000..b18e5d542
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt
@@ -0,0 +1,138 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt
new file mode 100644
index 000000000..a212749a6
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt
@@ -0,0 +1,142 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt
new file mode 100644
index 000000000..6abca8a56
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt
@@ -0,0 +1,142 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt
new file mode 100644
index 000000000..620c1710f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt
@@ -0,0 +1,143 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt
new file mode 100644
index 000000000..8bf8dd26a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt
@@ -0,0 +1,143 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt
new file mode 100644
index 000000000..3efdfa947
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt
@@ -0,0 +1,151 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt
new file mode 100644
index 000000000..f20a57ef8
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt
@@ -0,0 +1,151 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt
new file mode 100644
index 000000000..8fa913aea
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt
@@ -0,0 +1,161 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt
new file mode 100644
index 000000000..aeda230a3
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt
@@ -0,0 +1,161 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.verified.txt
new file mode 100644
index 000000000..c8fade53a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.verified.txt
@@ -0,0 +1,137 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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)
+ {
+ 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 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.verified.txt
new file mode 100644
index 000000000..6e46f01aa
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.verified.txt
@@ -0,0 +1,137 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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)
+ {
+ 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 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.verified.txt
new file mode 100644
index 000000000..0d5185930
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.verified.txt
@@ -0,0 +1,147 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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)
+ {
+ 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 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.verified.txt
new file mode 100644
index 000000000..4858dd900
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.verified.txt
@@ -0,0 +1,147 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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)
+ {
+ 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 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.verified.txt
new file mode 100644
index 000000000..5cd6629f8
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.verified.txt
@@ -0,0 +1,85 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.verified.txt
new file mode 100644
index 000000000..2537096c5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.verified.txt
@@ -0,0 +1,85 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.verified.txt
new file mode 100644
index 000000000..86563a15b
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.verified.txt
new file mode 100644
index 000000000..64aa3964a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.verified.txt
new file mode 100644
index 000000000..583ffca7f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.verified.txt
@@ -0,0 +1,85 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.verified.txt
new file mode 100644
index 000000000..1d1e03578
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.verified.txt
@@ -0,0 +1,85 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.verified.txt
new file mode 100644
index 000000000..23fa07284
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.verified.txt
new file mode 100644
index 000000000..7fd31a405
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt
new file mode 100644
index 000000000..5c58cc94f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt
new file mode 100644
index 000000000..aa75d90bd
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt
new file mode 100644
index 000000000..428f9e692
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt
@@ -0,0 +1,87 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt
new file mode 100644
index 000000000..b860c0820
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt
@@ -0,0 +1,87 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt
new file mode 100644
index 000000000..95a2049ba
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt
@@ -0,0 +1,85 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt
new file mode 100644
index 000000000..692c255cc
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt
@@ -0,0 +1,85 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt
new file mode 100644
index 000000000..6d04448d3
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt
new file mode 100644
index 000000000..f99fc8b1b
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt
@@ -0,0 +1,86 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.verified.txt
new file mode 100644
index 000000000..cc74782c6
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.verified.txt
@@ -0,0 +1,102 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.verified.txt
new file mode 100644
index 000000000..afe6106de
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.verified.txt
@@ -0,0 +1,102 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.verified.txt
new file mode 100644
index 000000000..afe6d365e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.verified.txt
@@ -0,0 +1,112 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.verified.txt
new file mode 100644
index 000000000..bbad1c5fb
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.verified.txt
@@ -0,0 +1,112 @@
+//------------------------------------------------------------------------------
+//
+// 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
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt
new file mode 100644
index 000000000..847e78d87
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt
new file mode 100644
index 000000000..fb897c9bf
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.verified.txt
new file mode 100644
index 000000000..10c531999
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.verified.txt
@@ -0,0 +1,103 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.verified.txt
new file mode 100644
index 000000000..5ea362830
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.verified.txt
@@ -0,0 +1,103 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(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();
+
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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)
+ {
+ 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 = false;
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.verified.txt
new file mode 100644
index 000000000..317a1bf6c
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.verified.txt
@@ -0,0 +1,127 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.verified.txt
new file mode 100644
index 000000000..a9e45110f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.verified.txt
@@ -0,0 +1,127 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.verified.txt
new file mode 100644
index 000000000..ee0d1dea5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.verified.txt
@@ -0,0 +1,128 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.verified.txt
new file mode 100644
index 000000000..10aa0e402
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.verified.txt
@@ -0,0 +1,128 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [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
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.verified.txt
new file mode 100644
index 000000000..de5067969
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.verified.txt
@@ -0,0 +1,127 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.verified.txt
new file mode 100644
index 000000000..7fac279df
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.verified.txt
@@ -0,0 +1,127 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.verified.txt
new file mode 100644
index 000000000..609dd414c
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.verified.txt
@@ -0,0 +1,128 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.verified.txt
new file mode 100644
index 000000000..3552bb9b5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.verified.txt
@@ -0,0 +1,128 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [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
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ 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"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.verified.txt
new file mode 100644
index 000000000..c85ca7254
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.verified.txt
@@ -0,0 +1,130 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.verified.txt
new file mode 100644
index 000000000..c75af94ec
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.verified.txt
@@ -0,0 +1,130 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ 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 class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 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.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.verified.txt
new file mode 100644
index 000000000..46b3ade84
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.verified.txt
@@ -0,0 +1,131 @@
+//------------------------------------------------------------------------------
+//
+// 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
+
+namespace Some.Namespace
+{
+ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ 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);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(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),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ 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 MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter