Does Asp.Net Core exposes too much information for required enums that were not supplied?

I have a simple code for an input model:

public class MyClass {     [Required]     public MyEnum? Type { get; set; } } 

Now if I do not send Type as a part of json to the request, I get this error from Web.Api:

“The JSON value could not be converted to System.Nullable`1[MyNamespace.MyClass]. Path: $ .type | LineNumber: 2 | BytePositionInLine: 16.”

This really looks like information exposure to me, though I cannot see any real danger in exactly this information, but still, more that nothing.

Is it of any real concern or is it just fine?

Enum design: when an “open” set would be acceptable as enum’s values?

I was looking into the Enum design recommended by Microsoft (see here), and I found a statement that made me stop to think for a while:

X DO NOT use an enum for open sets (such as the operating system version, names of your friends, etc.).

It says that OS versions are considered as open set, therefore it should not be enumerated. Moving forward with my doubt: I’m developing a proxy to an API, and I’m thinking in allow the proxy’s consumers choose the API version. The approach that I got in mind it’s just define an enum that lists all the API versions, thus consumers are allowed to pass which API version they want to use.

However, due to API versions is something that would be considered as open (eventually there will be more versions), I’m doubting in include the version choosing feature. So, does this guideline not fit in my scenario?

What is better: Concrete class with Enums or subclasses implementing an Interface?

Suppose for example, there could be multiple types of vehicles. And in this scenario, only the speed and how many people can seat in the vehicle matters.

Would it be better to have a concrete class, with vehicle type as an Enum, or should there be an vehicle interface and different types of vehicle implementing this interface?

I’ve got enums on top of enums, and then some more traits for those enums… it’s getting unwieldy

So I have an enum wrapping an enum, but I have another enum talking about what that enum is.

Which makes for a confusing English sentence, so here’s an example.

I got one enum:

enum Example {     A(Something),     B{ structfield: K },     C(Gamma, Third),     D(InsertSomethingElseCrazyHereThatCantImplementANYTraitsCauseReasons), } 

Which is all fine and dandy, but I needed another way to categorize them and compare the categories without matching. I also needed to pass along Examples without fields, and then I realized that I should probably use an enum describing my enum, so I ended up doing this:

#[derive(Eq, PartialEq, YouGetThePoint)] enum ExampleCategory {     A, B, C, D } 

Then, cause I wanna easily go from Example to ExampleCategory:

impl From<Example> for ExampleCategory {     fn from(e: Example) {         match e {             Example::A(_) => ExampleCategory::A             Example::B{..} => ExampleCategory::B             Example::C(_,_,_) => ExampleCategory::C,             Example::D(_) => ExampleCategory::D         }     } } 

So now, every time I need to add an Example, I end up needing to change code in three different places, and if I want a new trait... etc. Also, if theSomethinginExample::A` were another kind of this enum. *shudders

I think I made my point on why this is probably a bad idea.

I’m 90% positive there’s a better way to do this (that doesn’t involve macros since I don’t even know how I would go about doing that). How would I change this to be more DRY/better? Since the Example enum is actually a part of a larger issue.

Single chars mapped to enums in Rust

I’m writing a simple program to parse lines read from a file. As I’m new to Rust, I wonder if I’m creating too much boilerplate.

A sample line in the file looks like this: pqrXYZ12a9. Char at position 8 (starting from 0) can be a, b, or c and let’s say it represents some sort of “category”.

I think of modelling that specific field with an enum. Then I realised I need a function to parse the actual value to the specific variant.

Here’s my code – questions afterwards:

enum Category {         A,         B,         C,     }      fn parse_category(line: String) -> Category {         let category = &line[8 .. 9];                      match category {             "a" => Category::A,             "b" => Category::B,             "c" => Category::C,             _ => panic!(),         }     }          fn main() {         let test_string = String::from("pqrXYZ12a9");          match parse_category(test_string) {               Category::A => println!("Cat A"),            Category::B => println!("Cat B"),             Category::C => println!("Cat C"),         } } 

Things I’m not sure about, specifically are:

  • Can I avoid creating a parse_category function, and assign the specific letter to the Category variants? In fact, should I or should I not? Something like:

    // does not compile enum Category { A = ‘a’, … }

  • Is there a way to “name the slice” in parse_category? I’d like to avoid putting magical numbers here and there and have all fields defined somewhere together. In Python I’d create a slice object such as category_field = slice(8, 9) and then pass it as line[category_field].

  • Any other feedback is welcome.

Using Enums in a Class

I wrote a card class a while back in this post here: Previous Question I know it has been a long time but I recently came back to the project and wrote it using an ENUM as suggested in the answer to increase readability and to make input less error prone. I’ve never really used enums in python so the question is did I do it correctly or is there a better way to do it?

from enum import Enum  class Suit(Enum):     CLUB, HEART, DIAMOND, SPADE = range(1, 5)  class Rank(Enum):     TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)     JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'  class Card(object):     """Models a playing card, each Card object will have a suit, rank, and weight associated with each.      possible_suits -- List of possible suits a card object can have     possible_ranks -- List of possible ranks a card object can have     Suit and rank weights are initialized by position in list.     If card parameters are outside of expected values, card becomes joker with zero weight """      def __init__(self, suit, rank, in_deck = False):         if suit in Suit and rank in Rank:             self.suit = suit             self.rank = rank             self.suit_weight = suit.value             self.rank_weight = rank.value         else:             self.suit = "Joker"             self.rank = "J"             self.suit_weight = 0             self.rank_weight = 0         self.in_deck = in_deck      def __str__(self):         """Returns abbreviated name of card          Example: str(Card('Spades', 'A') outputs 'AS'         """         return str(self.rank.value) + str(self.suit.name[0])      def __eq__(self, other):         """Return True if cards are equal by suit and rank weight"""         return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight      def __gt__(self, other):         """Returns true if first card is greater than second card by weight"""         if self.suit_weight > other.suit_weight:             return True         if self.suit_weight == other.suit_weight:             if self.rank_weight > other.rank_weight:                 return True         return False      def modify_weight(self, new_suit_weight = None, new_rank_weight = None):         """Modifies weight of card object"""         if new_suit_weight:             self.suit_weight = new_suit_weight         if new_rank_weight:             self.rank_weight = new_rank_weight      def is_in_deck(self):         """Return True if card is in a deck, else false"""         return self.in_deck      def get_suit(self):         return self.suit      def get_rank(self):         return self.rank      def get_suit_weight(self):         return self.suit_weight      def get_rank_weight(self):         return self.rank_weight 

How could I apply enums to this block of code that checks against user input and returns a response based on that input?

How could I use enums in the following block of code to simplify it? The block is a program that has a command interpreter that returns different data based on user input. Is there a way I could easily apply enums and a match statement to the logic here?

use std::io;  fn main() {     let version = String::from("0.0.1");     let mut input = String::new();      shell(&mut input, &version); }  fn shell(input: &mut String, version: &String) {     input.clear();     match io::stdin().read_line(input) {         Ok(b) => {             if &input.trim() == &"ver" || &input.trim() == &"what are you" {                 println!("C ute R ust A mateur B eginner Shell ver: {}", version);                 shell(input, version);             } else {                 println!("Command '{}' Not Recognized", input);                 shell(input, &version);             }         }         Err(err) => panic!("incorrect"),     } }