-
Notifications
You must be signed in to change notification settings - Fork 371
Get mock values from schema.example
(if no default
value)
#304
base: master
Are you sure you want to change the base?
Conversation
In many cases you don't want to define a `default` value for a schema object, but you still want to provide representative examples, particularly in mock responses. This is particularly important where `string` type uses a `pattern` that doesn't match the hard-coded mock values, which in turns causes the mocked response to fail the response validator. This change allows mock values to be pulled from the `example` field to solve this problem. `example` is inserted between `default` and `enum`, leaving the the mock value source precedence as: 1. `1.2` `defaultValue` 2. `2.0` `default` 3. `2.0` `example` 4. `enum` (first value) 5. hardcoded value (or *now* for date/date-time) Test plan: * Added unit tests to cover the appropriate selection of `default` and `example` as the mock value for string, number, integer, and boolean types. * Used gulp to run lint and all unit tests and ensured everything passes. * Used coverage report to verify all new lines were tested at least once.
The previous code always generated 1 item in the array. If the property has a minItems value that is >1 then the mock will fail response validation. This change fixes it by generating the minimum number of items in the array to match minItems. If minItems is not defined, then it will continue to generate 1 item as before. Test plan: * Added unit tests to cover minItems set and not set * Used gulp to run lint and all unit tests and ensured everything passes. * Used coverage report to verify all new lines were tested at least once.
I've update this pull request to include generating mock |
Sorry for the delayed response. Mock values use to work this way but examples and mocks are not the same thing. Since the example values are just text and do not pass through validation, just blindly sending an example as a mock does not make as much sense to me. I'd love to hear what @webron thinks. |
@whitlockjc if I understand correctly, this is the |
@webron yes, this is the @whitlockjc do you mean schema validation on init, or response validation by the I think this is ok because the swagger spec says (my emphasis):
Thus it appears that is supposed to be an example of an instance (rather than a general explanatory field), and thus should comply with the format of that schema. That's my interpretation though, and the spec doesn't use MUST or SHOULD (or MUST NOT for that matter), so it doesn't seem 100% guaranteed. Let me know your thoughts and, as mentioned above, I could change to use something like |
Any updates? I'm looking for this for a while! @farrago thanks for your work, really helps! 👍 |
@imZack, |
@farrago thanks for your fast replying, I have noticed that they are built by recurring too. In my case, I want to use fixed mock data just like apiblueprint's response section and I have tried modified Anyway, if there is a better way to do some kind of fixed mock data way please let me know. Thanks! |
+1 this would be really helpful for us too! |
+1 this is a very nice feature to have for us too. One suggestion however, the example value should take precedence over default values. Meaning if the schema includes valid values for both example and default, the example value should be displayed. The current PR prefers default value over the example set. |
@shri046, I preferred default over example so that the behaviour would remain the same for anyone who already had both default and example in their definitions, and was thus getting the default as their mock data. Changing the order of precedence would be a breaking change for those users, and would need more convincing. |
any movement on this? |
I totally agree with @shri046. default should be used when nothing else is provided, thus example must have precedence over default. In the meantime, is this PR scheduled to be merge any time soon ? |
You may not want to define a
default
value for a schema object, but still want to provide representative examples, particularly in mock responses.This is particularly important where
string
type uses apattern
that doesn't match the hard-coded mock values, which in turns causes the mocked response to fail the response validator. This change allows mock values to be pulled from theexample
field to solve this problem.example
is inserted betweendefault
andenum
, leaving the the mock value source precedence as:1.2
defaultValue
2.0
default
2.0
example
enum
(first value)Test plan:
default
andexample
as the mock value for string, number, integer, and boolean types.Example use case:
Schema is
Before this change, the mock response would be
"Sample Text"
which would then fail the response validator as it has a space (which isn't allowed according to thepattern
).After the change, the mock response would be
"SomeTextWithOnlyAsciiLetters"
, which is a valid response.Caveats
example
fields higher up the chain (e.g.object
types, or wholeresponses
). This would require more extensive rework of how mock values are created.example
value. This was suggested as a requirement in Finish first pass at enhanced swagger-router mock mode #30 by @whitlockjc. My assumption in this change is that examples are valid values or can be made so.Alternative options
If this is not a good use of the
example
field, an alternative, and equally simple, option might be to us a customx-mock-value
parameter (or similar) to get the value from.