C
C#13mo ago
LazyGuard

❔ Prevent accepting numeric values as enum

Hello, I have an API that exposes an enum field Vehicle (Car, Bike, Truck) in the POST. The problem is that when I POST a request with a body {"vehicle" :"1" }, it gets accepted and gets automatically converted to Vehicle.Bike.. But something like {"vehicle" :"bar" } results in a bad request, which is what I need ! Any idea on how to stop parsing integer like enum values?
17 Replies
RubyNovaDev
RubyNovaDev13mo ago
You would require a custom mapping for that As enums are, at their core, integers So by default they're gonna end up as numbers Depending on how you are mapping your model, newtownoft Vs STJ, the requirements for this will probably differ I am unsure how you would explicitly make it a bad request to pass it as a number but you can probably bake it into the custom mapping somehow
LazyGuard
LazyGuard13mo ago
What do you mean by a custom mapping ?
RubyNovaDev
RubyNovaDev13mo ago
By default when you're using a model it will do it's best based on the reflection data to build a JSON object If you want behaviour that is different you have to specify it yourself
LazyGuard
LazyGuard13mo ago
Okay I see ! Do you have some code examples on how to do this things ?
RubyNovaDev
RubyNovaDev13mo ago
^ But even if I knew this it's been too long for me to remember exactly Your best bet is the documentation for whatever it is you're using for this
LazyGuard
LazyGuard13mo ago
How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
LazyGuard
LazyGuard13mo ago
Is that what you mean ? ovveriding the Read and Wrtie function of the jsonConverter of the enum
RubyNovaDev
RubyNovaDev13mo ago
If you are using STJ That will probably do it
LazyGuard
LazyGuard13mo ago
Yu I am using STJ
RubyNovaDev
RubyNovaDev13mo ago
Well all I can say is give it a whirl and see what explodes 🙂
LazyGuard
LazyGuard13mo ago
Yup, but still thinking about how to say "is not an integer value" 😆 Oh maybe there is some int.TryParse by default
RubyNovaDev
RubyNovaDev13mo ago
I'm sure you'll figure it iut Out* MSDocs are some of the best around after all
LazyGuard
LazyGuard13mo ago
yup yup, thanks dude 🙏
Shirasho
Shirasho13mo ago
STJ also has a string enum converter that accepts a bool setting whether integers are valid.
LazyGuard
LazyGuard13mo ago
yeah but when putting it to false, it will not accept 1,2,3 etc. but still accepts "1","2","3" etc.
Angius
Angius13mo ago
https://stackoverflow.com/a/59096114/6042255
services
.AddMvc()
// Or .AddControllers(...)
.AddJsonOptions(opts =>
{
var enumConverter = new JsonStringEnumConverter();
opts.JsonSerializerOptions.Converters.Add(enumConverter);
})
services
.AddMvc()
// Or .AddControllers(...)
.AddJsonOptions(opts =>
{
var enumConverter = new JsonStringEnumConverter();
opts.JsonSerializerOptions.Converters.Add(enumConverter);
})
Won't this do?
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Column names in each table must be unique. Column name x in table yThere is an Azure SQL db with 10 DTUs and migrations are broken with the following message: > Colum❔ Create list from items and other listsWhats the best way to create a new list that combines an item A, an item B, and a list C?✅ ef not being able to update database migrationsI've been trying to learn aspnet and done just the basics of it. But when using `dotnet ef database ❔ Null warning from accessing weather results from an API callI'm creating a weather app that allows you to type in the name of a city and then get it's informati❔ Visual Studio 2022 Super Slow Since The Last UpdateI pressed the Build 10 minutes ago and the building is still in progress. My app only have 1 button ❔ There is a DevExpress GridControl and i want to find a way to loop over its rowsCan someone help me find online how to do this thing. i want to loop over its data so that i can addSource Generator Auto Update Extension@admsnyder Thanks for reaching back out to me. This is in regards to the post I made in Q&A on the e❔ I want to find objects within objects. using composite patternHello I have an sudoku application that can check weither an number is correct or not. The current s❔ Globally Update Nuget PackagesEvery time I make a solution in visual studio, it wants to update these four packages. Can I update ❔ want to add a conditional .Take(10) to a query without duplicating the codeIm looking for an easy way to add a .Take(10) or .Take(x) to a iqueryable so that my test go faster