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

[Question]Why enum response to string #3547

Open
thinkal01 opened this issue Feb 8, 2025 · 2 comments
Open

[Question]Why enum response to string #3547

thinkal01 opened this issue Feb 8, 2025 · 2 comments
Labels
question Further information is requested

Comments

@thinkal01
Copy link

enum ConfigStatus {
CONFIG_STATUS_UNSPECIFIED = 0;
CONFIG_STATUS_ENABLE = 1; // 启用
CONFIG_STATUS_DISABLED = 2; // 禁用
}

message Response {
ConfigStatus status = 2 [(validate.rules).enum = {not_in: [0],defined_only: true}];
}

http response is: {"status":"CONFIG_STATUS_ENABLE"} not {"status:1}

why do not use protojson.MarshalOptions UseEnumNumbers:true

@thinkal01 thinkal01 added the question Further information is requested label Feb 8, 2025
Copy link

dosubot bot commented Feb 8, 2025

Hey @thinkal01! I'm here to help you with any bugs or questions you have. I can assist with solving issues, answering questions, and becoming a contributor. Let me know how I can assist you!

The Kratos framework uses a custom codec implementation that defaults to JSON encoding for marshaling and unmarshaling Protobuf messages. This implementation typically uses string representation for enums in HTTP responses. This approach is often preferred because it enhances readability and makes the API responses more understandable to humans, as the string representation provides context that numeric values do not.

While the default behavior is to use string representation, you can customize this by setting protojson.MarshalOptions with UseEnumNumbers: true if you prefer numeric representation. However, this might not be the default setting in Kratos, as the framework's customizations are designed to improve clarity and maintainability of API responses [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@shenqidebaozi
Copy link
Member

I don't understand what you're really asking

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

No branches or pull requests

2 participants