spec-first / connexion

Connexion is a modern Python web framework that makes spec-first and api-first development easy.
https://connexion.readthedocs.io/en/latest/
Apache License 2.0
4.47k stars 758 forks source link

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

Open maddin1991 opened 7 months ago

maddin1991 commented 7 months ago

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:

eharvey71 commented 6 months ago

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 commented 6 months ago

@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 commented 6 months ago

I’m still investigating. The first time it broke, I was on windows. It worked on MacOS for me but I was on 3.0.2. Once I installed 3.0.5, it broke on MacOS. Either way, no dice on Windows 10. I’ve been through every module, hoping to prove helpful.

On Tue, Feb 13, 2024 at 4:58 PM Robbe Sneyders @.***> wrote:

@eharvey71 https://github.com/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

— Reply to this email directly, view it on GitHub https://github.com/spec-first/connexion/issues/1861#issuecomment-1942707023, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH6LDYNXMPNI2KCDNMWZ43YTPOZNAVCNFSM6AAAAABCXZ7H2CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBSG4YDOMBSGM . You are receiving this because you were mentioned.Message ID: @.***>

maddin1991 commented 6 months ago

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 commented 6 months ago

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 commented 5 months ago

New issue opened: #1909

eharvey71 commented 3 months ago

I'm no longer having this issue with 3.1