spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Error when handling formats if json rulesets are extended

Open jnsppp opened this issue 3 years ago • 2 comments

Error when handling formats if json rulesets are extended

When using extended json rulesets, spectral is running into a type error. Running rulesets without the extends keyword individually works perfectly fine. This is a bug report for json rulesets only. YAML rulesets are working as expected.

To Reproduce

We are running spectral from nodejs as described in here: https://meta.stoplight.io/docs/spectral/eb68e7afd463e-spectral-in-java-script#load-rulesets-and-api-specification-files.

export const runSpectral = async (spec: any, ruleSetPath: string, ruleSetName: string): Promise<LintingResult> => {
    let result = LintingResult.OK;
    const contract = new Document(JSON.stringify(spec), parsers.Json);
    const spectral = new Spectral();
    const ruleset = await bundleAndLoadRuleset(path.join(__dirname, ruleSetPath), { fs, fetch });
    spectral.setRuleset(ruleset as Ruleset);

    await spectral.run(contract).then(results => {
        result = checkResults(results);
        if (result === LintingResult.ERROR) {
            console.log(`${ruleSetName} linting not successful\n`);
        } else if (result === LintingResult.WARNING) {
            console.log(`${ruleSetName} linting identified some warning. Please check manually\n`);
        } else {
            console.log(`${ruleSetName} linting successful\n`);
        }
    });
    return result;
};

When running rulesets individually, spectral runs and return the expected results. If extending a rulesets with a custom ruleset, e.g.:

oas-spectral.json

{
    "extends": ["spectral:oas"],
    "rules": {
        "operation-tag-defined": "off",
        "info-contact": "off",
        "oas3-unused-component": "info",
        "oas3-valid-media-example": "off",
        "openapi-tags": "off"
    }
}

and mqc-spectral.json (this is the one, that is failing ❗️❗️❗️)

{
   "extends": [
      "./oas-spectral.json"
   ],
    "rules": {
       ...
    }
}

we are running into the following error:

spectral-type-error

Expected behavior and how to fix

When running spectral from JavaScript, it should be possible to use the extends keyword in custom json rulesets.

I invested some time to go through your code and I think I found the lines that are not correct. When changing https://github.com/stoplightio/spectral/blob/develop/packages/core/src/ruleset/rule.ts#L163-L167 to the following:

       for (const format of this.formats) {
            if (formats.has(format)) {
                return true;
            }
        }

everything works as expected. (Changed the usage of this keyword) 👍🏻

Screenshots

Screenshot 2022-12-03 at 11 16 03 Screenshot 2022-12-03 at 11 18 00

Environment:

  • Library versions:

    "@stoplight/spectral-core": "^1.15.2", "@stoplight/spectral-parsers": "^1.0.2", "@stoplight/spectral-ruleset-bundler": "^1.5.0", "@stoplight/spectral-runtime": "^1.1.2",

  • OS: macOS 12.2.1

  • Node version: any

If you need further information, please let me know. 🚀

jnsppp avatar Dec 05 '22 16:12 jnsppp

hey, is there a plan to release a fix?

riccardo-angelilli avatar Jul 03 '24 13:07 riccardo-angelilli

I've created PR https://github.com/stoplightio/spectral/pull/2654

riccardo-angelilli avatar Jul 05 '24 06:07 riccardo-angelilli

:tada: This issue has been resolved in version 1.19.1 :tada:

The release is available on @stoplight/spectral-core-1.19.1

Your semantic-release bot :package::rocket:

stoplight-bot avatar Sep 13 '24 10:09 stoplight-bot