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

feat(core): support scoped aliases #1840

Merged
merged 8 commits into from
Oct 11, 2021
Merged

Conversation

P0lip
Copy link
Contributor

@P0lip P0lip commented Sep 20, 2021

Closes #1837

Needs #1838 #1839

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

@P0lip P0lip added the enhancement New feature or request label Sep 20, 2021
@P0lip P0lip self-assigned this Sep 20, 2021
@P0lip P0lip force-pushed the feat/core/support-scoped-aliases branch 3 times, most recently from c2f3330 to bc92140 Compare September 27, 2021 07:37
@P0lip P0lip changed the base branch from develop to chore/core/scoped-aliases-validation September 27, 2021 14:37
Base automatically changed from chore/core/scoped-aliases-validation to develop October 2, 2021 10:21
@P0lip P0lip force-pushed the feat/core/support-scoped-aliases branch from bc92140 to d5353da Compare October 4, 2021 07:33
@P0lip P0lip requested a review from a team October 4, 2021 07:34
@P0lip P0lip marked this pull request as ready for review October 4, 2021 07:34
@P0lip P0lip enabled auto-merge (squash) October 5, 2021 20:56
Copy link
Contributor

@marbemac marbemac left a comment

Choose a reason for hiding this comment

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

Is there a separate issue to update the docs?

Not strictly related to this PR, but do we have sense of perf impact of using aliases versus not using aliases?

{ targets }: RulesetScopedAliasDefinition,
formats: Set<Format> | null,
): string {
const errorMessage = `Alias "${name}" is applicable to certain formats, but the format of the linted document is not matched`;
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this manifest itself to end user, and what behavior does it cause? If there's a test around this behavior lmk and that might tell me, just couldn't find it :).

If there is a rule that uses an alias that isn't applicable to the document being linted, will this cause the lint to not go through and/or error?

Copy link
Contributor Author

@P0lip P0lip Oct 6, 2021

Choose a reason for hiding this comment

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

image
given the following document

swagger: 2.0
definitions:
  User:
    type: woops

and the following ruleset

formats:
  - oas2
  - oas3
aliases:
  SchemaObject:
    targets:
      - formats:
          - oas3
        given: $.components.schemas[*]
rules:
  valid-type-schema:
    given: "#SchemaObject"
    then:
      field: type
      function: enumeration
      functionOptions:
        values:
          - string
          - boolean
          - number
          - null
          - object
          - array

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't quite tell from the screenshot - did that result in the lint failing, or is it just a log to stdout?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marbemac it fails, I updated the screenshot.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm - do we want that? makes it impossible to write some rules that target oas2 and some that target oas3 etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But we don't inform the user about a potentially broken json path

We don't, but it's more apparent than it is with aliases.

I agree with the "pointer to a json path" bit.

I mean, I'm certainly not against making it more lenient - just need to evaluate how this would feel.
Let's make Nauman decide.
Getting rid of that throw would be very easy, so let's not take the implementation factor into the account when making the choice.
Both options are easy to implement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After some thinking, I actually believe that the case we deal with here might closer to to an empty given:

given: []

Copy link
Contributor

Choose a reason for hiding this comment

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

I think letting the file lint is the right thing to do. We should have some sort of warning though that a particular rule wasn't run as folks could assume that the file was linted against that rule. Maybe that as a message/info is the right way to go about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

That could be a separate thing though. Doesn't need to happen in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough.
I'd still argue we should throw, but let it be. It doesn't change much from the perspective of code.

@P0lip
Copy link
Contributor Author

P0lip commented Oct 6, 2021

Is there a separate issue to update the docs?

Yeah, there's some in platform-internal https://github.com/stoplightio/platform-internal/issues/8141

Not strictly related to this PR, but do we have sense of perf impact of using aliases versus not using aliases?

That's actually a difficult question. I'd say it depends.
From the technical standpoint, there isn't any performance hit, because we simply unfold these aliases to regular JSONPath expressions, so the only cost we take is that "resolving" logic that I added in this PR and it's a totally negligible cost.
On the other hand, aliases may make it more tempting to write more complex JSONPath expressions, which, at times, may in fact result in a degredaded performance.
It all boils to down the paths you supply Nimma with - while it covers pretty much all common cases, there are certain scenarios it handles worse.
These cases are shown here.
Overall, such usage is very rare, and I doubt this will change with aliases, but it may potentially happen.

That's why I'm thinking about exposing some internal logic I use in that library so we can potentially use it in Spectral/Studio, and warn about inefficient/bad JSONPath expressions.
These poor JSONPath expressions are rather not intended anyway, because they are very likely to match properties you don't want to target.

@P0lip P0lip requested a review from marbemac October 6, 2021 14:40
@P0lip P0lip merged commit b278497 into develop Oct 11, 2021
@P0lip P0lip deleted the feat/core/support-scoped-aliases branch October 11, 2021 21:00
stoplight-bot pushed a commit that referenced this pull request Oct 12, 2021
# [@stoplight/spectral-core-v1.6.0](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-core-v1.5.1...@stoplight/spectral-core-v1.6.0) (2021-10-12)

### Features

* **core:** support scoped aliases ([#1840](#1840)) ([b278497](b278497))
@stoplight-bot
Copy link
Collaborator

🎉 This PR is included in version @stoplight/spectral-core-v1.6.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change Aliases to accept targets for multiple formats and description
4 participants