JsonMatcher should serialize enum as text if such attribute is defined #426

Closed
opened 2025-12-29 15:23:33 +01:00 by adam · 4 comments
Owner

Originally created by @ghost on GitHub (Jul 8, 2022).

Describe the bug

The JsonMatcher does not respect the enum serialization attributes

Expected behavior:

The matcher should match on a json containing the enum as a string instead of a number

Test to reproduce

public enum MyType
{
	[JsonConverter(typeof(StringEnumConverter))]  // newtonsoft
	Type1
}

public class Request
{
	public MyType MyType { get; set; }
}

var request = new MyRequest { MyType = MyType.Type1 };

In the WireMock Given definition:

.WithBody(new JsonMatcher(MatchBehaviour.AcceptOnMatch, request, true))

The request won't be matched because the json matcher (see its private _valueAsJToken) tries to match on MyEnum = 0, instead of MyType1

What serializer is used? Is there a way to tweak it so it serializes enums as strings?

Originally created by @ghost on GitHub (Jul 8, 2022). ### Describe the bug The JsonMatcher does not respect the enum serialization attributes ### Expected behavior: The matcher should match on a json containing the enum as a string instead of a number ### Test to reproduce ``` public enum MyType { [JsonConverter(typeof(StringEnumConverter))] // newtonsoft Type1 } public class Request { public MyType MyType { get; set; } } var request = new MyRequest { MyType = MyType.Type1 }; ``` In the WireMock Given definition: ``` .WithBody(new JsonMatcher(MatchBehaviour.AcceptOnMatch, request, true)) ``` The request won't be matched because the json matcher (see its private _valueAsJToken) tries to match on MyEnum = 0, instead of MyType1 ### Other related info What serializer is used? Is there a way to tweak it so it serializes enums as strings?
adam added the question label 2025-12-29 15:23:33 +01:00
adam closed this issue 2025-12-29 15:23:34 +01:00
Author
Owner

@StefH commented on GitHub (Jul 8, 2022):

@FredCni
Can you make a full working example project, or a PullRequest with a new unit-test which demonstrates this error?

@StefH commented on GitHub (Jul 8, 2022): @FredCni Can you make a full working example project, or a PullRequest with a new unit-test which demonstrates this error?
Author
Owner

@StefH commented on GitHub (Jul 9, 2022):

#770

@StefH commented on GitHub (Jul 9, 2022): #770
Author
Owner

@StefH commented on GitHub (Jul 9, 2022):

@FredCni
The attribute is wrongly placed. Use this:

[JsonConverter(typeof(StringEnumConverter))]
public enum MyType
{
	Type1
}
@StefH commented on GitHub (Jul 9, 2022): @FredCni The attribute is wrongly placed. Use this: ``` csharp [JsonConverter(typeof(StringEnumConverter))] public enum MyType { Type1 } ```
Author
Owner

@ghost commented on GitHub (Jul 11, 2022):

Thank you, that fixed it!

@ghost commented on GitHub (Jul 11, 2022): Thank you, that fixed it!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#426