php-openapi icon indicating copy to clipboard operation
php-openapi copied to clipboard

Unable to reference other local json file

Open mattias-persson opened this issue 1 year ago • 2 comments

Hi!

I'm trying to build an OAPI definition where I split shared data into separate files. I'm only using json format as of now. One example would be doing something like this:

{
  "openapi": "3.0.0",
  "info": {
    "title": "My API",
    "version": "1, 2"
  },
  "paths": {
    "/v1/users/profile": {
      "get": {
        "operationId": "V1GetUserProfile",
        "summary": "Returns the user profile",
        "responses": {
          "401": {
            "$ref": "./401.json"
          }
        }
      }
    }
  }
}

and the 401 file would contain the actual details:

"description": "401 response",
"content": {
  "application/json": {
    "schema": {
      "properties": {
        "message": {
          "type": "string"
        }
      },
      "required": [
        "message"
      ]
    },
    "example": {
      "message": "Unauthenticated."
    }
  }
}

When attempting to do this, it seems this package formats the URI to become file://401.json. This causes the parser to fail. The stack trace looks like this:

cebe\openapi\exceptions\UnresolvableReferenceException : Failed to resolve Reference './401.json' to cebe\openapi\spec\Response Object: file_get_contents(file:///401.json): Failed to open stream: No such file or directory
 vendor/cebe/php-openapi/src/spec/Reference.php:220
 vendor/cebe/php-openapi/src/spec/Responses.php:244
 vendor/cebe/php-openapi/src/SpecBaseObject.php:410
 vendor/cebe/php-openapi/src/SpecBaseObject.php:410
 vendor/cebe/php-openapi/src/spec/PathItem.php:200

I'm on a Mac. Any ideas?

mattias-persson avatar Dec 29 '22 13:12 mattias-persson

Having the same issue here

tomasbreffitt avatar Apr 04 '23 00:04 tomasbreffitt

Did you try

 "401": {
    "$ref": "401.json"
  }

Aribros avatar Sep 15 '23 10:09 Aribros