Skip to content

Generated C# code for stateful (Scenario) mappings does not compile and drops the WhenStateIs condition #1501

Closed

Description

@Bafyn

Summary

The C# code generator exposed via GET /__admin/mappings/code produces incorrect code for any mapping that uses a Scenario/State.

For a mapping configured with .WhenStateIs(...) and/or .WillSetStateTo(...), the generator emits:

.WhenStateIs("<NextState>", <TimesInSameState>)

This is wrong in two ways:

  1. It does not compile. There is no WhenStateIs(string, int) overload, only WhenStateIs(string) and WhenStateIs(int) exist. So the generated code cannot be pasted back into a project and built.
  2. It silently drops the execution condition. The mapping's actual WhenStateIs value is never emitted at all, so the condition disappears from the generated code.

Steps to reproduce

var server = WireMockServer.StartWithAdminInterface();

// A stateful (Scenario) mapping:
//  - only eligible when the scenario "checkout" is in state "StepA"
//  - moves the scenario to state "StepB" after it has matched twice
server
    .Given(Request.Create().WithPath("/order").UsingGet())
    .InScenario("checkout")
    .WhenStateIs("StepA")
    .WillSetStateTo("StepB", 2)
    .RespondWith(Response.Create().WithSuccess());

// Generate the equivalent C# code for the mappings.
using var http = new HttpClient();
var code = await http.GetStringAsync($"{server.Url}/__admin/mappings/code");
Console.WriteLine(code);

The generated output contains:

server
    .Given(Request.Create()
        .UsingMethod("GET")
        .WithPath(...)
    )
    .WithGuid("...")
    .InScenario("checkout")
    .WhenStateIs("StepB", 2)          // does not compile; wrong value; "StepA" is lost
    .RespondWith(Response.Create()
        .WithStatusCode(200)
    );

Trying to compile the generated snippet fails, e.g.:

error CS1501: No overload for method 'WhenStateIs' takes 2 arguments

Expected behavior

The generated code should reconstruct the original mapping and compile as-is:

    .InScenario("checkout")
    .WhenStateIs("StepA")
    .WillSetStateTo("StepB", 2)

Actual behavior

  • The generated code uses a non-existent WhenStateIs(string, int) overload, so it does not compile.
  • The NextState value is emitted under WhenStateIs.
  • The real WhenStateIs condition is omitted entirely.

Notes

  • Only C# code generation is affected; JSON serialization and round-trip of scenario state is correct.

Activity

  1. added
    bug
    on Aug 17, 2026
  2. self-assigned this
    on Aug 17, 2026
  3. StefH commented on Aug 17, 2026

    @StefH
    Collaborator

    Thanks.

    I noted that there is also error in WithPath using WildcardMatcher

  4. added a commit that references this issue on Aug 19, 2026
    2b165eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions