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

Parameter with required and example fails valid-example rule #243

Closed
wants to merge 29 commits into from

Conversation

lag-of-death
Copy link
Contributor

@lag-of-death lag-of-death commented Jun 3, 2019

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

#223

Does this PR introduce a breaking change?

No.

dependabot-preview bot and others added 11 commits May 23, 2019 22:20
Bumps [@stoplight/types](https://github.com/stoplightio/types) from 5.1.2 to 7.0.1.
- [Release notes](https://github.com/stoplightio/types/releases)
- [Commits](stoplightio/types@v5.1.2...v7.0.1)

Signed-off-by: dependabot[bot] <support@dependabot.com>
Bumps [yaml](https://github.com/eemeli/yaml) from 1.5.1 to 1.6.0.
- [Release notes](https://github.com/eemeli/yaml/releases)
- [Commits](eemeli/yaml@v1.5.1...v1.6.0)

Signed-off-by: dependabot[bot] <support@dependabot.com>
* fix: handle ajv resolving issues

* feat: include resolver errors
Bumps [tar](https://github.com/npm/node-tar) from 2.2.1 to 2.2.2. **This update includes security fixes.**
- [Release notes](https://github.com/npm/node-tar/releases)
- [Commits](isaacs/node-tar@v2.2.1...v2.2.2)
@lag-of-death lag-of-death changed the title [WIP] fix: do not warn about data/required when required is not an array [SO-246] fix: do not warn about data/required when required is not an array Jun 3, 2019
@lag-of-death lag-of-death changed the title [SO-246] fix: do not warn about data/required when required is not an array [SO-246] Spectral: Parameter with required and example fails valid-example rule Jun 3, 2019
@lag-of-death lag-of-death requested a review from philsturgeon June 3, 2019 14:59
@philsturgeon philsturgeon changed the title [SO-246] Spectral: Parameter with required and example fails valid-example rule Spectral: Parameter with required and example fails valid-example rule Jun 3, 2019
};

function withoutRequiredIfNotArray(schemaObject: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required or is this an older change that is left in here?

This should not need to be done. If required: true is there it means the rule is operating on a parameter object, which with the jsonpath rules you've set up, it shouldnt ever be.

The only way a schema object will have a required: true in it, is because the user has invalid API specifications, and that'll get caught earlier.

Copy link
Contributor Author

@lag-of-death lag-of-death Jun 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @phil :)
so oas3-schema rule lints a spec with:

     parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
            example: "2323"
          example: "2323"

as OK. So, I'm assuming this is valid OAS3. Maybe it isn't?
Now:

  1. valid-openapi-example will match schema and schema.example
  2. valid-example will match parameters[0] and parameters[0].example

The problem is with 2, there is required: true on the object, that is later passed to

if (!ajv.validate(schemaObj, targetVal) && ajv.errors) {
, and that's why I'm using withoutRequiredIfNotArray. Maybe oas3-schema should not lint this case as OK ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is entirely valid OAS3 yeah.

  1. valid-schema-example should match example against the schema object
  2. valid-openapi-example should match example against the current ($.) object

Copy link
Contributor Author

@lag-of-death lag-of-death Jun 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phil, done, withoutRequiredIfNotArray is removed now as well

@lag-of-death lag-of-death force-pushed the SO-246_fix_data/required_issue branch 2 times, most recently from c28f8bf to db3b0d9 Compare June 4, 2019 10:59
@lag-of-death lag-of-death force-pushed the SO-246_fix_data/required_issue branch from db3b0d9 to 8f45e90 Compare June 4, 2019 11:08
@philsturgeon
Copy link
Contributor

This is pretty close but OpenAPI v2 and v3 are tricky beasts. Let's do some TDD and I'll write up the tests for you to code into.

@@ -215,3 +216,95 @@ describe('valid-example', () => {
},
);
});

describe('valid-schema-example', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we've split this one rule into two but have left them both in the old file? The file names match the rules so we should have:

  • src/rulesets/oas3/tests/valid-openapi-example.ts
  • src/rulesets/oas3/tests/valid-schema-example.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not in src/rulesets/oas3/ruleset.json, then where I should put these 2 new rules? Thought oas3/ruleset.json is the place as the two rules belong to oas3 validators, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The valid-example.ts is when there was one rule. You are splitting it into two rules. So make two files, the ones above, and tadaaaa you’re done. No new ruleset, that’s a total misuse of ruleset a.

1 rule becomes 2 rules.

@@ -0,0 +1,17 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen a rule specific ruleset before. What is in the intention? Is this for testing?

Copy link
Contributor Author

@lag-of-death lag-of-death Jun 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to move out this rule from oas3 rules. My understanding is that spectral can also validate jsons that are not oas2/oas3. We could also move this rule to some other not oas2/oas3 ruleset, but haven't found one (is there one existing?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functions are generic and can be used anywhere. We have schemaPath as a generic function, and this is the basis of the valid example rules.

The rules are different for oas2 and oas3 because the specification structures are different.

So we reuse the schemaPath function, but do not reuse the valid example stuff because we cannot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that's why I have moved valid-example out of ruleset.json for oas3

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this new file.

Copy link
Contributor Author

@lag-of-death lag-of-death Jun 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philsturgeon, if I remove it, where do I put valid-example? I'm kinda confused. I still have to test valid-example rule..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philsturgeon, I can move already existing tests from src/rulesets/oas3/__tests__/valid-example.ts to src/rulesets/oas2/__tests__/valid-example.ts, since valid-example rule didn't change for oas2. So this way src/rulesets/oas3/__tests__/valid-example.ts and src/rulesets/oas3/valid-example-ruleset.json are removed. Would that be OK?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So after seeing the talk about how many different types of param/header/schema example there is, maybe you could use that to create some tests which highlight each type of parameter and property example we looked at, then make sure this change works with each of those?

The ruleset you added is not the way to go about DRYing things, although I do understand the urge. Mixing up OAS2 and OAS3 is also not the way to go, as the two specs are different.

Seeing as you have split the valid-example rule in two, and the describe tests split in two, I was expecting you to just split the valid-example file in two.

instead of having describe('valid-schema-example', () => { inside src/rulesets/oas3/__tests__/valid-example.ts the convention is to match the test file with whatever you are describing, so if you are gonna have two different rules have two different test files.

Is that all starting to make sense?

Chris Miaskowski and others added 4 commits June 7, 2019 10:59
- fix the example of importing rules
- add repl.it examples
- move programmatic api docs to separate file
- clean up the readme a bit and update outputs
@lag-of-death
Copy link
Contributor Author

Hi @phil. This is WIP, but I wanted to show you what's been done so far. Please mind that external examples (.externalValue) were not considered.

@@ -0,0 +1,37 @@
import * as AJV from 'ajv';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand what this file is, could you give it a quick explanation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. @philsturgeon, this file is used by valid-type-example rule:

"function": "exampleType"

This rule takes care of the cases where we have:

type: some_type
example: some example

So where the shape of an example is defined by .type, not by schema.

lag-of-death and others added 8 commits June 13, 2019 11:41
…es (#251)

* fix(pattern.ts): enable use of regex flags for pattern in ruleset files

#242

* Update src/functions/pattern.ts

Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com>

* Update src/functions/pattern.ts

Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com>

* test(pattern.test.ts): testing exception on invalid flag in string regex

#242

* test(pattern.ts): use a different assertion
@philsturgeon philsturgeon changed the title Spectral: Parameter with required and example fails valid-example rule Parameter with required and example fails valid-example rule Jul 16, 2019
@philsturgeon philsturgeon added this to the On Deck milestone Jul 17, 2019
@philsturgeon philsturgeon added the t/bug Something isn't working label Jul 17, 2019
@chris-miaskowski chris-miaskowski modified the milestones: On Deck, September '19 Aug 13, 2019
@philsturgeon
Copy link
Contributor

Work is now being done in #472

@philsturgeon philsturgeon deleted the SO-246_fix_data/required_issue branch August 23, 2019 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants