gnostic icon indicating copy to clipboard operation
gnostic copied to clipboard

Handle google/protobuf/wrappers.proto types on protoc-gen-openapi

Open galihputera opened this issue 3 years ago • 2 comments

According to https://developers.google.com/protocol-buffers/docs/proto3#json, Wrapper Types use the same representation in JSON as the wrapped primitive type. On this MR I Modify protoc-gen-openapi code to be able to parse Wrapper Types on schema and parameters.

Also in this MR is changes to map google.protobuf.Timestamp field into string when used as URL query parameters. Previously it will be mapped into two separate fields, for example timestamp_type will mapped into timestampType.seconds and timestampType.nanos image

Manual Testing

Unit Test

Steps

make all
go test ./...

Result

ok      github.com/google/gnostic       0.702s
?       github.com/google/gnostic/cmd/disco     [no test files]
?       github.com/google/gnostic/cmd/parse-linter-output       [no test files]
ok      github.com/google/gnostic/cmd/petstore-builder  0.468s
ok      github.com/google/gnostic/cmd/protoc-gen-jsonschema     0.745s
?       github.com/google/gnostic/cmd/protoc-gen-jsonschema/generator   [no test files]
ok      github.com/google/gnostic/cmd/protoc-gen-openapi        1.900s
?       github.com/google/gnostic/cmd/protoc-gen-openapi/generator      [no test files]
?       github.com/google/gnostic/cmd/protoc-gen-openapi/generator/wellknown    [no test files]
?       github.com/google/gnostic/cmd/report    [no test files]
?       github.com/google/gnostic/cmd/report-messages   [no test files]
?       github.com/google/gnostic/cmd/vocabulary-operations     [no test files]
ok      github.com/google/gnostic/compiler      0.110s
?       github.com/google/gnostic/conversions   [no test files]
ok      github.com/google/gnostic/discovery     0.375s
ok      github.com/google/gnostic/extensions    0.302s
?       github.com/google/gnostic/extensions/sample/generated/gnostic-x-sampleone       [no test files]
?       github.com/google/gnostic/extensions/sample/generated/gnostic-x-sampleone/proto [no test files]
?       github.com/google/gnostic/extensions/sample/generated/gnostic-x-sampletwo       [no test files]
?       github.com/google/gnostic/extensions/sample/generated/gnostic-x-sampletwo/proto [no test files]
ok      github.com/google/gnostic/generate-gnostic      1.077s
?       github.com/google/gnostic/jsonschema    [no test files]
ok      github.com/google/gnostic/jsonwriter    1.077s
?       github.com/google/gnostic/lib   [no test files]
?       github.com/google/gnostic/linters/go/gnostic-lint-descriptions  [no test files]
?       github.com/google/gnostic/linters/go/gnostic-lint-paths [no test files]
?       github.com/google/gnostic/metrics       [no test files]
?       github.com/google/gnostic/metrics/lint  [no test files]
?       github.com/google/gnostic/metrics/metrics       [no test files]
ok      github.com/google/gnostic/metrics/rules 0.779s
ok      github.com/google/gnostic/metrics/sourceinfo    0.927s
ok      github.com/google/gnostic/metrics/vocabulary    0.773s
ok      github.com/google/gnostic/openapiv2     0.596s
ok      github.com/google/gnostic/openapiv3     0.500s
?       github.com/google/gnostic/openapiv3/schema-generator    [no test files]
ok      github.com/google/gnostic/plugins       0.453s
?       github.com/google/gnostic/plugins/gnostic-analyze       [no test files]
?       github.com/google/gnostic/plugins/gnostic-analyze/statistics    [no test files]
?       github.com/google/gnostic/plugins/gnostic-analyze/summarize     [no test files]
ok      github.com/google/gnostic/plugins/gnostic-complexity    0.214s
?       github.com/google/gnostic/plugins/gnostic-linter        [no test files]
?       github.com/google/gnostic/plugins/gnostic-plugin-request        [no test files]
?       github.com/google/gnostic/plugins/gnostic-process-plugin-response       [no test files]
?       github.com/google/gnostic/plugins/gnostic-summary       [no test files]
ok      github.com/google/gnostic/plugins/gnostic-vocabulary    0.166s
?       github.com/google/gnostic/printer       [no test files]
?       github.com/google/gnostic/surface       [no test files]
?       github.com/google/gnostic/tools/format-schema   [no test files]
?       github.com/google/gnostic/tools/j2y2j   [no test files]

Check generated openapi using Swagger UI

Command

cd cmd/protoc-gen-openapi
protoc -I ../../ -I ../../third_party -I examples examples/tests/protobuftypes/message.proto --openapi_out=naming=proto:.
docker run -p 8080:8080 -e SWAGGER_JSON=/api.yaml -v $(pwd)/openapi.yaml:/api.yaml swaggerapi/swagger-ui

Result on Query String

image

Result on Schema

image

galihputera avatar Aug 31 '22 04:08 galihputera

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

google-cla[bot] avatar Aug 31 '22 04:08 google-cla[bot]

Bump on this. Can a maintainer please review?

chaayac avatar Sep 16 '22 23:09 chaayac

up

galihputera avatar Sep 27 '22 04:09 galihputera

I signed it!

galihputera avatar Dec 02 '22 09:12 galihputera

Hi @galihputera

Sorry for the slow response. This looks good! Thank you!

Have you tested this against Envoy or something like that? I don't have a test setup running, but I will get one up and test this with Envoy, just to verify that it converts correctly.

morphar avatar Dec 10 '22 07:12 morphar

Hi @galihputera

Sorry for the slow response. This looks good! Thank you!

Have you tested this against Envoy or something like that? I don't have a test setup running, but I will get one up and test this with Envoy, just to verify that it converts correctly.

Hey,

This has nothing to do with envoy. Envoy works with the json representation of the protobuf which by design has to work that way. (i.e. you don't need to set up envoy - just invoke "to_json" on the protobuf and you'll see that these values are translated directly into strings)

You can also see it in the official documentation of wrappers.proto: https://github.com/google/gnostic/blob/3751138311725a0e9d353b805b2e121c94155d18/third_party/google/protobuf/wrappers.proto#L111

// The JSON representation for `StringValue` is JSON string.

same-id avatar Dec 20 '22 14:12 same-id

👍 this looks great - I came here looking for the Timestamp conversion - so glad I found this PR.

Haven't tested the wrapper types with envoy - but I can confirm that for Timestamp <> String it works as advertised, we've been using it in our GRPC APIs for years, with the official ESPv2 json-grpc transcoder. https://github.com/google/gnostic/blob/3751138311725a0e9d353b805b2e121c94155d18/third_party/google/protobuf/timestamp.proto#L108-L147

Same is valid for Duration.

aditanase avatar Jan 22 '23 08:01 aditanase

Thanks, @timburks for your approval! but I still couldn't merge it. Could you help me do that?

galihputera avatar Mar 24 '23 15:03 galihputera

@galihputera done, thank you!

timburks avatar Mar 24 '23 17:03 timburks