You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The idea of this kind of structure is to have a type that has its own validation, and can't be assigned wrongly to another similar type by mistake (imagine a situation where you have a "string productId" and a "string skuId" and they might be mismatched).
System.Text.Json is very nice here, because it allows me to create my own converter that parses the value and transforms it if necessary
publicclassProductIdConverter:JsonConverter<ProductId>{publicoverride ProductId Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){varstr= reader.GetString()!;// update the product id to a new version. idk, this is just an example.if(str.StartsWith("v1"))str= ProductIdLegacyUpdater.Update(str);returnnew ProductId(str);}publicoverridevoidWrite(Utf8JsonWriterwriter,ProductIdvalue,JsonSerializerOptionsoptions){
writer.WriteStringValue(value.Value);}}
This allows me to make my boundaries open to string values without the API user having to understand this abstraction, but the problem is that Swagger will recognize this as an object by default. But that's fine, I can configure that on startup
however, that will require referencing the ProductId part on startup and it'll quickly get verbose. Of course, there are ways to organize this so it's in a separate package and all that, but that's not really what I want
Describe the solution you'd like
I wish I could add an attribute to my class/record/struct/enum that tells swagger what is the expected OpenApiSchema for that this type should be represented by, in a similar way that JsonConverter does it.
Think of it like this: Right now, we can use .AddJsonOptions to customize our JSON converters with our service builders, or we can use JsonConverterAttribute to customize the converter on a case-by-case basis, applying it to an entire class, a single property of a class and so on.
I wish I could have the same DX for Custom Type Mappings, something like a SwaggerCustomTypeAttribute that would take a type that provides a Func<OpenApiSchema> or something similar, and then use that information to register it as a type when swagger is generating its information.
Additional context
I managed to implement a similar behavior in my project like so:
and then I can add [SwaggerCustomType(typeof(ProductIdSwaggerTypeProvider))] to my value object(ProductId in this example), where ProductIdSwaggerTypeProvider : ISwaggerCustomTypeProvider.
Finally, I added the following to my swagger gen options:
foreach(var type in typesToMap){if(Attribute.GetCustomAttribute(type,typeof(SwaggerCustomTypeAttribute))is SwaggerCustomTypeAttribute typeProvider){
options.SchemaGeneratorOptions.CustomTypeMappings.Add(type, typeProvider.GetSwaggerCustomTypeProvider());}}
where typesToMap is a manually written array of Types that I know have the SwaggerCustomTypeAttribute, though I could probably do a more automated version of that by scanning all types in the assembly.
My point is, I wish I could have that attribute and avoid the whole writing down the value objects array, or scanning the assembly for implementing types, or even have the attribute work on properties and/or parameters, in the same way that JsonConverterAttribute works.
All this said, if this feature exists somewhere I failed to find it within the docs so I'd appreciate a direction for that :)
Thank you
The text was updated successfully, but these errors were encountered:
Is your feature request related to a specific problem? Or an existing feature?
Consider the following scenario, where I've defined a Value Object to encapsulate a custom-tailored Id
The idea of this kind of structure is to have a type that has its own validation, and can't be assigned wrongly to another similar type by mistake (imagine a situation where you have a "string productId" and a "string skuId" and they might be mismatched).
System.Text.Json is very nice here, because it allows me to create my own converter that parses the value and transforms it if necessary
This allows me to make my boundaries open to string values without the API user having to understand this abstraction, but the problem is that Swagger will recognize this as an object by default. But that's fine, I can configure that on startup
however, that will require referencing the
ProductId
part on startup and it'll quickly get verbose. Of course, there are ways to organize this so it's in a separate package and all that, but that's not really what I wantDescribe the solution you'd like
I wish I could add an attribute to my class/record/struct/enum that tells swagger what is the expected OpenApiSchema for that this type should be represented by, in a similar way that JsonConverter does it.
Think of it like this: Right now, we can use
.AddJsonOptions
to customize our JSON converters with our service builders, or we can useJsonConverterAttribute
to customize the converter on a case-by-case basis, applying it to an entire class, a single property of a class and so on.I wish I could have the same DX for Custom Type Mappings, something like a
SwaggerCustomTypeAttribute
that would take a type that provides aFunc<OpenApiSchema>
or something similar, and then use that information to register it as a type when swagger is generating its information.Additional context
I managed to implement a similar behavior in my project like so:
and then I can add
[SwaggerCustomType(typeof(ProductIdSwaggerTypeProvider))]
to my value object(ProductId in this example), whereProductIdSwaggerTypeProvider : ISwaggerCustomTypeProvider
.Finally, I added the following to my swagger gen options:
where
typesToMap
is a manually written array ofType
s that I know have theSwaggerCustomTypeAttribute
, though I could probably do a more automated version of that by scanning all types in the assembly.My point is, I wish I could have that attribute and avoid the whole writing down the value objects array, or scanning the assembly for implementing types, or even have the attribute work on properties and/or parameters, in the same way that
JsonConverterAttribute
works.All this said, if this feature exists somewhere I failed to find it within the docs so I'd appreciate a direction for that :)
Thank you
The text was updated successfully, but these errors were encountered: