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

spectral doesn't work with a schema containing the $ref field #2093

Closed
KhudaDad414 opened this issue Mar 16, 2022 · 3 comments
Closed

spectral doesn't work with a schema containing the $ref field #2093

KhudaDad414 opened this issue Mar 16, 2022 · 3 comments

Comments

@KhudaDad414
Copy link

Describe the bug
Spectral doesn't provide any output if the schema contains a $ref field anywhere. It works fine if I don't have any $ref fields in my schema.

To Reproduce

run the following code in a node app. or use cli. I have tested it in both ways and the results are the same.

import pkg from '@stoplight/spectral-core';
const { Spectral, Document } = pkg;
import { Yaml } from '@stoplight/spectral-parsers';
import { schema } from '@stoplight/spectral-functions';
const myDocument = new Document(`typee: request`, Yaml, '/my-file');

const spectral = new Spectral();
spectral.setRuleset({
  rules: {
    'my-rle-name': {
      description: 'Does not validates.',
      severity: 'error',
      given: '$',
      then: {
        function: schema,
        functionOptions: {
          schema: {
            title: 'Operation Schema',
            type: 'object',
            additionalProperties: false,
            patternProperties: {
              '^x-[\\w\\d\\.\\-\\_]+$': {
                $ref: 'https://raw.githubusercontent.com/asyncapi/asyncapi-node/v2.7.7/schemas/2.0.0.json#/definitions/specificationExtension',
              },
            },
            properties: {
              type: {
                type: 'string',
                enum: ['request', 'response'],
              },
            },
          },
        },
      },
    },
  },
});
spectral.run(myDocument).then(console.log);

Expected output

[
  {
    code: 'my-rle-name',
    message: 'Does not validates.',
    path: [],
    severity: 0,
    source: '/my-file',
    range: { start: [Object], end: [Object] }
  }
]

Actual output

[]

Environment:

  • Library version: @stoplight/spectral-core 1.11.0
  • OS: Windows 11
@P0lip
Copy link
Contributor

P0lip commented Mar 16, 2022

I'd say this is works as expected. As things stand We don't support $ref anywhere in the ruleset.
I believe there's some asyncapi schema npm package, so perhaps you could use it instead?

@KhudaDad414
Copy link
Author

@P0lip thanks for the comment. that makes sense. but wouldn't it be better to have some kind of log saying we don't support $refs in schema function when someone passes $ref to the schema function? saying this because it took me more time than what I would like to admit to figure out why my input isn't being processed 😄

@sgatade
Copy link

sgatade commented Nov 14, 2023

I dont think this is only an issue with schema function. Below is my piece of ruleset:

functions:
  - test-context

rules:
  ref-has-no-sibling:
    description: Ref tag has no sibling
    severity: warn
    formats:
      - oas2
      - oas3
    given: $..[?(@property === "$ref")]
    then:
      function: test-context

The custom function test context is only printing the input it receives (for now). But when I run Swagger PetStore OAS 3 Spec against this rule, it does not print any $ref nodes!

NOTE: I added a extra sibling next to a $ref node in my local Pet Store OAS Spec

jsonpath.com with the same JSON Path gives me all the ref nodes properly from the same OAS spec.

[
  "#/components/schemas/Pet",
  "#/components/schemas/Error",
  "#/components/schemas/NewPet",
  "#/components/schemas/Pet",
  "#/components/schemas/Error",
  "#/components/schemas/Pet",
  "#/components/schemas/Error",
  "#/components/schemas/Error",
  "#/components/schemas/NewPet"
]

Spectrals own source code at Spectral Ruleset Github has the below code which uses the same piece of JSON path.

    'no-$ref-siblings': {
      formats: [oas2, oas3_0],
      description: 'Property must not be placed among $ref',
      message: '{{error}}',
      severity: 0,
      recommended: true,
      resolved: false,
      given: "$..[?(@property === '$ref')]",
      then: {
        function: refSiblings,
      },
    },

When I execute spectral by extending spectral:oas, I do get a nice little message for the $ref issue below.

 154:54    error  no-$ref-siblings              $ref must not be placed next to any other properties  paths./pets/{id}.get.responses[200].content.application/json.schema.somethingelse

Why is $ref working in spectral:oas but not in a custom rule?

Thanks and regards,

SG

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

No branches or pull requests

3 participants