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

When loading expectations from OpenAPI spec, for every operation mockserver only creates 1 expectation, even if there are different responses/code configured #1806

Open
kapoorrohit opened this issue Oct 14, 2023 · 3 comments

Comments

@kapoorrohit
Copy link

Describe the issue
Open API spec has responses like 200, 401, 500. But when I create expectations from this Open API, it only returns first response for every operation.

MockServer version
5.15.0

  1. How you are running MockServer (i.e maven plugin, docker, etc) -> Java Client
  2. Code you used to create expectations -> Expectation[] test = mockServer.upsert(
    openAPIExpectation(
    "file:/Users/..../openapi.yaml"
    )
    );
  3. What error you saw -> Only first response returned for each operation

Expected behaviour
All responses to be returned

@sann3
Copy link

sann3 commented Feb 19, 2024

Any update on this ?

@IvanZhivkov-sumup
Copy link

IvanZhivkov-sumup commented May 13, 2024

A single request can return only one response. So All response to be returned is not applicable.
The mock server documentation states:
if multiple response bodies are specified for an operation (i.e. different status codes) the first response body is used by default, however this can be controlled by using operationsAndResponses - link

Using operationsAndResponses you can create an expectation to return a specific response. This way you could create multiple expectations each with a different response. However each request will still get only one response, so you still have to figure out what you want to achieve.

@aswini1988
Copy link

I have a similar scenario where I use the OpenAPI Specification to create mocks.
https://www.mock-server.com/mock_server/using_openapi.html.
Using the operationsAndResponses argument I am creating two mocks for the same operation ID but for different status codes for example operation-id is getPetByID and the status code is 200 & 400. But when I hit the endpoint to get the response from Mockserver for the 200 status code it returns a response either from 400 or 200 which is matched first.
My question is how should I create mock data for these two status codes for a single operation ID so that Mockserver can return response 200 when the request is valid and 400 when the request is invalid.

var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).openAPIExpectation({
"specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
"operationsAndResponses": {
"showPetById": "200"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);

mockServerClient("localhost", 1080).openAPIExpectation({
"specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
"operationsAndResponses": {
"showPetById": "400"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);

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

4 participants