Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to map requests to the openapi examples #2673

Open
jbadeau opened this issue Mar 9, 2025 · 2 comments
Open

How to map requests to the openapi examples #2673

jbadeau opened this issue Mar 9, 2025 · 2 comments

Comments

@jbadeau
Copy link

jbadeau commented Mar 9, 2025

One can leverage WireMock's robust proxy stub mappings features to proxy requests to specific Prism mocks based on the request details.

Wiremock proxy stub mapping

{
  "request": {
    "method": "GET",
    "urlPattern": "/scorecards/default/application/techradar"
  },
  "response": {
    "proxyBaseUrl": "http://127.0.0.1:4010",
    "additionalProxyRequestHeaders": {
      "Prefer": "techradar"
    }
  }
}

In this example, WireMock acts as a simple proxy. It forwards a request for a scorecard (with namespace: default, kind: application, name: techradar) to Prism and adds the Prefer header to techradar, which corresponds to the example name.

OpenApi 3.x response examples

  /scorecards/{namespace}/{kind}/{name}:
    get:
      summary: Retrieve scorecard for a specific entity
      description: Get the scorecard for a specific entity identified by its kind, namespace, and name triple.
      operationId: getScorecard
      parameters:
        - name: namespace
          in: path
          description: Namespace of the entity
          required: true
          schema:
            type: string
            default: default
            examples:
              - default
        - name: kind
          in: path
          description: Type of the entity
          required: true
          schema:
            type: string
            examples:
              - application
        - name: name
          in: path
          description: Name of the entity
          required: true
          schema:
            type: string
            examples:
              -
      responses:
        "200":
          description: Scorecard for the requested entityRef
          content:
            application/hal+json:
              schema:
                $ref: "#/components/schemas/Scorecard"
              examples:
                sda:
                  $ref: "./examples/getScorecard.sda.json"
                techradar:
                  $ref: "./examples/getScorecard.techradar.json"

More complex mappings are of course easily supported. For example, utilizing Wiremock’s support for Handlebars one can:

{
  "request": {
    "method": "GET",
    "urlPattern": "/scorecards/default/application/([^/]+)"
  },
  "response": {
    "proxyBaseUrl": "http://127.0.0.1:4010",
    "additionalProxyRequestHeaders": {
      "Prefer": "{{request.pathSegments.[4]}}"
    },
    "transformers": ["response-template"]
  }
}

Originally posted by @jbadeau in #1838

@jbadeau jbadeau changed the title How to map requests to the examples How to map requests to the openapi examples Mar 9, 2025
@jbadeau
Copy link
Author

jbadeau commented Mar 9, 2025

I understand using a competitor like Wiremock might not be ideal but it works well and is relatively easy to do. Perhaps some server middleware is required to make it all easier and more natural.

@alexplayer15
Copy link

Hi, we are currently using Prism to run contract tests and it would be great to have a feature where we can auto populate requests based off example values in our OAS.

If I've understood correctly WireMock is currently a workaround for this and not the ideal solution? I am happy to try and add native support for auto-populating request values inside Prism.

Would you want it to look something like this?:

You can run Prism CLI:
prism proxy --auto-populate-request (some new flag in the CLI)

And then when a request is sent to Prism, if it has any missing values in the header/body they are populated based on the examples in the Spec?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants