-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Change swagger generator to produce lowerCamelCase field names, change default marshaler to do the same, and also emit default values #540
Conversation
@alexleigh. can you help me understand something about the output? Do you know if this will change any of the generated code in substantive ways? I know this is kind of a vast question, but is there reason to expect that in python the generated output code for the field @tmc I would appreciate if you could also weigh in here. To me this seems reasonable but I will admit that it is breaking. Should we couple this with changing the default and mark this as a 2.0 release? |
For the code generated by swagger-codegen using the schema files generated by this project, the impact of this change should be relatively minimal. Much like how protoc converts snake_case protobuf field names to lowerCamelCase field names for JSON marshaling, swagger-codegen takes care to convert the lowerCamelCase field names in the JSON schema to the preferred field names in the target language. For example, in the generated Python code, there is this map:
This is a mapping from the lowerCamelCase names in JSON to the snake_case field names in Python. The situation is similar for Ruby and Go, and I imagine for other languages swagger-codegen should pick the appropriate field naming as well. You are right that this change is breaking, insofar as the generated Swagger schemas will change. I actually think another change makes sense when combined with this change: the default jsonpb marshaling in grpc-gateway should have |
@alexleigh can you do the requested change ? or do you want me to submit another PR ? I'm also in need of this. just change this
to false. |
@Kuqd there have been other attempts to do this (#508 comes to mind). It might be useful to start with their work. |
Is it related ? Not sure I follow, the point is to not output snake case as it's the standard for protobuf but camel case which is the json standard. |
Sorry, I did a poor job of communicating what my intent was. We have for a while wanted to turn things from snake_case to camelCase and turn on default value emission in the same change. They are not inherently linked but they are both breaking changes. Since they are breaking, it's my preference to do both of them in rapid succession (or at the same time) as to minimize impact on people's production systems and keep the migrations down to a single event. |
@achew22 if you prefer, I can amend this PR to change the default marshaling to use camelCase and emit default values. Currently I accomplish both of those by customizing the marshaling options. But it would be good to change the default marshaling options to follow suit. |
TBH I would love it if anyone would do it. $DAYJOB has been keeping me extremely busy of late so I haven't had time to do it. I will take care of the releasing a 2.0 if you want to go through and enable default values and camelCase |
@alexleigh you got it. Thanks a lot |
I changed the default marshaling to use camelCase and emit default fields and fixed the tests. Unfortunately, swagger-codegen 2.2.2 does not support enum fields for generated Go code (2.3.0 purports to support it but in my tests it seems broken, it generates incorrect type names that don't compile). So when default values for enum fields are emitted, swagger-codegen generated Go client code can't unmarshal the object due to the missing enums. To keep the existing tests I had to make a copy of the ABE message that has all the same fields, except for all of the enums, and adjust the client test code to use these messages instead. On the plus side this let me enable one of the previously disabled tests. |
LGTM |
Could you file a bug against swagger codegen and @mention me in it? I'd prefer to not have a duplicate ABE proto if it's at all possible. |
One of the recurring themes of this project has been trouble around the default marshaller. This change modifies it to be more what people expect when they first start the project. 1. It emits the proto3 json style version of field names instead of the field name as it appeared in the .proto file. 2. It emits zero values for fields. This means that if you have a field that is unset it will now have a value unlike before. Fixes: grpc-ecosystem#540, grpc-ecosystem#375, grpc-ecosystem#254, grpc-ecosystem#233
Do you guys have an ETA ? |
I just replied to the comment over there. That PR has all the info I've got. |
To update ppl here, the aforementioned PR is now in |
One of the recurring themes of this project has been trouble around the default marshaller. This change modifies it to be more what people expect when they first start the project. 1. It emits the proto3 json style version of field names instead of the field name as it appeared in the .proto file. 2. It emits zero values for fields. This means that if you have a field that is unset it will now have a value unlike before. Upgrade to swagger-codegen 2.4.0 Also fix a regex-o in .travis.yml. + needed to be escaped. Fixes: grpc-ecosystem#540, grpc-ecosystem#375, grpc-ecosystem#254, grpc-ecosystem#233
This seems to be superceded by #546 |
We'd be happy to reconsider this for v2. |
In the protobuf canonical JSON representation, field names are in lowerCamelCase. This change causes the swagger generator to produce schema files that correspond to the canonical JSON representation, using lowerCamelCase field names. To produce output that conforms to the generated schema, the server should marshal using
jsonpb
withOrigName: false
. The test server has been changed accordingly to succeed against the changed schema.This fixes #375.