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

Handle Swagger Schema Objects #17

Open
jeremyb opened this issue Jun 20, 2017 · 3 comments
Open

Handle Swagger Schema Objects #17

jeremyb opened this issue Jun 20, 2017 · 3 comments

Comments

@jeremyb
Copy link

jeremyb commented Jun 20, 2017

Hi,

I'm using Swagger to define Schema Objects which are based on the JSON Schema Specification Draft 4.

I have a standalone example that you can find here:
https://gist.github.com/jeremyb/a0419ad9d5501fb602f052169112166f

I'm curious to know if you might be interested to make symfony-json-rest-api works with Swagger (not only with separate JSON Schema files).

Currently, I see some issues with the PayloadValidator class (especially with $jsonSchemaFilename: file_exists and realpath calls).

Also, we probably need to update JsonSchemaTools to allow the creation from a SchemaStorage?

Do you have some ideas about how to handle that? I'm available to contribute in the next days/weeks :)

@tyx
Copy link
Member

tyx commented Jun 20, 2017

Hi !

I'm curious to know if you might be interested to make symfony-json-rest-api works with Swagger (not only with separate JSON Schema files).

Definitively yes! Swagger is close to be mainstream. I guess we should keep decoupled stuff but we can make effort to make swagger validation compatible.

I guess we could enforce the usage of SchemaStorage even for simple jsonSchema. Let say a SchemaLoader, so the realpath and file_exists would be done outside the validator. (SRP will thank us).

Something like

<?php

namespace Rezzza\SymfonyRestApiJson;

class PayloadValidator
{
    private $jsonSchemaTools;

    private $schemaloader;

    public function __construct(JsonSchemaTools $jsonSchemaTools, SchemaLoader $schemaloader)
    {
        $this->jsonSchemaTools = $jsonSchemaTools;
        $this->schemaloader = $schemaloader;
    }

    public function validate($payload, $validationPath)
    {
        $validationSchema = $this->schemaloader->schemaOfPath($validationPath);

        $delegateValidator = $this->jsonSchemaTools->createValidator();
        $refResolver = $this->jsonSchemaTools->createRefResolver();

        $delegateValidator->check(
            json_decode($payload),
            $refResolver->resolve($validationSchema->asString())
        );

        if (!$delegateValidator->isValid()) {
            throw InvalidPayload::withErrors($payload, $jsonSchemaFilename, $delegateValidator->getErrors());
        }
    }
}

where schema loader would accept an array of id => filepath in construct (we could event support http in a next step).

So for your example:

new SchemaLoader([
    'petstore' => 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore-simple.json'
]);

and in routing.yml

route:
     _jsonSchema: "petstore#/definitions/NewPet"

It would fix all issues, does not it ?

@jeremyb
Copy link
Author

jeremyb commented Jun 23, 2017

Hi @tyx

Sorry for the late response.

Basically, I really like what you purpose, especially the SchemaLoader part. And it would fix all the issues. The pain point to me is $refResolver->resolve($validationSchema->asString()) and in particular the asString(). Perhaps there's something to do with the justinrainbow/json-schema package with the UriRetriever for example? Do you see what I have in mind? It's just an idea for now, so I need to think more about this, and I didn't have enough time this week to investigate deeper :)

@tyx
Copy link
Member

tyx commented Jun 23, 2017

Ok yes may be we should have extra work around the refResolver but I'm sure it is not a big deal :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants