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

[Enhancement] Properties without setter are read-only #85

Closed
madebyherzblut opened this issue Feb 20, 2012 · 6 comments
Closed

[Enhancement] Properties without setter are read-only #85

madebyherzblut opened this issue Feb 20, 2012 · 6 comments

Comments

@madebyherzblut
Copy link

When I set the annotation @Accessor(getter="foo") on a property I expect this property to be read-only.

Example:

<?php

class ObjectType
{
    /**
     * @Serializer\Type("integer")
     * @Serializer\Accessor(getter="getId")
     */
    protected $id;
}

$types = $serializer->deserialize(
  '[{"id":123123,"bundle":"TestBundle","controller":"TestController"}]',
  'array<ObjectType>',
  'json'
 );

Result:

object(ObjectType)[58]
      protected 'id' => int 123123
      protected 'name' => null
      protected 'bundle' => string 'TestBundle' (length=10)
      protected 'controller' => string 'TestController' (length=14)

I could skip some validation/cleaning logic if id is never set by deserialize.

@schmittjoh
Copy link
Owner

The accessor annotation just changes the way the value is retrieved. If you need to exclude something, then you can use the @exclude annotation on the id property, or a custom exclusion strategy.

@madebyherzblut
Copy link
Author

@Exclude wouldn't work because I would like to include this property when the object is serialized (therefore "read-only"). A custom exclusion strategy is indeed an option, although it requires more code than the suggest enhancement.

@schmittjoh
Copy link
Owner

I understand your proposal, but it's a different layer. Maybe serialization groups would help you, or we could add another annotation, sth like @readonly.

@ruudk
Copy link
Contributor

ruudk commented Mar 8, 2012

So there's no solution for this right now? I want the exact same thing and @exclude wont help because I want it when I serialize the object.

@ruudk
Copy link
Contributor

ruudk commented Mar 8, 2012

Another way of fixing this would be by removing the exception and just return.

if (!$metadata->type) {
  //throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->getDeclaringClass()->getName(), $metadata->name));
  return;
}

Maybe making it optional via config?

@schmittjoh
Copy link
Owner

There are basically two solutions that I see:

  1. Add a @readonly annotation
  2. Add support for @group (which I think is better as it is more generic, and not tied to this specific use-case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants