schmittjoh / JMSSerializerBundle

Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
http://jmsyst.com/bundles/JMSSerializerBundle
MIT License
1.8k stars 312 forks source link

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

Open RossOliver opened 11 years ago

RossOliver commented 11 years ago

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?

jonathaningram commented 11 years ago

@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.

artworkad commented 11 years ago

+1 same issue here

apostle-nl commented 11 years ago

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

davidkalosi commented 10 years ago

+1 same issue this time with join table inheritance

fredcallagan commented 10 years ago

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

maninhat commented 10 years ago

Same issue

clubdesarrolladores commented 10 years ago

So, why this error persists 10 month later?

jameshalsall commented 10 years ago

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

fredcallagan commented 10 years ago

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 notifications@github.com:

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 .

jameshalsall commented 10 years ago

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

karser commented 10 years ago

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

andreaslarssen commented 9 years ago

+1

yurytolochko commented 9 years ago

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 )

netsensei commented 9 years ago

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.

paceto256 commented 9 years ago

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

Coffee2CodeNL commented 7 years ago

This is still happening...

keanolane commented 7 years ago

+1

piotr-oles commented 6 years ago

+1

gleydsonsilva commented 6 years ago

+1

Napas commented 6 years ago

+1

lgraubner commented 6 years ago

+1

lgraubner commented 6 years ago

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

nicholasruunu commented 4 years ago

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

astronati commented 4 years ago

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