Skip to content

False passes with file loaded schemas. #340

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

Closed
elb98rm opened this issue Dec 9, 2016 · 13 comments
Closed

False passes with file loaded schemas. #340

elb98rm opened this issue Dec 9, 2016 · 13 comments

Comments

@elb98rm
Copy link

elb98rm commented Dec 9, 2016

I'm working on a laravel project (so please excuse any framework specific functions), and I'm noticing false positives on the validation tests.
The system is part of a restful api, and as such I'm validating all requests for item creation.

If I define the following schema (docs/json/test_schema.json):

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Foo",
  "description": "Properly defines a Foo",
  "type": "object",
  "properties": {
    "foo_id_text": {
      "description": "UUID for this Foo",
      "type": "string"
    }
  },
  "required": [
    "foo_id_text"
  ]
}

Inside my controller, I have the following:

$json = json_decode('{}');

$this->validator->check(
    $json,
    (object)['$ref' => 'file://' . base_path('docs/json/test_schema.json')]
);

var_dump($this->validator->isValid()); exit;

... then I get:

bool(true)

Note that:

  • if I create a FILE based json and FILE based compare, OR
  • do an INLINE schema definition compared to an INLINE json submission
    ... it behaves as expected (i.e : bool(false)

I've reduced this to such a low level I think it is actually a bug. Can you please investigate (alongside me).

Cheers
Rick

@shmax
Copy link
Collaborator

shmax commented Dec 11, 2016

if I create a FILE based json

Confused. Isn't that what you're demonstrating in your sample?

I just gave it a try and it seemed to work for me, but that may be because I have written my own UriRetriever (because the default UriRetriever never seems to work for me). Have you verified with certainty that UriRetriever::retrieve is producing a valid schema (and not, say, an empty object)?

@elb98rm
Copy link
Author

elb98rm commented Dec 12, 2016

So inline means I literally define it as a variable within the code:

$json = { ... }
$json_schema = { ... }

File based schema means I include it as follows:

(object)['$ref' => 'file://' . base_path('docs/json/test_schema.json')]
(object)['$ref' => 'file://' . base_path('docs/json/test_json.json')]

As I mentioned. The following methods work as expected:

  • file based json and file based schema
  • inline based json and inline schema

If, as in my example, I use a file based schema and an inline json declaration it doesn't work.

To answer your questions - if I do the following:

$uri_r = new UriRetriever();
var_dump($uri_r->retrieve('file:///var/www/html/docs/json/test_schema.json'));

I get the schema as I'd expect:

object(stdClass)#475 (7) {
  ["$schema"]=>
  string(39) "http://json-schema.org/draft-04/schema#"
  ["title"]=>
  string(3) "Foo"
  ["description"]=>
  string(22) "Properly defines a Foo"
  ["type"]=>
  string(6) "object"
  ["properties"]=>
  object(stdClass)#477 (1) {
    ["foo_id_text"]=>
    object(stdClass)#476 (2) {
      ["description"]=>
      string(17) "UUID for this Foo"
      ["type"]=>
      string(6) "string"
    }
  }
  ["required"]=>
  array(1) {
    [0]=>
    string(11) "foo_id_text"
  }
  ["id"]=>
  string(47) "file:///var/www/html/docs/json/test_schema.json"
}

To show it's not working, I run the following:

// in the constructor
$this->validator = new Validator();

// later on...
$json = {};

$this->validator->check(
	$json,
	(object)['$ref' => 'file://' . base_path('docs/json/test_schema.json')]
);

var_dump($this->validator->isValid());

The result:

bool(true)

This should fail, as there is a requirement for foo_id_text.

Apart from the slightly laravel changes (base_path etc), this is pretty much straight out of the docs in github.

@shmax
Copy link
Collaborator

shmax commented Dec 12, 2016

Gotcha. Still can't reproduce, though. Can we see your full code sample, including the declaration of your validator (and any dependency injections)?

@elb98rm
Copy link
Author

elb98rm commented Dec 12, 2016

Ok, you pretty much have it, but here's all the relevant bits of info and code:

Laravel 5.3

include in composer.json :

"justinrainbow/json-schema": "*",

The class:

use JsonSchema\Validator;

class AssembliesController 

The __construct :

    /**
     * AssembliesController constructor.
     */
    public function __construct()
    {
        // no auth while testing
        $this->middleware('auth')->except(
            [
                'jsonIndex',
                'jsonDetails',
                'jsonCreate'
            ]
        );

        // set up some helpers
        $this->uuid_helper = new UuidHelper();
        $this->json_helper = new JsonHelper();
        $this->validator = new Validator();
    }

