Admin API: PUT Mapping, FormatException because of wrong parsing of the Query #203

Closed
opened 2025-12-29 14:25:02 +01:00 by adam · 2 comments
Owner

Originally created by @andi0b on GitHub (Aug 20, 2019).

Originally assigned to: @StefH on GitHub.

It sounds a bit crazy, but if you PUT a new Mapping through the Admin API you get an Exception if the GUID starts with a or d. This happens because of this line of code:

12444cc11e/src/WireMock.Net/Server/FluentMockServer.Admin.cs (L362)

It trims away characters from the beginning, including a and d. We had flaky unit tests because of that and could't believe it in the beginning when we found that issue ;)

How to reproduce:

Start Wiremock.NET 1.0.27 with Admin API and send Request

PUT http://localhost:9981/__admin/mappings/a0000000-0000-0000-0000-000000000000 HTTP/1.1
Host: localhost:9981
User-Agent: insomnia/6.6.2
Connection: Keep-Alive
Content-Type: application/json
Accept: */*
Content-Length: 35

{
	"Request": {},
	"Response": {}
}

Then you will receive this error:

System.FormatException Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
   at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException)
   at System.Guid.TryParseGuidWithDashes(ReadOnlySpan`1 guidString, GuidResult& result)
   at System.Guid.TryParseGuid(ReadOnlySpan`1 guidString, GuidStyles flags, GuidResult& result)
   at System.Guid.Parse(ReadOnlySpan`1 input)
   at System.Guid.Parse(String input)
   at WireMock.Server.FluentMockServer.MappingPut(RequestMessage requestMessage)
   at WireMock.ResponseProviders.DynamicResponseProvider.ProvideResponseAsync(RequestMessage requestMessage, IFluentMockServerSettings settings)
   at WireMock.Mapping.ResponseToAsync(RequestMessage requestMessage)
   at WireMock.Owin.WireMockMiddleware.InvokeInternal(HttpContext ctx)

because this get's excuted:

string guidString = "/__admin/mappings/a0000000-0000-0000-0000-000000000000".TrimStart("/__admin/mappings".ToCharArray());
//  guidString = "0000000-0000-0000-0000-000000000000" (the a get's trimmed away)

Guid guid = Guid.TryParse(guidString); 
// Exception

What I expect:

Put another GUID there, and it suddenly works:

PUT http://localhost:9981/__admin/mappings/b0000000-0000-0000-0000-000000000000 HTTP/1.1
Host: localhost:9981
User-Agent: insomnia/6.6.2
Connection: Keep-Alive
Content-Type: application/json
Accept: */*
Content-Length: 35

{
	"Request": {},
	"Response": {}
}

HTTP/1.1 200 OK
Date: Tue, 20 Aug 2019 11:40:49 GMT
Content-Type: application/json
Server: Kestrel
Content-Length: 37

{"Status":"Mapping added or updated"}
Originally created by @andi0b on GitHub (Aug 20, 2019). Originally assigned to: @StefH on GitHub. It sounds a bit crazy, but if you PUT a new Mapping through the Admin API you get an Exception if the GUID starts with `a` or `d`. This happens because of this line of code: https://github.com/WireMock-Net/WireMock.Net/blob/12444cc11e0c806a2436e2a823227dc757781b29/src/WireMock.Net/Server/FluentMockServer.Admin.cs#L362 It trims away characters from the beginning, including `a` and `d`. We had flaky unit tests because of that and could't believe it in the beginning when we found that issue ;) # How to reproduce: Start Wiremock.NET 1.0.27 with Admin API and send Request ```http PUT http://localhost:9981/__admin/mappings/a0000000-0000-0000-0000-000000000000 HTTP/1.1 Host: localhost:9981 User-Agent: insomnia/6.6.2 Connection: Keep-Alive Content-Type: application/json Accept: */* Content-Length: 35 { "Request": {}, "Response": {} } ``` Then you will receive this error: ```c# System.FormatException Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException) at System.Guid.TryParseGuidWithDashes(ReadOnlySpan`1 guidString, GuidResult& result) at System.Guid.TryParseGuid(ReadOnlySpan`1 guidString, GuidStyles flags, GuidResult& result) at System.Guid.Parse(ReadOnlySpan`1 input) at System.Guid.Parse(String input) at WireMock.Server.FluentMockServer.MappingPut(RequestMessage requestMessage) at WireMock.ResponseProviders.DynamicResponseProvider.ProvideResponseAsync(RequestMessage requestMessage, IFluentMockServerSettings settings) at WireMock.Mapping.ResponseToAsync(RequestMessage requestMessage) at WireMock.Owin.WireMockMiddleware.InvokeInternal(HttpContext ctx) ``` because this get's excuted: ```c# string guidString = "/__admin/mappings/a0000000-0000-0000-0000-000000000000".TrimStart("/__admin/mappings".ToCharArray()); // guidString = "0000000-0000-0000-0000-000000000000" (the a get's trimmed away) Guid guid = Guid.TryParse(guidString); // Exception ``` # What I expect: Put another GUID there, and it suddenly works: ```http PUT http://localhost:9981/__admin/mappings/b0000000-0000-0000-0000-000000000000 HTTP/1.1 Host: localhost:9981 User-Agent: insomnia/6.6.2 Connection: Keep-Alive Content-Type: application/json Accept: */* Content-Length: 35 { "Request": {}, "Response": {} } HTTP/1.1 200 OK Date: Tue, 20 Aug 2019 11:40:49 GMT Content-Type: application/json Server: Kestrel Content-Length: 37 {"Status":"Mapping added or updated"} ```
adam added the bug label 2025-12-29 14:25:02 +01:00
adam closed this issue 2025-12-29 14:25:02 +01:00
Author
Owner

@StefH commented on GitHub (Aug 20, 2019):

Good catch !
https://github.com/WireMock-Net/WireMock.Net/pull/326

@StefH commented on GitHub (Aug 20, 2019): Good catch ! https://github.com/WireMock-Net/WireMock.Net/pull/326
Author
Owner

@StefH commented on GitHub (Aug 20, 2019):

solved

@StefH commented on GitHub (Aug 20, 2019): solved
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#203