Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for passing enum params in Web API

I have a RESTful Web API project, and I have 2 different Enum scenarios that I'm unsure of re best practice.

Scenario 1 : Straightforward Enum Param

My API method requires a parameter called ruleType, with valid values being EmailAddress and IPAddress. My enum within the Web API project looks like this:

public enum RuleType {     None = 0,     EmailAddress = 1,     IPAddress = 2 } 

My question for this scenario is, should I use ?ruleType=EmailAddress in my request to the API (which automatically binds that value to my RuleType property within the API method)? If so, how best to validate that the RuleType param sent, is a valid RuleType Enum value?

Scenario 2 : Multiple Enum Values for a Single Param

My API method has an optional fields param, which is allows you to specify any additional data that should be returned. E.g. &fields=ruleOwner,rule. This would return those 2 extra bits of data in the response.

I have an enum in the Web API project which relates to each possible field that may be requested, and at present, I am splitting the comma separated fields param, then looping through each string representation of that enum, parsing it to the equivalent enum, resulting in a list of Enum values which I can then use within my API to retrieve the relevant data.

This is the Enum:

public enum OptionalField {     None = 0,     RuleOwner = 1,     Rule = 2,     etc. } 

What would be best practice here? I was looking into bitwise enums, so a single value is sent in the API request which resulted in any combination of fields but didn't know if this would work well with a Web API, or if there's generally a better way to go about this?

like image 959
marcusstarnes Avatar asked Sep 30 '16 10:09

marcusstarnes


People also ask

How do you pass enum in Request Param?

We can then use this enum as a RequestParameter in a Spring controller: @GetMapping("/mode2str") public String getStringToMode(@RequestParam("mode") Modes mode) { // ... } Or we can use it as a PathVariable: @GetMapping("/findbymode/{mode}") public String findByEnum(@PathVariable("mode") Modes mode) { // ... }

Can you pass an enum?

An enum is a class. So you can pass an instance of the class ( EnumValues. generalInformation. PHONE for example), or you can pass the class itself ( EnumValues.

How do you use enum in DTO?

You can use the name() or toString() method of your Enum . You can provide an additional "translation" inside the Enum like: public enum Status { S1("translation1"), S2("translation2"), S3("translation3"); private final String translation; private Status(String t) { translation = t; } ... }


1 Answers

The simplest answer is, "It doesn't matter".

If the parameter in your controller method is of the enumeration type

public IHttpActionResult Foo(RuleType ruleType) 

In WebAPI, It Just Works - no matter if the client request URL specifies the parmeter value as ?ruleType=1 or ?ruleType=EmailAddress

If the client specifies a value that isn't valid for the enumeration, an exception is thrown (The parameters dictionary contains a null entry for parameter 'ruleType' of non-nullable type 'RuleType' for method 'Foo' ... and the client gets a 400 Bad Request response.

like image 87
Mr. T Avatar answered Oct 15 '22 00:10

Mr. T