JMSSerializerBundle: The discriminator field name \"discriminator\" of the base-class "..." conflicts with a regular property of the sub-class "...".

I have came across an issue with single table inheritance and serialisation - I get the above error unless I comment out the following code in ClassMetadata.php, after which it works as expected:

if (isset($this->propertyMetadata[$this->discriminatorFieldName])
                    && ! $this->propertyMetadata[$this->discriminatorFieldName] instanceof StaticPropertyMetadata) {
                throw new \LogicException(sprintf(
                    'The discriminator field name "%s" of the base-class "%s" conflicts with a regular property of the sub-class "%s".',
                    $this->discriminatorFieldName,
                    $this->discriminatorBaseClass,
                    $this->name
                ));
            }

I think the exception is thrown because it’s checking if the subclass has a property with the name of the discriminator field, which it has to as it subclasses the base-class which defines the field - unless I’m misunderstanding?

About this issue

  • Original URL
  • State: open
  • Created 11 years ago
  • Comments: 24

Commits related to this issue

Most upvoted comments

Recently I faced same error. What I did is just remove discriminator field from class.

Was:

/**
 * @JMS\Discriminator(field="type", map={"foo": "FooRequest", "bar": "BarRequest"})
 */
abstract class BaseRequest {
    /**
     * THIS ONE CAUSE ERROR
     * @JMS\Type("string")
     */
    public $type;
}

class FooRequest extends BaseRequest {
    /**
     * @JMS\Type("string")
     */
    public $foo;
}

class BarRequest extends BaseRequest {
    /**
     * @JMS\Type("string")
     */
    public $bar;
}

Become:

/**
 * @JMS\Discriminator(field="type", map={"foo": "FooRequest", "bar": "BarRequest"})
 */
abstract class BaseRequest {

}

class FooRequest extends BaseRequest {
    /**
     * @JMS\Type("string")
     */
    public $foo;
}

class BarRequest extends BaseRequest {
    /**
     * @JMS\Type("string")
     */
    public $bar;
}

Hope it will be help somebody )