question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`oneof` generated class does not contain `enum` types

See original GitHub issue

Hi, I was playing with oneof keyword and found out that the generated class seems not following the doc. on https://developers.google.com/protocol-buffers/docs/reference/java-generated#oneof . For example, if i have the follow in .proto:

message Message{
    oneof payload{
        int32 foo_int =4;
        string foo_string =9;
    }
}

it generates something like:


  @WireField(
      tag = 4,
      adapter = "com.squareup.wire.ProtoAdapter#INT32"
  )
  public final Integer foo_int;

  @WireField(
      tag = 9,
      adapter = "com.squareup.wire.ProtoAdapter#STRING"
  )
  public final String foo_string;

but not the enum types as I expected;

And with that, it seems the only way I can know what “type” of my payload is checking foo_int and foo_string to see which one is not null. But what about if I want have adding more types in oneof block, is there a quicker way to know the “type” of my payload?

Thanks in advance

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
swankjessecommented, Sep 14, 2016

I’m looking at this API and I don’t see the improvement. Presumably this is where you’re starting from:

  if (message.foo_int != null) {
    doIntThings(message.foo_int);
  } else if (message.foo_string != null) {
    doStringthings(message.foo_string);
  } else {
    doNothing();
  }

The proposed API gets you here:

  Payload<?> payload = message.payload;
  if (payload.type == Integer.class) {
    doIntThings((Integer) payload.field);
  } else if (payload.type == String.class) {
    doStringthings((String) payload.field.);
  } else {
    doNothing();
  }

I think the closest we get with oneof that’s an improvement is a visitor, and it’s not really much better.

0reactions
alecmuffettcommented, Nov 13, 2020

Hello All - I just found this, am wondering if there has been any change, since?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use OpenAPI "oneOf" property with openapi-generator ...
I have been battling to generate a PHP client for an API that heavily uses anyOf and oneOf. After some googling and finding...
Read more >
oneOf, anyOf, allOf, not - Swagger
Besides these, there is a not keyword which you can use to make sure the value ... type: object; properties: bark: type: boolean;...
Read more >
Handbook - Enums - TypeScript
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define...
Read more >
Documentation for the java Generator
Do not annotate Model and Api with complementary annotations. ... Annotate Model and Api using the Swagger Annotations 1.x library. none.
Read more >
Enum Types - Java™ Tutorials
An enum type is a special data type that enables for a variable to be a set of ... The variable must be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found