-
-
Notifications
You must be signed in to change notification settings - Fork 248
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
fix(oas): improve examples validation #1284
Conversation
|
||
function isObject(value: unknown): value is Dictionary<any> { | ||
// todo: expose a util, so it can be used everywhere | ||
return value !== null && typeof value === 'object'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@P0lip or maybe just use lodash isObjectLike
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I considered doing it, but hm, tree-shaking feels shaky 😆
Let me see how it impacts the size of the bundled fn.
It's the difference is negligible, I'll go for isObjectLike
These rules have always been hell, due to the incredibly confusing nature of all the different types of examples that can show up anywhere in OAS2, OAS3, and JSON Schema. It honestly might need custom functions. Using https://github.com/stoplightio/studio/issues/381 as a reference to all the bajillion different combinations of rule, can we think about how we'd implement what combination of examples to handle: Schema Objects (for OAS2, OAS3, JSON Schema) when there could be x-example or example or examples or x-examples depending on whatever type of thing is there, and the value is always a bare value, or an array of bare values. Then there's Media Types, Parameters, etc which are sometimes "Schema like" but not, or might actually have a "Schema" inside them... No one rule could do everything, and we keep getting bug reports when we miss a certain specific combination, so whatever we do needs to work for everything. P.S No need to validate externalValue. |
6a0ba75
to
a9658a6
Compare
05911df
to
97a6b0f
Compare
97a6b0f
to
3db38c2
Compare
|
||
const result = this.functions.schema.call( | ||
this, | ||
exampleValue.value, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to my other comment: schema examples
can be multiple: true
but do not have a value
key.
{
"title" : "Match anything",
"description" : "This is a schema that matches anything.",
"default" : "Default value",
"examples" : [
"Anything",
4035
]
}
@philsturgeon I added oas2-valid-example rule. |
Excellent! I'm going to try this out on https://github.com/philsturgeon/examples-example and see how far we get. I'd suggest we use these as a basis for test harness. Once we've got the ones which are meant to fail to actually fail, we can try them with $ref variants too. |
Amazing work here, running this branch on from https://github.com/philsturgeon/examples-examples (the oas3.yaml) I am seeing these errors:
The first 5 are in v5.5 but the last 4 are only in this branch. The errors are not wildly useful as they are, but they are catching onto the fact that there are problems in the examples.
It's also noticed some response examples using the "500":
description: Fail, because the example is nonsense
content:
application/json:
schema:
properties:
id:
type: integer
name:
type: string
completed:
type: boolean
completed_at:
type: string
format: date-time
required:
- id
- name
- completed
examples:
response:
value:
completed: Not a boolean
completed_at: Not a date So that's cool. It's still missing this:
I'll have a look at OAS2 too. |
For oas2.yaml in that repo I'm seeing these two errors:
That's two more than the latest release! Similarly to oas3 feedback, if a response example is an object that sits inside a schema, it doesnt seem to get picked up. This example should fail, but doesn't:
OAS2 "property examples" should also fail, but currently do not. These do work in OAS3.
In OAS2, parameter examples don't seem to be failing as I suppose we aren't looking for x-example (the only way to make an example for a parameter in OAS2).
|
@philsturgeon I think this should be ready for another round. |
a9fcc27
to
9063a7f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work, you nailed every single example in these documents.
I gave this a go on the GitHub OpenAPI in sample-specs, and I'm seeing this output:
They've actually got some errors in their doc, and I'll talk to them about that, but why is this first one double reporting for a single error? Is this a $ref issue?
@philsturgeon ah, seems like the same rule targets the same portion of the spec. |
@philsturgeon how do you feel about merging some of the rules? |
Signed-off-by: Jakub Rożek <[email protected]>
4609bf5
to
6d1fda3
Compare
Yeah I thought we could probably get away with some rule merges so long as we can get the right function options into place. I let seems like different targets will need different function options, but if you can get the tests to pass with the changed rules then... go ahead! Maybe we can do the minimal change now and merge the rules as a follow up in 6.0, like you said. |
I'd need to drop a rule or two and merge them into some existing one. |
* fix(oas): oas3-valid-...-example rules check examples as well * feat: specify fields * feat: oas2-valid-response-example rule * fix: tweak oas3-*-schema-example rules * fix: cover more oas2 examples * fix: merge rule Signed-off-by: Jakub Rożek <[email protected]>
Fixes #901
Checklist
Does this PR introduce a breaking change?