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

Error when handling formats if json rulesets are extended #2362

Closed
jnsppp opened this issue Dec 5, 2022 · 3 comments · Fixed by #2664
Closed

Error when handling formats if json rulesets are extended #2362

jnsppp opened this issue Dec 5, 2022 · 3 comments · Fixed by #2664
Labels
c/spectral p/medium released reviewed-medium Medium bug was reviewed & kept as medium t/bug Something isn't working triaged

Comments

@jnsppp
Copy link

jnsppp commented Dec 5, 2022

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. 🚀

@mnaumanali94 mnaumanali94 added t/bug Something isn't working p/medium labels Jan 26, 2023
@matty-v matty-v added the reviewed-medium Medium bug was reviewed & kept as medium label Mar 29, 2023
@riccardo-angelilli
Copy link
Contributor

hey, is there a plan to release a fix?

@riccardo-angelilli
Copy link
Contributor

riccardo-angelilli commented Jul 5, 2024

I've created PR #2654

@stoplight-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 1.19.1 🎉

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

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/spectral p/medium released reviewed-medium Medium bug was reviewed & kept as medium t/bug Something isn't working triaged
Projects
None yet
6 participants