Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I get enum string values in my contract? #527

Closed
onno-amsterdam opened this issue Nov 12, 2024 · 1 comment
Closed

How do I get enum string values in my contract? #527

onno-amsterdam opened this issue Nov 12, 2024 · 1 comment

Comments

@onno-amsterdam
Copy link

Hi guys, love the tool but can use some help.

I've been trying to create a contract with enum values, I get number but I want the string value as my provider returns strings. How can I make sure the contract will contain "gender" : "Male" instead of "gender": 0? Below is a simplified example, however the class I'm using has many many enums.

using IPactBuilderV4;

Let's say I have this object:

public class Person {
   public Gender Gender {get; set;}
}

public enum Gender {
   [Display(Name = "Male")]
   Male
}

And this is my consumer contract code (simplified snippet):

var person = new Person()
{
    Gender = Gender.Male,
};

this.pact
    .UponReceiving("a for a person")
        .Given("a person exists in database")
        .WithRequest(HttpMethod.Get, $"/person")
    .WillRespond()
        .WithStatus(HttpStatusCode.OK)
        .WithJsonBody(Match.Type(person);

I get this contract (snippet)

...
"response": {
        "body": {
          "content": {
            "gender": 0
          },
          "contentType": "application/json",
          "encoded": false
        },
...
@adamrodger
Copy link
Contributor

You would add the JsonStringEnumConverter to your serialiser settings to control whether enums are serialised as strings or numbers

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter?view=net-8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants