JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

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

Open RossOliver opened this issue 12 years ago • 24 comments

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?

RossOliver avatar May 31 '13 13:05 RossOliver

@RossOliver maybe take a look at the XML, Yaml and Annotation Drivers. They might give you a clue as to how to disable the discriminator. E.g. I just had to disable it in my XML class like this in order to fix the error:

<class discriminator-disabled="true"/>

I'm not sure if this is a hack that is working around a design issue I've got, but I don't see why I shouldn't be allowed to have my DB column named (e.g.) type and at the same time have a serialized type virtual property.

jonathaningram avatar Jul 18 '13 12:07 jonathaningram

+1 same issue here

artworkad avatar Aug 06 '13 14:08 artworkad

Same here... although commenting out the section in ClassMetadata.php did not help in my case

apostle-nl avatar Oct 24 '13 13:10 apostle-nl

+1 same issue this time with join table inheritance

davidkalosi avatar Jan 17 '14 20:01 davidkalosi

Hi All, Any update on this problem? somehow seems to behave strange when tries to deserialise an object that uses inheritance still. Same error message here too

fredcallagan avatar Feb 17 '14 12:02 fredcallagan

Same issue

maninhat avatar Feb 20 '14 17:02 maninhat

So, why this error persists 10 month later?

clubdesarrolladores avatar Mar 19 '14 18:03 clubdesarrolladores

ping @schmittjoh is this expected behaviour? Experiencing the same issue in the latest release

jameshalsall avatar May 13 '14 21:05 jameshalsall

Seems that the issue is still there.

Ended up implementing a specific behaviour for some Class we had in the project. In our case was just a single limited case so was the quickest solution found.

Good luck

Fryderyk

2014-05-13 22:26 GMT+01:00 James Halsall [email protected]:

ping @schmittjoh https://github.com/schmittjoh is this expected behaviour? Experiencing the same issue in the latest release

— Reply to this email directly or view it on GitHubhttps://github.com/schmittjoh/JMSSerializerBundle/issues/299#issuecomment-43015246 .

fredcallagan avatar May 13 '14 21:05 fredcallagan

Thanks for the reply, I've attempted to workaround it in similar ways as the guys mentioned above but I'm using XML configuration which is slightly different. I will post an example later of what I'm trying

jameshalsall avatar May 14 '14 08:05 jameshalsall

The exception throws even on entities that not related to the entities that i serialize. Annotation way @JMS\Discriminator(disabled=true)

karser avatar Jul 02 '14 14:07 karser

+1

andreaslarssen avatar Mar 02 '15 11:03 andreaslarssen

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 )

yurytolochko avatar Jun 10 '15 10:06 yurytolochko

I think just stumbled here after wondering why I couldn't disable the discriminator field in the output.

1/ The 'disabled' flag for the discriminator is not mentioned in the documentation yet this is a very important bit of the API.

2/ I think there is a good design reason to add this: the discriminator property sole function is to aid later deserialization through JMS. If you are XML output is following a specific XSD schema and is consumed by another application, then discriminator fields are just cruft as they are only internally relevant to the serializing application. Generally, XML parsers should ignore those extra fields, but if you can avoid them in your serialization output: all the better.

netsensei avatar Oct 13 '15 20:10 netsensei

FYI I hit it the exception this way using: JMSSerializerBundle + Doctrine Table Inheritance and if you name the doctrine's discriminator-column = "discriminator" :) U'll hit the same exception

In my case I just needed to rename the discriminator-column From: discriminator To: other_value

paceto256 avatar Oct 16 '15 14:10 paceto256

This is still happening...

Coffee2CodeNL avatar Jul 18 '17 14:07 Coffee2CodeNL

+1

keanolane avatar Aug 21 '17 13:08 keanolane

+1

piotr-oles avatar Nov 21 '17 15:11 piotr-oles

+1

gleydsonsilva avatar Dec 21 '17 16:12 gleydsonsilva

+1

Napas avatar Mar 19 '18 14:03 Napas

+1

lgraubner avatar Apr 11 '18 07:04 lgraubner

It does seem to work if I enable exclusion_policy: all though.

lgraubner avatar Apr 11 '18 07:04 lgraubner

I'm using the field so had to set disabled = true in the discriminator. Not really sure why, seems a bit unintuitive.

nicholasruunu avatar Apr 28 '20 15:04 nicholasruunu

I'm still reproducing the issue and no previous solution is working

astronati avatar Jun 10 '20 08:06 astronati