Coder Social home page Coder Social logo

[Serializer] Restrictions about symfony HOT 4 OPEN

evrinoma avatar evrinoma commented on June 15, 2024
[Serializer] Restrictions

from symfony.

Comments (4)

krzysztof-ciszewski avatar krzysztof-ciszewski commented on June 15, 2024 1

I figured out why this happens. The type in the projectsData example is correctly detected as ProjectData class because the property name is considered plural. When that happens the type extractor looks for the method with add prefix first and from that it detects the correct type. With projectsDto the name is considered singular, so it first looks for method with set prefix and since it's array it deserializes into a simple array.

To fix this you just have to add a phpdoc typehint to your property

    #[SerializedName('projectDto')]
    /**
    * @var \App\Dtos\ProjectData[]
    */
    private array $projectsDto = [];

I am not sure if this is considered a bug. In my opinion it is, we should always check for add methods first since they are most likely to have a type hint in the argument, but I might be missing something. Looking for advice here.

Depending on this being considered a bug I can either create a fix or update the docs and mention this case.

from symfony.

EugeneKwasny avatar EugeneKwasny commented on June 15, 2024

Thank you @evrinoma for creating this bug report! This indeed looks
like a bug. I reproduced the bug in the "main" branch of
https://github.com/EugeneKwasny/issue-5475-bug-repoducer

Status: Reviewed

from symfony.

evrinoma avatar evrinoma commented on June 15, 2024

I'm afraid your assumption seems to be incorrect, and it won't work as expected. Could you please take a closer look at the code, not just at the ReflectionExtractor, but also at this problematic part of the EnglishInflector. In this file, variable names are hardcoded in reverse, and data is present in the list, but not dto. In common, It seems odd if I were to use German and name a variable projektDaten, and then the serializer behaves differently. It appears to be a suboptimal solution that contradicts the principles of KISS (Keep It Simple, Stupid). Why not simply allow users to specify the data type for a specific field via context? Why use reflection to study fields and analyze field and method names? Speed and simplicity are crucial for serializers, and if we have to struggle with every variable name, then what's the point of having it? It feels like a feeble imitation of artificial intelligence. More than that now it's slower than oldest JMS serializer.

from symfony.

krzysztof-ciszewski avatar krzysztof-ciszewski commented on June 15, 2024

I agree with you that behavior should not be affected by property naming, but this is a pretty niche edge case. To encounter this you have to not use the docblocks, which(I hope) is pretty rare.

I'm afraid your assumption seems to be incorrect, and it won't work as expected

I have tested it with the docblock and it works, I can make a PR to the bug-reproducer repo if you're interested.

It is possible to add support to setting types via context or configuration, but I feel like that would get rejected due to the design differences.

from symfony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.