The method:

    public function jsonCreate(Request $request): JsonResponse
    {
            // Test with an empty schema that should definitely fail:
            $json = json_decode('{}');
            // check the schema location
            var_dump((object)['$ref' => 'file://' . base_path('docs/json/test_schema.json')]);
            // check the schema object
            $uri_r = new UriRetriever();
            var_dump($uri_r->retrieve('file:///var/www/html/docs/json/test_schema.json'));

            $this->validator->check(
                $json,
                (object)['$ref' => 'file://' . base_path('docs/json/test_schema.json')]
            );
            // check the result (should be false)
            var_dump($this->validator->isValid());

            exit;

The schema: /docs/json//test_schema.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Foo",
  "description": "Properly defines a Foo",
  "type": "object",
  "properties": {
    "foo_id_text": {
      "description": "UUID for this Foo",
      "type": "string"
    }
  },
  "required": [
    "foo_id_text"
  ]
}

The output:

object(stdClass)#472 (1) {
  ["$ref"]=>
  string(47) "file:///var/www/html/docs/json/test_schema.json"
}
object(stdClass)#475 (7) {
  ["$schema"]=>
  string(39) "http://json-schema.org/draft-04/schema#"
  ["title"]=>
  string(3) "Foo"
  ["description"]=>
  string(22) "Properly defines a Foo"
  ["type"]=>
  string(6) "object"
  ["properties"]=>
  object(stdClass)#477 (1) {
    ["foo_id_text"]=>
    object(stdClass)#476 (2) {
      ["description"]=>
      string(17) "UUID for this Foo"
      ["type"]=>
      string(6) "string"
    }
  }
  ["required"]=>
  array(1) {
    [0]=>
    string(11) "foo_id_text"
  }
  ["id"]=>
  string(47) "file:///var/www/html/docs/json/test_schema.json"
}
bool(true)

As discussed, the last result should be bool(false)

Note, I have also tried this with a non empty json packet, for example:

$json = json_decode('{"bar_id_text": "ED1A9EC0-B8AA-11E6-9405-7B93AE0EFC7F"}');

Cheers
Rick

@shmax
Copy link
Collaborator

shmax commented Dec 13, 2016

I don't see anything obviously wrong with any of that. At this point, I'm afraid the best advice I can give you is to fire up the debugger and start stepping through. We know you can get things to work as expected by doing it inline, so I would start by debugging that to get a feel for how the flow should work, then going through it again with the file-based version. Let us know what you find out!

@elb98rm
Copy link
Author

elb98rm commented Dec 13, 2016

Roger that. I've got a few things to finish on the project first, so I'll have to prioritise them.
I should be able to review it this week tho. I'll report back in.
Rick.

@shmax
Copy link
Collaborator

shmax commented Mar 11, 2017

I don't know if you're still working on this, but is it possible that the only problem is that you're missing a call to addStorage? @erayd might have some insight.

@erayd
Copy link
Contributor

erayd commented Mar 11, 2017

@elb98rm I think you're using an old version of the library - recommend you update it. I've done some testing, and it looks like this was fixed in 3.0.0, but is a problem in older versions.

If you are using a version >= 3.0.0, or if you can reproduce it >= 5.1.0 please let us know - otherwise I think we can probably consider this issue closed.

Tests used to reproduce:

/tmp/schema.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Foo",
    "description": "Properly defines a Foo",
    "type": "object",
    "properties": {
        "foo_id_text": {
            "description": "UUID for this Foo",
            "type": "string"
        }
    },
    "required": [
        "foo_id_text"
    ]
}

Test script

<?php

require('vendor/autoload.php');

use JsonSchema\Validator;

$validator = new Validator();

$schema = json_decode('{"$ref":"file:///tmp/schema.json"}');
$invalidInput = json_decode('{}');
$validInput = json_decode('{"foo_id_text":"stringValue"}');

$validator->check($validInput, $schema);
assert($validator->isValid() === true);

$validator->check($invalidInput, $schema);
assert($validator->isValid() === false);

echo("Test completed OK\n");

@elb98rm
Copy link
Author

elb98rm commented Mar 12, 2017

Thanks for the feedback. I'll look at this shortly and respond.

@erayd
Copy link
Contributor

erayd commented Apr 11, 2017

@elb98rm Did you have a chance to look at this?

@elb98rm
Copy link
Author

elb98rm commented Apr 13, 2017

Sorry - this got lost in a development cycle: I'll have an answer for you this weekend.

@DannyvdSluijs
Copy link
Collaborator

@elb98rm 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.

@elb98rm
Copy link
Author

elb98rm commented Feb 5, 2024

Yup. I've moved on: you can clean up the ticket! :)

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

4 participants