Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum Types are serialized into numerics instead String #10

Open
ichinna opened this issue Jun 15, 2021 · 4 comments
Open

Enum Types are serialized into numerics instead String #10

ichinna opened this issue Jun 15, 2021 · 4 comments

Comments

@ichinna
Copy link

ichinna commented Jun 15, 2021

I have below Protobuf mapping and I am trying to use this as a response type of my REST API and then generating OpenAPI (Swagger). And also trying to use ProtobufPropertiesModule to exclude unwanted/additions payload within Protobuf lib.

Protobuf schema

enum StandardResourceBulkType {
  NORMALIZED = 0;
  NATIVE = 1;
}


message StandardResourceMetadata {
  StandardResourceBulk bulk = 1;
  StandardResourceEvent events = 2;
  bool hasCustomFieldDiscovery = 3;
  StandardResourceMetadataMethod method = 4;
  repeated string primaryKey = 5;
  repeated string searchableJoins = 6;
  bool hasSearchables = 7;
}

message StandardResourceBulk {
  bool hasBulk = 1;
  repeated StandardResourceBulkType bulkTypes = 2;
}

So as per the above mappings bulkTypes is an array of enums. But when I serialize this I am having json like below

{
  "bulk": {
    "type": "object",
    "properties": {
      "hasBulk": {
        "type": "boolean"
      },
      "bulkTypes": {
        "type": "array",
        "items": {
          "type": "integer",
          "format": "int32"
        }
      }
    }
  }
}

But, I am expecting something like below

{
  "bulk": {
    "type": "object",
    "properties": {
      "hasBulk": {
        "type": "boolean"
      },
      "bulkTypes": {
        "type": "array",
        "items": {
          "type": "stirng"
        }
      }
    }
  }
}

I tried different ways to override the Jackson serialization within ProtobufPropertiesModule, but nothing worked, and I am exhausted now. Please let me know if there is a way to serialize bulkTypes as I mentioned above.

Thanks in advance!

@ichinna ichinna changed the title Enum Types are serialized into numerics instead String as their original value Enum Types are serialized into numerics instead String Jun 15, 2021
@xehonk
Copy link
Collaborator

xehonk commented Jun 17, 2021

I was able to reproduce this, but I don't currently have any solution.
The field for an enum list in the protobuf class is an integer list, that's why it's serialized as such by jackson.

See ProtobufPropertiesModule.protobufBeanDescription. The code below is able to detect the case, but I'm not sure how to make jackson return the correct type information.

if (p.hasField() && p.getField().getType().hasContentType() && p.getField().getType().getContentType().isTypeOrSubTypeOf(Integer.class)) {
	if (types.get(name).getJavaType() == FieldDescriptor.JavaType.ENUM) {
		// how to make this return a enum[] array rather than an int[]?
	}
}

@ichinna
Copy link
Author

ichinna commented Jun 18, 2021

@xehonk thanks for the firm reply. I fell into the same path but failed to parse the type to enum-strings. I tried to follow the same process as you did for String types by using @JsonFormat(shape = Shape.STRING) annotation, which is a property/field level annotation in this case. But to parse enums as strings we need to have @JsonFormat(shape = Shape.OBJECT) as a class-level annotation on top of actual enums type, in my case StandardResourceBulk.

Looking for an approach to add this annotation at the class level 🤔

@xehonk
Copy link
Collaborator

xehonk commented Jun 18, 2021

It might be possible to instead return a BeanPropertyDefinition with getter and setter (as those have the right types) and without the field itself.

new POJOPropertyBuilder(...)
 .addGetter
 .addSetter

@xehonk
Copy link
Collaborator

xehonk commented Jun 18, 2021

The commit aove works only one way so far. The return value is displayed properly, but when used as a parameter it completely is empty now. Not sure why.
But maybe you can have a look yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants