outofcoffee / imposter

Scriptable, multipurpose mock server. Run standalone mock servers, or embed mocks within your tests.
https://imposter.sh
Other
374 stars 61 forks source link

Cannot use YAML anchors and references in config files #646

Open birnbuazn opened 1 month ago

birnbuazn commented 1 month ago

Seems like you are using Jackson Dataformats YAML under the hood, which unfortunately doesn't handle anchors and references in YAML files nicely:

https://github.com/FasterXML/jackson-dataformats-text/issues/98

Which is a pain, because I want to reuse regex patterns in my config file, which currently does not work:

    requestBody:
      allOf:
        - xPath: "/soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp/wsu:Created"
          operator: Matches
          value: &Timestamp ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$
        - xPath: "/soap:Envelope/soap:Header/wsse:Security/wsu:Timestamp/wsu:Expires"
          operator: Matches
          value: *Timestamp
outofcoffee commented 1 month ago

Hi @birnbuazn, thanks for raising this.

One work-around could be to set them as an environment variable (or put them into a .env file), and then refer to them using the ${env.SOME_VAR} syntax.

We were considering having something like the components concept that OpenAPI specs use, for reusable elements in a config file. Would something like this work for you?

birnbuazn commented 1 month ago

Both would work in my case, I guess.

The workaround with the environment variables seems tempting, although it might obscure things a bit, because the regex pattern in my case would be defined outside the YAML file, which can make it easier to read, because you get a nice label for the pattern. On the other hand, it might be a little harder for people to reason about the spec. if some arbitrary environment variables are defined elsewhere.

A fully blown components concept would of course be helpful. Might be a bit too much, though for my use case. Would you consider referencing simple String values as well?