BodyAsFile .json files interferes with WatchStaticMappings #127

Closed
opened 2025-12-29 14:23:16 +01:00 by adam · 3 comments
Owner

Originally created by @markdebruijne on GitHub (Aug 12, 2018).

  • Configure ReadStaticMappings(myMappingsFolder) to import request matchers from static .json files
  • Also Configure FileWatchers for that folder: WatchStaticMappings(myMappingsFolder)
  • Configure one of those requests to respond based on a BodyAsFile
"Response": {
    "StatusCode": 200,
    "BodyAsFile": "mappings/myResponseBodyFile.json"
  }
  • Run the application, which will monitor all files in that folder
  • Update myResponseBodyFile.json

Result: Unhandled exception occurs

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
  Source=WireMock.Net
  StackTrace:
   at WireMock.Validation.Check.NotNull[T](T value, String parameterName) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Validation\Check.cs:line 44
   at WireMock.Server.FluentMockServer.DeserializeAndAddOrUpdateMapping(MappingModel mappingModel, Nullable`1 guid, String path) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Server\FluentMockServer.Admin.cs:line 415
   at WireMock.Server.FluentMockServer.ReadStaticMappingAndAddOrUpdate(String path) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Server\FluentMockServer.Admin.cs:line 202
   at WireMock.Server.FluentMockServer.<WatchStaticMappings>b__12_1(Object sender, FileSystemEventArgs args) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Server\FluentMockServer.Admin.cs:line 164
   at WireMock.Util.EnhancedFileSystemWatcher.OnChanged(FileSystemEventArgs e) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Util\EnhancedFileSystemWatcher.cs:line 128
   at WireMock.Util.EnhancedFileSystemWatcher.OnChanged(Object sender, FileSystemEventArgs e) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Util\EnhancedFileSystemWatcher.cs:line 224
   at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(WatcherChangeTypes changeType, String name)
   at System.IO.FileSystemWatcher.ParseEventBufferAndNotifyForEach(Byte[] buffer)
   at System.IO.FileSystemWatcher.ReadDirectoryChangesCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

It breaks because the .json file only contains the Body of the response, while the deserializer is expecting both a Request: {} and Response: {} element

This feels like a bug because I want to have my request .json and responseBody .json stored close together on my file system. Is there another approach to store them differently which is compatible with the file watchers. I do want (during development) want to be able to watch both.

Originally created by @markdebruijne on GitHub (Aug 12, 2018). - Configure `ReadStaticMappings(myMappingsFolder)` to import request matchers from static `.json` files - Also Configure FileWatchers for that folder: `WatchStaticMappings(myMappingsFolder)` - Configure one of those requests to respond based on a `BodyAsFile` ``` "Response": { "StatusCode": 200, "BodyAsFile": "mappings/myResponseBodyFile.json" } ``` - Run the application, which will monitor all files in that folder - Update `myResponseBodyFile.json` Result: Unhandled exception occurs ``` System.ArgumentNullException HResult=0x80004003 Message=Value cannot be null. Source=WireMock.Net StackTrace: at WireMock.Validation.Check.NotNull[T](T value, String parameterName) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Validation\Check.cs:line 44 at WireMock.Server.FluentMockServer.DeserializeAndAddOrUpdateMapping(MappingModel mappingModel, Nullable`1 guid, String path) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Server\FluentMockServer.Admin.cs:line 415 at WireMock.Server.FluentMockServer.ReadStaticMappingAndAddOrUpdate(String path) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Server\FluentMockServer.Admin.cs:line 202 at WireMock.Server.FluentMockServer.<WatchStaticMappings>b__12_1(Object sender, FileSystemEventArgs args) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Server\FluentMockServer.Admin.cs:line 164 at WireMock.Util.EnhancedFileSystemWatcher.OnChanged(FileSystemEventArgs e) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Util\EnhancedFileSystemWatcher.cs:line 128 at WireMock.Util.EnhancedFileSystemWatcher.OnChanged(Object sender, FileSystemEventArgs e) in D:\data\inetpub\WireMock.Net\src\WireMock.Net\Util\EnhancedFileSystemWatcher.cs:line 224 at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(WatcherChangeTypes changeType, String name) at System.IO.FileSystemWatcher.ParseEventBufferAndNotifyForEach(Byte[] buffer) at System.IO.FileSystemWatcher.ReadDirectoryChangesCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) ``` It breaks because the `.json` file only contains the Body of the response, while the deserializer is expecting both a `Request: {}` and `Response: {}` element This feels like a bug because I want to have my request .json and responseBody .json stored close together on my file system. Is there another approach to store them differently which is compatible with the file watchers. I do want (during development) want to be able to watch both.
adam closed this issue 2025-12-29 14:23:16 +01:00
Author
Owner

@StefH commented on GitHub (Aug 12, 2018):

The ReadStaticMappings and WatchStaticMappings are meant to be used for the complete mappings. A complete mapping contains both the request and the response. For an example, see https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#__adminmappings-1.

If you want to use BodyAsFile, you could just try to use a file with another extension, like .response. That kind of files will not be read or watched.

Note that you probably need to set caching to false, (BodyAsFileIsCached = false) for that specific mapping.

@StefH commented on GitHub (Aug 12, 2018): The ReadStaticMappings and WatchStaticMappings are meant to be used for the complete mappings. A complete mapping contains both the **request** and the **response**. For an example, see https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#__adminmappings-1. If you want to use `BodyAsFile`, you could just try to use a file with another extension, like `.response`. That kind of files will not be read or watched. Note that you probably need to set caching to false, (BodyAsFileIsCached = false) for that specific mapping.
Author
Owner

@StefH commented on GitHub (Sep 11, 2018):

@markdebruijne does above text answer your question? Or do you still have more questions?

@StefH commented on GitHub (Sep 11, 2018): @markdebruijne does above text answer your question? Or do you still have more questions?
Author
Owner

@StefH commented on GitHub (Oct 12, 2018):

@markdebruijne Closing this issue.

If you still have questions, comment here or create a new issue.

@StefH commented on GitHub (Oct 12, 2018): @markdebruijne Closing this issue. If you still have questions, comment here or create a new issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#127