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

Feature request: Select from multiple examples by name #30

Open
mkq opened this issue Aug 18, 2021 · 4 comments · Fixed by #39
Open

Feature request: Select from multiple examples by name #30

mkq opened this issue Aug 18, 2021 · 4 comments · Fixed by #39
Assignees
Labels
enhancement New feature or request

Comments

@mkq
Copy link

mkq commented Aug 18, 2021

As per README.md,

If multiple examples for the response object are specified, the first one will be used as a resulting sever response.

I would like to select the example by name, on a per request basis. For example, I might have examples named "myResourceFlat" and "myResourceWithSubresources" in the OpenAPI spec, and want to return "myResourceWithSubresources" if query parameter "expand=subresources" is present, else "myResourceFlat".

Could you please add support for this? Maybe something like

createMockMiddleware(
    ...,
    exampleSelector: (req, defaultExampleSelector) => {
        // const defaultExample = defaultExampleSelector(req) //? if needed
        return req.query?.expand === 'subresources' ? 'myResourceWithSubresources' : 'myResourceFlat'
    }
}

(Maybe I'm missing something which can already be done with a jsfCallback, as I'm new to openapi-mock-express-middleware and know even less about json-schema-faker.)

Thanks!

@aleksandryackovlev
Copy link
Owner

Sorry for the late reply.

For now I can't come up with a fast solution for this use case. It's because the config for json-schema-faker, where we handle usage of examples, is created not at the time of request. Hence, there is no access to the req object. See https://github.com/aleksandryackovlev/openapi-mock-express-middleware/blob/master/src/utils/generator.ts#L14

@mkq
Copy link
Author

mkq commented Sep 3, 2021

Thanks for the reply. Looking forward to the not-so-fast solution then, maybe… :-)

Or does it make more sense to open a json-schema-faker feature request?

@aleksandryackovlev
Copy link
Owner

It doesn't have anything to do with json-schema-faker. This issue is about how openapi-mock-express-middleware is made.
I didn't think about such a use case at the time of its creation.

Anyway, the ability to select a response for a particular request could definitely be useful.

I'm gonna think how to implement it.

@aleksandryackovlev aleksandryackovlev self-assigned this Sep 6, 2021
@aleksandryackovlev aleksandryackovlev added the enhancement New feature or request label Sep 6, 2021
@aleksandryackovlev aleksandryackovlev linked a pull request Jan 31, 2022 that will close this issue
@aleksandryackovlev
Copy link
Owner

aleksandryackovlev commented Jan 31, 2022

The feature has been implemented partially. Now it's possible to add custom logic for the generation of responses from the examples attributes. See https://github.com/aleksandryackovlev/openapi-mock-express-middleware#responses-generated-from-examples.

Yet the request object is still not available for the configure method. I've added this feature to the roadmap for the next major version.

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

Successfully merging a pull request may close this issue.

2 participants