Giter Site home page Giter Site logo

Comments (8)

oprypkhantc avatar oprypkhantc commented on May 19, 2024 1

Just stumbled upon this myself. They work with native default values IF they come after ones without default values.

from serde.

Crell avatar Crell commented on May 19, 2024 1

Resolved in #31. I'm shocked that bug managed to survive so long, honestly. I could have sworn there was a test for that case. But it's been resolved, and supports null as a default value, too. (Which required some fun enum dancing.)

from serde.

Crell avatar Crell commented on May 19, 2024 1

The Field default should work for any field type. SequenceField et al are TypeFields, which are type-specific sub-attributes. You absolutely can and should use both Field and SequenceField on the same property.

from serde.

pqt-edannenberg avatar pqt-edannenberg commented on May 19, 2024

Sprinkled some debugs into the Serde source and for the test case above the shouldUseDefault property currently always evaluates to false. After forcing it to true and using a default value other than null it worked as expected.

from serde.

Crell avatar Crell commented on May 19, 2024

The tests already cover a whole bunch of default values. Is this limited to when the default value being set is null, perhaps?

from serde.

Crell avatar Crell commented on May 19, 2024

Also, try on the master branch. There's been a lot of improvements around null handling recently that may have already resolved this.

from serde.

pqt-edannenberg avatar pqt-edannenberg commented on May 19, 2024

The tests already cover a whole bunch of default values. Is this limited to when the default value being set is null, perhaps?

No luck. Tested with current master:

class Foo
{
    public function __construct(
        #[Field(default: "42")]
        public string $bar,
        public string $name
    ) {
        //
    }
}

$serde = new SerdeCommon();
dump($serde->deserialize(['name' => 'foobar'], 'array', Foo::class));

Foo {#6350
  +name: "foobar"
}
$ composer show -t crell/serde
crell/serde dev-master A general purpose serialization and deserialization library
├──crell/attributeutils ~0.8.2
│  ├──crell/fp ~0.4.0
│  │  └──php ~8.1
│  └──php ~8.1
├──crell/fp >= 0.3.3
│  └──php ~8.1
└──php ~8.1

from serde.

pqt-edannenberg avatar pqt-edannenberg commented on May 19, 2024

Thanks for the swift fix! One more thing I noticed while working with defaults, would it make sense to also add a default arg to SequenceField and DictionaryField? It felt a bit inconsistent to me.

from serde.

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.