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

No such file or directory: '\\\\\\schemas.json' when using local reference #1861

Open
maddin1991 opened this issue Feb 3, 2024 · 7 comments

Comments

@maddin1991
Copy link

maddin1991 commented Feb 3, 2024

Description

I'm getting this error when i use local references to a spec file in the same folder:

_RefResolutionError: [Errno 2] No such file or directory: '\\\\\\schemas.json'

My OS is windows10/11. I did not test it on linux.

Steps to reproduce

This is my specification:

openapi: 3.0.3
info:
  title: Api
  version: 1.0.11
  description: description
paths:
  /test:
    get:
      operationId: get.test
      parameters:
        - $ref: schemas.json#/parameters/UserId
      responses:
        '200':
          description: ok

And this is my schemas.json file:

parameters:
  UserId:
    name: userId
    in: query
    required: true
    schema:
      type: string
      format: uuid

Additional info:

Output of the commands:

  • Python 3.11.7
  • connexion 3.0.5
@eharvey71
Copy link

eharvey71 commented Feb 13, 2024

I'm having the same issue after breaking out my schemas, parameters, and paths into their own subdirectories in an effort to organize a large yaml. I discovered that this is working fine in connexion 3.0.2.

If I downgrade to connexion 3.0.2, all is well. Looks like 3.0.3 and above include the breaking change.

@RobbeSneyders
Copy link
Member

@eharvey71 are you on Windows as well?

If I downgrade to connexion 3.0.2, all is well. Looks like 3.0.3 and above include the breaking change.

I don't immediately see any relevant changes in 3.0.3

@eharvey71
Copy link

eharvey71 commented Feb 13, 2024 via email

@maddin1991
Copy link
Author

Maybe there is something broken with the jsonschema library. This worked for me as a temporary fix:

from pathlib import Path
import yaml
import connexion.json_schema
from connexion.json_schema import ExtendedSafeLoader

class CustomFileHandler:
    """Handler to resolve file refs."""
    def __call__(self, uri):
        path = Path("your_corret_path_to_spec").resolve()
        with open(path) as fh:
            return yaml.load(fh, ExtendedSafeLoader)


connexion.json_schema.handlers.update(
    {
        "file": CustomFileHandler(),
        "": CustomFileHandler(),
    }
)

@eharvey71
Copy link

eharvey71 commented Feb 15, 2024

This is an elegant solution and thank you. I have my yml files divided into subdirectories for paths, parameters, schemas, etc...
In my configuration, I'm initializing my app like this:

basedir = pathlib.Path(__file__).parent.resolve()
swagoptions = SwaggerUIOptions(swagger_ui = True, swagger_ui_template_dir = basedir / 'swagger-ui')
connex_app = FlaskApp(__name__, specification_dir=basedir / "apispecs")

I can create the utility class and leverage my basedir in all of this, but I'm getting errors at startup:

jsonschema.exceptions._RefResolutionError: [Errno 21] Is a directory

Paths are like this to specs, where my swagger.yml refers to the proper paths:

project/
      │── apispecs/
               |── swagger.yml
               |── parameters/
                         |── _index.html
               │── securitySchemas/
                         |── _index.html
               │── schemas/
                         |── _index.html

swagger.yml sample:

components:
  schemas:
    $ref: "./schemas/_index.yml"
  parameters:
    $ref: "./parameters/_index.yml"
  securitySchemes:
    $ref: "./security/_index.yml"

I would think this solution would be looking at the file associated with each $ref. Not sure why that's happening but I'll keep at it.

@eharvey71
Copy link

New issue opened: #1909

@eharvey71
Copy link

I'm no longer having this issue with 3.1

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