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

[+] support integer64 as string option #355

Merged
merged 2 commits into from
May 10, 2022
Merged

[+] support integer64 as string option #355

merged 2 commits into from
May 10, 2022

Conversation

ppaanngggg
Copy link
Contributor

as https://developers.google.com/protocol-buffers/docs/proto3#json

In fact, int64, fixed64, uint64 should be string in json.

So add option to turn int64, fixed64, uint64 to string in openapi

@ppaanngggg ppaanngggg requested a review from a team as a code owner May 6, 2022 13:19
Copy link
Collaborator

@morphar morphar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ppaanngggg looks good 👍

Maybe 64 bit types should default to string as described in the link.
What are your thoughts on this @timburks?

cmd/protoc-gen-openapi/main.go Outdated Show resolved Hide resolved
for _, tt := range openapiTests {
fixture := path.Join(tt.path, "openapi_string_integer64.yaml")
if _, err := os.Stat(fixture); errors.Is(err, os.ErrNotExist) {
if !GENERATE_FIXTURES {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doe this have any relevance to gnostic? Or is it something used locally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I don't understand, I just copy from other unit tests

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I thought GENERATE_FIXTURES was new.

if err != nil {
t.Fatalf("protoc failed: %+v", err)
}
if GENERATE_FIXTURES {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above.

@ppaanngggg
Copy link
Contributor Author

Maybe 64 bit types should default to string as described in the link.

I think it should be in the future. Because protojson works like this.

@timburks
Copy link
Contributor

timburks commented May 7, 2022

@ppaanngggg Thanks for catching this! I would prefer to just make this the correct behavior and not have an option. @morphar do you need the old behavior?

@ppaanngggg
Copy link
Contributor Author

@timburks I think it should be default.

I just concern about it will break something, afterall I am not familiar with other things in this project.

@morphar
Copy link
Collaborator

morphar commented May 8, 2022

@morphar do you need the old behavior?

@timburks no. I will always go for "most correct behavior".
It seems that envoy is extremely strict with regards to interpreting int64 as strings as well:
protocolbuffers/protobuf#5015

Note: I haven't tested this though. Envoy may allow int values as input for int64.

@timburks
Copy link
Contributor

timburks commented May 8, 2022

@ppaanngggg let's please make this fix the new behavior (with no option). The link @morphar cited is a good example for us to follow in keeping things simple and using https://developers.google.com/protocol-buffers/docs/proto3#json as our standard.

@ppaanngggg thanks for finding and fixing this!

@ppaanngggg
Copy link
Contributor Author

ppaanngggg commented May 8, 2022

Envoy may allow int values as input for int64.

According to doc before, protojson allow both integer and string input, but only string output.

@timburks Done

case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind,
protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind:
kindSchema = wk.NewStringSchema()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great - such a clear and simple change! Thanks!

@timburks timburks merged commit c62333b into google:main May 10, 2022
@ppaanngggg ppaanngggg deleted the feat/integer64 branch May 28, 2022 03:23
@ajorgensen
Copy link

ajorgensen commented Mar 7, 2024

Would it be possible to consider reverting this change? My understanding is that the protoc-gen-openapi tool is intended to generate an OpenAPI v3 specification from a protobuf definition. The specification is merely a definition and does not directly relate to json or javascript and its limitations regarding int64 handling. OpenAPI v3 specifications, as generated by this tool, explicitly allow the integer format of int64 (refer to https://swagger.io/docs/specification/data-models/data-types/#numbers).

I believe the guidance for protobuf to json encoding is irrelevant here, it's important to recognize that the code in question is not encoding a protobuf message as json, but rather generating an OpenAPI v3 specification from a protobuf definition which can then be used to generate both server and client code in any number of different languages each of which will have its own way of knowing how it needs to handle int64. As the protoc-gen-openapi generator is written now, you completely lose the type information for int64 integers meaning that the generated code could not make a determination on how to handle the value over the wire. It is the responsibility of the code generated from the OpenAPI v3 spec to determine how to handle the value over the wire, not the responsibility of the spec definition which this tool is generating.

I would be happy to take on the work of reviewing and reverting this change either in totality or in part to restore the functionality of representing int64 data types as integers within the generated v3 specifications. However, I would like to emphasize that this original change seems to be confusing the generation of a OpenAPI v3 spec definition with guidance for serializing a protobuf message as json.

ajorgensen added a commit to ajorgensen/gnostic that referenced this pull request Mar 7, 2024
This reverts commit c62333b.

The original change conflated the guidance for encoding protobuf
messages as json objects with generating an OpenAPI v3 spec file.
OpenAPI v3 explicitly allows for integer types of int64
(https://swagger.io/docs/specification/data-models/data-types/#numbers).
The OpenAPI v3 spec generated by this tool will then usually be fed into
language specific generators which will determine how they want to
handle each of the types depending on the specific target language.
ajorgensen added a commit to ajorgensen/gnostic that referenced this pull request Mar 7, 2024
This reverts commit c62333b.

The original change conflated the guidance for encoding protobuf
messages as json objects with generating an OpenAPI v3 spec file.
OpenAPI v3 explicitly allows for integer types of int64
(https://swagger.io/docs/specification/data-models/data-types/#numbers).
The OpenAPI v3 spec generated by this tool will then usually be fed into
language specific generators which will determine how they want to
handle each of the types depending on the specific target language.
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

Successfully merging this pull request may close these issues.

None yet

4 participants