-
Notifications
You must be signed in to change notification settings - Fork 21
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
Handle examples
per property for array mocking (using mixing or index)
#951
Comments
An example, which I know is different to yours, that works is: components: {}
info:
title: issue-951-mock
version: 0.1.0
openapi: 3.0.0
paths:
/hello:
get:
responses:
"200":
content:
application/json:
examples:
2:
value:
message: Hello from a mocked response 2!
1:
value:
message: Hello from a mocked response 1!
schema:
properties:
message:
type: string
type: object
description: A simple hello world!
/validated:
post:
requestBody:
content:
application/json:
schema:
properties:
name:
type: string
required:
- name
type: object
responses:
"200":
content:
text/plain:
example: Hello mocked Kusk!
schema:
type: string
description: ""
x-kusk:
cors:
methods:
- GET
- POST
origins:
- "*"
mocking:
enabled: true
public_api_path: "simple/spec.json" kusk mock -i ./issue-951-mock.yaml $ bash -c 'while [[ true ]]; do curl "http://localhost:8080/hello"; echo; done'
{"message":"Hello from a mocked response 1!"}
{"message":"Hello from a mocked response 1!"}
{"message":"Hello from a mocked response 1!"}
{"message":"Hello from a mocked response 1!"}
{"message":"Hello from a mocked response 1!"}
{"message":"Hello from a mocked response 1!"}
{"message":"Hello from a mocked response 2!"} NB: That it returns The way I specified Can you point me to the part of the specification that shows that the example you shared is valid? https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v3.1/schema.yaml#L168 - The one you shared uses an
So: components:
schemas:
GetOrganizationResult:
type: object
properties:
id:
type: number
examples:
- 1
- 2
- 3
example: 1
name:
type: string
examples:
- Syneki
- KMP Agency
- Oyez
example: Syneki
domain:
type: string
examples:
- syneki.com
- kmp.agency
- oyez.fr
example: Syneki
required:
- id
- name
- domain Should not be supported because |
@kerwanp It seems that what you reported isn't defined by OpenAPI specs. |
Describe the enhancement you'd like to see
Currently when mocking a path returning a schema with properties that have
examples
it only uses theexample
.When returning an array of this schema it also uses the
example
.The enhancement would be to use the
examples
to provide better mocked data for paths returning arrays.Here are two ways of providing mocked data using the
examples
of each property of the schema.examples
Examples
If I have the following schema:
Before enhancement
Currently it always uses the
example
of each property, even when returning a list of the schema.After enhancement
By doing that it becomes also extremely easy to mock really complex schemas with nested objects.
A bit of context
I wanted to add the reason why I find this enhancement extremely interesting.
Currently it is still complex to develop locally on a project using the API Gateway as it has to run on Kubernetes.
If we do an update we have to deploy the app on a Cluster, the OpenAPI definition and then test properly.
Even by using tools to make this process extremely easy, it takes time.
That's why I think that the mocking feature is an important part.
In my case I automatically generate the OpenAPI definition file for each service using Swagger and more specifically
@nestjs/swagger
.The example of above has been generated with the following code:
With few lines of code I can:
And soon I will implement https://www.npmjs.com/package/class-validator to also generate the validation part.
The text was updated successfully, but these errors were encountered: