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

Using jsonpath in refResolver... #279

Closed
cheeryfella opened this issue Jun 13, 2016 · 5 comments
Closed

Using jsonpath in refResolver... #279

cheeryfella opened this issue Jun 13, 2016 · 5 comments

Comments

@cheeryfella
Copy link

I am attempting to validate a request body against jsonschema.

Schema is swagger 2.0 generated from stoplight...

salient json:

{
  "paths": {
    "/token": {...},
    "/token/{token}": {...},
    "{uuid}": {...}
    "/": {
      "post": {
        "parameters": [
          {...},
          {
            "name": "body",
            "in": "body",
            "schema":
            ....
          },
          {...}
        ]
      }
    } 
  }

This is my attempt to load the schema

$schema = $refResolver->resolve('file://' . realpath(SPEC_ROOT . '/svc.json') . '#/paths/~1/post/parameters[?(@.name=="body")]/schema');

only response I get

JsonSchema\Exception\UnresolvableJsonPointerException: File: file:///code/specification/svc.json is found, but could not resolve fragment: #/paths/~1/post/parameters[?(@.name=="body")]/schema in /code/vendor/justinrainbow/json-schema/src/JsonSchema/RefResolver.php:108

Are jsonpath filters supported and if so what am I overlooking?

@steffkes
Copy link
Contributor

Are jsonpath filters supported and if so what am I overlooking?

not as far as i know. i haven't seen anything related to filters while working with our JsonPointer implementation. up to …/svc.json#/paths/~1/post/parameters it works like expected - then it starts failing with the given exception.

@bighappyface
Copy link
Collaborator

Is this still an issue?

@iongion
Copy link

iongion commented Feb 5, 2017

For this schema I still get it if I try to resolve RequestCreateInput

{
  "definitions": {
    "RequestCreateInputProductFile": {
      "type": "object",
      "properties": {
        "filename": {
          "type": "string"
        },
        "size": {
          "type": "number"
        },
        "mimetype": {
          "type": "string"
        }
      }
    },
    "RequestCreateInputProduct": {
      "type": "object",
      "properties": {
        "productName": {
          "type": "string"
        },
        "productDescription": {
          "type": "string"
        },
        "acquisitionField": {
          "type": "string"
        },
        "unitOfMeasurementId": {
          "type": "number"
        },
        "supplyDate": {
          "type": "string"
        },
        "productNotes": {
          "type": "string"
        },
        "quantity": {
          "type": "number"
        },
        "files": {
          "type": "array",
          "items": {
            "schema": {
              "$ref": "#/definitions/RequestCreateInputProductFile"
            }
          }
        }
      },
      "required": [
        "productName",
        "productDescription",
        "acquisitionField",
        "unitOfMeasurementId",
        "supplyDate",
        "quantity"
      ]
    },
    "RequestCreateInput": {
      "type": "object",
      "properties": {
        "requestName": {
          "type": "string"
        },
        "paymentOptionId": {
          "type": "number"
        },
        "requestNotes": {
          "type": "string"
        },
        "productsData": {
          "type": "array",
          "items": {
            "schema": {
              "$ref": "#/definitions/RequestCreateInputProduct"
            }
          }
        }
      },
      "required": [
        "requestName",
        "paymentOptionId"
      ]
    },
    "RequestCreateResult": {
      "type": "object",
      "properties": {
        "requestId": {
          "type": "string"
        },
        "requestName": {
          "type": "string"
        }
      },
      "required": [
        "requestId",
        "requestName"
      ]
    },
    "RequestCreateSuccesResponse": {
      "type": "object",
      "required": [
        "success",
        "message"
      ],
      "properties": {
        "success": {
          "type": "boolean",
          "default": true
        },
        "message": {
          "schema": {
            "$ref": "#/definitions/RequestCreateResult"
          }
        }
      }
    }
  }
}

@iongion
Copy link

iongion commented Feb 5, 2017

Found the issue - my side, and I guess it might be for others.
The schema passed to the resolver did not in fact contain any of these definitions!

@DannyvdSluijs
Copy link
Collaborator

@cheeryfella since #277 the RefResolver was removed which became part of v3.0.0. I quickly checked based on your inputs if the above would work but it seems the JSONPointer you're giving is incorrect, it is actually a mix of JSONPath (rfc9535) and JSONPointer (rfc6901). The $ref support uses JSONPointer which doesn't allow for filtering as that is part of JSONPath.

For reference the code I used for reproduction can be found below. I've tweaked the JSONPointer part of the schema to be valid. This however pins the index of the parameters which can cause issues when shifting parameter order.

In an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume this is a rather old issue, which sadly was left unanswered, and can be closed? Feel free to close it yourself with some comments if helpful.

Schema:

{
  "paths": {
    "/token": {},
    "/token/{token}": {},
    "{uuid}": {},
    "/": {
      "post": {
        "parameters": [
          {},
          {
            "name": "body",
            "in": "body",
            "schema": {}
          },
          {}
        ]
      }
    }
  }
}

Code:

<?php

require_once '../../vendor/autoload.php';

$data = (object) [];

$validator = new JsonSchema\Validator;
$result = $validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json') . '#/paths/~1/post/parameters/1/schema']);

var_dump($result);

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

6 participants