sudorandom / protoc-gen-connect-openapi

Plugin for generating OpenAPIv3 from protobufs matching the Connect RPC interface
MIT License
102 stars 7 forks source link

Use json fields instead of proto fields #34

Closed pcruz7 closed 1 month ago

pcruz7 commented 1 month ago

Hello!

Is there a way to use JSON fields instead of proto fields?

Many thanks for the generator

sudorandom commented 1 month ago

For the actual property name, JSON field names are already used. So this messages:

message ParameterValues {
  double double_value = 1;
}

will yield the following name specification:

components:
  schemas:
    test.v1.ParameterValues:
      properties:
        doubleValue:
          oneOf:
            - type: string
            - type: number
          title: double_value

Note that "doubleValue" is the actual property name being used. It maintains the original field name from protobuf only for the "title", which is a descriptive field. Is the title what you want to change? I feel like the protobuf naming here is more appropriate because it tells you slightly more about the property, which is already "named" using the JSON naming scheme.

Does that help? Did you hit a specific issue related to this? If so, can you describe what's happening for you?

pcruz7 commented 1 month ago

Hello,

First of all thank you for the quick reply!

I didn't explain correctly, so apologies beforehand.

What I was referring to was the properties "doubleValue" and I was asking if it was possible to change to use the json name instead.

Basically instead of supporting a JSON with

{"doubleValue":"1.0"}

Is if it could be instead

{"double_value":"1.0"}

And have that documented that way

Thanks in advance!

sudorandom commented 1 month ago

The way protobuf represents JSON names is typically with camelCase, not with lower_snake_case, at least by default. See more on this here: https://protobuf.dev/programming-guides/proto3/#json-options, specifically:

By default proto3 JSON printer should convert the field name to lowerCamelCase and use that as the JSON name. An implementation may provide an option to use proto field name as the JSON name instead. Proto3 JSON parsers are required to accept both the converted lowerCamelCase name and the proto field name.

If there's an json_name option of the field, like this, it should already be respected by this plugin:

message Message {
  string description = 1 [json_name = "json_description"];
}
      properties:
        json_description:
          type: string
          title: description

All of this is to say, I think this might be a good option to have but I think the way you're phrasing it is backwards. "What I was referring to was the properties "doubleValue" and I was asking if it was possible to change to use the json name instead." The "JSON name" is already being used. You want an option to use the protobuf field names instead of the JSON names.

sudorandom commented 1 month ago

v0.11.0 has a new option, with-proto-names, which does the following: "Use protobuf field names instead of the camelCase JSON names for property names."

pcruz7 commented 1 month ago

thank you @sudorandom !