Giter VIP home page Giter VIP logo

Comments (3)

goodov avatar goodov commented on June 20, 2024

Can confirm. toBinary and toJson doesn't output default values for required fields when we use proto2.

Is this a bug or intended behavior? This should be solvable somehow. One approach is to made an option for those two functions to serialize required values even in the default state.

from protobuf-ts.

jcready avatar jcready commented on June 20, 2024

There is only partial support for proto2. The required label is not supported. At least for toJson there is an option to emitDefaultValues, but there is no such option for toBinary.

from protobuf-ts.

goodov avatar goodov commented on June 20, 2024

The info on partial support for proto2 is a bit outdated. The library is able to serialize and deserialize such fields without issues, there's no any bool fallback.

From my experiments with other TS libraries this one is a clear winner, but the inability to configure default value writes to support proto2 clients is a deal breaker.

There was actually a very similar request to support this case some time ago: #194

I can think of few approaches to make this work:

  1. Support this as a flag in BinaryWriteOptions. This is pretty invasive, adds unnecessary runtime bloat that's not needed for most scenarios, but is clear and understandable.
  2. Support this in protoc plugin with a flag like always_emit_default_values. This is a little bit hacky. The idea here is to keep TS interfaces as is (do not add ? to fields), but:
    • modify generated serializers to use !== undefined check instead of !== 0 and != "";
    • add opt: true for reflection serializers to do the same.

The protoc flag looks like a solid solution to support this edge case for users who are forced to support proto2 in their projects, and at the same time don't intervene in the usual proto3 use cases of this library. Thoughts?

from protobuf-ts.

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.