Skip to content

Incorrect swagger definition when using foreign fields #3668

@nickvanderzwet

Description

@nickvanderzwet

API Platform version(s) affected: 2.5.6

Description

The Read definition is created for the first found field (as foreign-field for that particular entity) or the entity itself. So when a foreign-field is found first is uses the serialization group of this field and ignores the normalizationContext of the entity.

How to reproduce

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"a:read"}, "swagger_definition_name"="Read"},
 *     denormalizationContext={"groups"={"a:write"}, "swagger_definition_name"="Write"}
 * )
 */

class A {
/**
 * @var B
 * @Groups({"a:read", "a:write"})
 */
 private $b;
}
Object B
/**
 * @ApiResource(
 *     normalizationContext={"groups"={"b:read"}, "swagger_definition_name"="Read"},
 *     denormalizationContext={"groups"={"b:write"}, "swagger_definition_name"="Write"}
 * )
 */

class B {
/**
 * @Groups({"b:read", "a:read"})
 */
 private $id;

/**
 * @Groups({"b:read"})
 */
 private $field2;

/**
 * @Groups({"b:read"})
 */
  private $field3;

Object A is handled first and because of $b it already creates a B-Read based on a:read.

In https://github.com/api-platform/core/blob/master/src/JsonSchema/SchemaFactory.php#L107 it checks for an B-Read which was already created and it ignores all the b:read fields in Object B.

Possible Solution

Additional Context
The actual response of the API is correct but the api spec isn't. This is an issue when generating classes based on the api spec. So basically an incorrect api-spec.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions