OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Protobuf-Schema] Option to generate wrapper types for primitives

Open
#12,291 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Proto3 primitive types don't handle well missing values.
In Java you need to call message.hasField(MyMessage.getDescriptor().findFieldByName("myfield")) to know if a field was actually present on the wire, which is not very usable. Calling message.getField() would return the primitive default value.

With #11072 support was added for optional keyword (https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md), but it has some constraints such as fields having null type in the spec.

Another approach that could be supported is to use the built-in wrapper types or "WellKnownTypes" of Protobuf (https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/wrappers.proto)

Describe the solution you'd like

Add a codegen option <useWrapperTypes>true</useWrapperTypes> to use the google.protobuf.xxxxValue types instead of primitive types, and add the import "google/protobuf/wrappers.proto"; in the generated .proto

Describe alternatives you've considered

Allow custom type mapping between OAS types and proto types. But WellKnownTypes being part of proto coding guidelines, they make sense to be pre-set.

Additional context

I'm willing to contribute a PR with the changes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the protobuf schema generator and the code that emits generated .proto files; the issue does not name repository files or tests. Read google/protobuf/wrappers.proto to map primitive types to wrapper types, then verify that the option causes the wrappers import and corresponding generated field types to appear.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.