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

Landing remaining rules #99

Merged

Conversation

acunniffe
Copy link
Contributor

This PR has been a 2 week effort from @smizell to:

  • port API Next rules from spectral to readable code
  • port Json API rules from spectral to readable code
  • make Stoplight's OAS 3 rules work with optic-ci

Let us know how these look, specifically if the API Next and Json API rules feel more readable and maintainable (one of the goals).

Also noted and commented, the spectral compatibility feels like a big win here. If there are other rulesets you want in the future from the spectral community, they should work OOTB.

@acunniffe acunniffe requested a review from a team as a code owner December 1, 2021 22:31
@CLAassistant
Copy link

CLAassistant commented Dec 1, 2021

CLA assistant check
All committers have signed the CLA.

require("./rulesets/specification").rules,
);

snykRulesService.useSpectralRuleset({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How spectral rules can be added

Copy link
Contributor

@cmars cmars left a comment

Choose a reason for hiding this comment

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

First pass comments so far. Really excited to try this out. I believe there are some lint errors, yarn run format should fix them up.

@@ -212,7 +214,7 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
method: "",
},
},
};
} as any;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

@@ -366,7 +419,7 @@ export class SnykApiCheckDsl implements ApiCheckDsl {
method: "",
},
kind: "ContextRule",
},
} as any,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are custom rules that don't really come out of OpenAPI (in this example its rules about your Context)

We added typing in the library last week to make the OpenAPI traverser more reliable / type safe, but these changes did not fit in. We need to think about custom rule types and how we want them to interop going forward. Definitely temporary

const { expect } = require("chai");
import { expect } from 'chai';
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

Comment on lines 33 to 41
propertyFormat: ({ bodyProperties }: SnykApiCheckDsl) => {
bodyProperties.requirement.should(
"have a format when a string",
({ flatSchema }, context) => {
if (flatSchema.type !== "string") return;
if (!withinAttributes(context)) return;
expect(flatSchema.format).to.exist;
},
);
Copy link
Contributor

Choose a reason for hiding this comment

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

We found requiring format: to be problematic; some fields need to contain string content not expressed in the default OAS3 formats, and while we might invent custom formats for these, it'd be difficult to coordinate processing these custom formats consistently across multiple systems in different languages. So we removed the format: requirement on string properties in #89.

We feel that requiring example: and description: should be good enough for a great documentation experience.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, you're right this stuff is hard to enforce across languages.

Do you want us to comment this out, remove it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove it please.

[200, 201].includes(response.statusCode)
) {
const responseSchema =
specItem.content?.["application/vnd.api+json"]?.schema;
Copy link
Contributor

Choose a reason for hiding this comment

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

If the developer mis-types the content type, uses something else like application/json, is the rule error message meaningful enough to the developer, to understand what needs fixing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It made sense in our testing, but it's hard to say what the average developer will know about the guidelines. Do you have an idea what you'd want it to say?

Also we should go through and add a docsLink for each rule that we can so developers can click into sweater comb docs

Copy link
Contributor

Choose a reason for hiding this comment

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

It made sense in our testing, but it's hard to say what the average developer will know about the guidelines. Do you have an idea what you'd want it to say?

As I'm writing this, I realize this might be better expressed as a different rule (all 2xx responses except 204 need an application/vnd.api+json response), but seeing it referenced here made me wonder how it'd be handled.

Also we should go through and add a docsLink for each rule that we can so developers can click into sweater comb docs

Yes please, that would be great!

Copy link
Contributor

Choose a reason for hiding this comment

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

@acunniffe
Copy link
Contributor Author

First pass comments so far. Really excited to try this out. I believe there are some lint errors, yarn run format should fix them up.

Sorry about that. All our internal repos have single quote on, so when we copy code in and out it gets messed up. I realized we should be running format and will do so going forward!

@cmars
Copy link
Contributor

cmars commented Dec 2, 2021

I'd be OK with single quote (less typing!) -- as long as it's all consistently formatted. Though, I'd rather do that change in a separate PR, to keep the diffs readable.

@cmars cmars mentioned this pull request Dec 2, 2021
@cmars
Copy link
Contributor

cmars commented Dec 2, 2021

Just one more thing, can we update json-schema and jsonpointer to address these vulns?

Screen Shot 2021-12-02 at 10 50 40 AM

Screen Shot 2021-12-02 at 10 48 36 AM

@acunniffe
Copy link
Contributor Author

acunniffe commented Dec 2, 2021

These appear to be in stoplight/spectral-ruleset so I can't update them w/o forking and upstreaming. Looks like you use "^1.2.2" in main. That appears vulnerable too. How should we proceed?

@cmars
Copy link
Contributor

cmars commented Dec 2, 2021

Thanks for checking! We'll have to ignore them for now and revisit later. For a CI tool, the risk is low, but a service that uses this may need more careful consideration down the road.

@cmars cmars merged commit b32b614 into snyk:optic-main Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants