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

Customizing One Resolver Removes Other Default Resolvers #1315

Closed
dillonredding opened this issue Aug 26, 2020 · 2 comments
Closed

Customizing One Resolver Removes Other Default Resolvers #1315

dillonredding opened this issue Aug 26, 2020 · 2 comments
Labels
json-refs everything JSON Refs resolving related t/bug Something isn't working

Comments

@dillonredding
Copy link

Describe the bug
In an attempt to work around #1314, I found that when overriding only the file $ref resolver, the default http and https $ref resolvers are not used and are replaced with the provided file $ref resolver.

To Reproduce

C:/api/openapi.yaml:

openapi: 3.0.3
info:
  title: Test API
  version: 1.0.0
paths:
  /test:
    get:
      responses:
        '200':
          $ref: components.yaml#/Ok
        '400':
          $ref: https://gist.githubusercontent.com/dillonredding/4b3792af17a7335bca325b64cbf74e74/raw/4e450ed7b12c87f99245ee0bf20db08497130508/openapi-components.yaml#/BadRequest

C:/api/components.yaml:

Ok:
  description: Success

C:/dev/script.ts:

import fs from 'fs';
import YAML from 'js-yaml';
import path from 'path';
import { Resolver } from '@stoplight/json-ref-resolver';
import { isOpenApiv3, Spectral } from '@stoplight/spectral';
import { DiagnosticSeverity } from '@stoplight/types';

const openApi = fs.readFileSync('C:/api/openapi.yaml', 'utf-8');

const resolver = new Resolver({
    resolvers: {
        file: {
            resolve: ref =>
                fs.promises.readFile(path.join('C:/api', ref.path()), 'utf-8')
        }
    }
});

const spectral = new Spectral({ resolver });
spectral.registerFormat('oas3', isOpenApiv3);
spectral.run(YAML.safeLoad(openApi) as string)
    .then(results => {
        results.filter(result => result.severity === DiagnosticSeverity.Error)
            .forEach(result => console.log(result.message))
    });
$ ts-node C:\dev\script.ts
ENOENT: no such file or directory, open 'C:\api\dillonredding\4b3792af17a7335bca325b64cbf74e74\raw\4e450ed7b12c87f99245ee0bf20db08497130508\openapi-components.yaml'

Expected behavior
According to the documentation:

If you'd like [to ...] adjust the resolution, you are absolutely fine to do it. In order to achieve that, you need to create a custom json-ref-resolver instance. [Emphasis mine]

I interpret this as being able to customize specific resolvers. Customizing the file $ref resolver shouldn't require reimplementing the others.

Environment:

  • TypeScript: v3.9.6
  • Spectral: v5.5.0
  • Node: v12.15.0
  • Windows: 10 Enterprise
@dillonredding dillonredding added the t/bug Something isn't working label Aug 26, 2020
@nulltoken
Copy link
Contributor

@dillonredding Sadly, it's possible the documentation is indeed misleading on that topic. Thanks for pointing that out.

There would be two ways addressing that:

@philsturgeon
Copy link
Contributor

Tweak the code in https://github.com/stoplightio/spectral/blob/c021b045e9c8c76bb3ee4dc624edc7779c05b369/src/resolvers/http-and-file.ts so that's easier to potentially inject overriden resolvers per scheme

I think this will be the way to go, as we're already planning to make resolvers more easily injectable for #1054.

@P0lip P0lip added the json-refs everything JSON Refs resolving related label Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
json-refs everything JSON Refs resolving related t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants