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

allOf is not properly merged #31

Open
shadowhand opened this issue Jul 29, 2019 · 15 comments
Open

allOf is not properly merged #31

shadowhand opened this issue Jul 29, 2019 · 15 comments
Labels
bug Something isn't working help wanted Extra attention is needed needs investigation Work is needed to figure out the root cause of the problem.

Comments

@shadowhand
Copy link
Contributor

shadowhand commented Jul 29, 2019

Given a schema that uses allOf to combine two different object schemas, the properties of the two schemas are not combined:

{
  "components": {
    "schemas": {
      "identifier": {
        "type": "object",
        "properties": {
           "id": {"type": "string"}
        }
      },
      "person": {
        "allOf": [
          {"$ref": "#/components/schemas/identifier"},
          {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

Not 100% sure that schema validates, but it should be a good start.

cebe added a commit that referenced this issue Jul 30, 2019
@cebe
Copy link
Owner

cebe commented Jul 30, 2019

Hi, thanks for the report, could you elaborate on how you read the schema and what you expect in the output. I added a test for your Schema (b9eddbd) and as far as I see everything is loaded correctly.

@shadowhand
Copy link
Contributor Author

I believe the resolved reference should include both the id and name properties; something like:

assert($resolvedRef->type === 'object');
assert($resolvedRef->properties->id->type === 'string');
assert($resolvedRef->properties->name->type === 'string');

When I tried to use https://github.com/lezhnev74/openapi-psr7-validator it gave me errors with any schema that used allOf, stating that it couldn't find the (combined) properties.

@cebe
Copy link
Owner

cebe commented Aug 7, 2019

The purpose of php-openapi is to read and write OpenAPI description files as they are, so you get the raw allOf element with its sub-schemas only. For schema validation you need to use other tools (at least now this is not implemented). The validation of the request schema against the API description has to be made by lezhnev74/openapi-psr7-validator which should validate the request based on allOf semantics.

@cebe
Copy link
Owner

cebe commented Aug 7, 2019

ping @lezhnev74

@lezhnev74
Copy link
Contributor

Hey! Ok, this looks like a problem with resolving references. Let me try to write a failing test first.

@lezhnev74
Copy link
Contributor

I've added this test which validates a response body against a schema with a reference:

The validator builds a schema in the memory and resolves all the references automatically.
You can access resolved schema like this:

$schema = $validator->getSchema();
$allOf = $schema->paths['/ref']->post->responses['200']->content['application/json']->schema->allOf;
var_dump($allOf[0]->properties['age']->type); // "integer"
var_dump($allOf[1]->properties['name']->type); // "string"

@lezhnev74
Copy link
Contributor

@cebe as I understand, one can resolve references manually like this:

use cebe\openapi\Reader;
use cebe\openapi\ReferenceContext;
use function realpath;


$filePath = "./schema.yaml";
$schema = Reader::readFromYamlFile($filePath);
$schema->resolveReferences(new ReferenceContext($schema, realpath($filePath)));

@cebe
Copy link
Owner

cebe commented Aug 9, 2019

resolveReferences() is called automatically by default, so the reference should not be a problem:

$resolveReferences parameter defaults to true:

php-openapi/src/Reader.php

Lines 93 to 100 in 13b9175

public static function readFromYamlFile(string $fileName, string $baseType = OpenApi::class, $resolveReferences = true): SpecObjectInterface
{
$spec = static::readFromYaml(file_get_contents($fileName), $baseType);
$spec->setReferenceContext(new ReferenceContext($spec, $fileName));
if ($resolveReferences) {
$spec->resolveReferences();
}
return $spec;

@cebe cebe added help wanted Extra attention is needed needs investigation Work is needed to figure out the root cause of the problem. bug Something isn't working labels Oct 24, 2019
@cebe
Copy link
Owner

cebe commented Oct 28, 2019

Hi, version 1.3.0 fixes a lot of issues with references, please try if this is solved by the changes.

@cebe
Copy link
Owner

cebe commented Feb 28, 2020

Closing this issue for now, please comment if the issue persists. I'll reopen it then.

@hotmeteor
Copy link

@cebe We're still seeing this issue.

allOf components are not resolving correctly.

You can see the attached issue here: hotmeteor/spectator#38

@cebe
Copy link
Owner

cebe commented Apr 23, 2021

@hotmeteor I see hotmeteor/spectator#38 (comment) is closed now, is there still something to fix in cebe/php-openapi or was this only a problem in your tool?

@hotmeteor
Copy link

No, there's still an issue. I created a workaround in my tool, but I'd prefer to see things merging as expected. This is the example schema that we're testing with: https://github.com/hotmeteor/spectator/blob/master/tests/Fixtures/Components.v1.json

@cebe cebe reopened this May 26, 2021
@hotmeteor
Copy link

@cebe I forgot I had reported this issue, so I had created another ticket here: #120

It can be closed, but it does contain some useful information for testing.

@cebe
Copy link
Owner

cebe commented Oct 13, 2021

still not really sure what this is about. Could someone come up with a failing test case for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed needs investigation Work is needed to figure out the root cause of the problem.
Projects
None yet
Development

No branches or pull requests

4 participants