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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example and pattern annotations are not respect for query and path parameters #4130

Open
pafrench opened this issue Mar 22, 2024 · 8 comments

Comments

@pafrench
Copy link

馃悰 Bug Report

Using the sample proto below, only default appears in the output json - example and pattern do not.

To Reproduce

message GetTileForImageRequest {
	// The unique reference number of the image.
	string image_urn = 1 [(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
		example: "urn:eagleview.com:v4:spatial-data:raster:visual:ated:QIgDbfZw-kqKfTxbMQXwhw:0",
		default: "urn:eagleview.com:v4:spatial-data:raster:visual:ated:QIgDbfZw-kqKfTxbMQXwhw:0",
		pattern: "^(urn:eagleview\\.com:v\\d+:spatial-data:\\w+:\\w+:\\w+:[A-Za-z0-9-_]+):(\\d+)$",
	}];
}

I'm using buf to generate the swagger using the following template:

buf.gen.swagger.yaml:

version: v1
plugins:
  - name: openapiv2
    out: build/go
    opt:
      - json_names_for_fields=false
      - visibility_restriction_selectors=LATEST
$ buf generate --template buf.gen.swagger.yaml --path proto/evt/iw/service

Expected behaviour

{
  "name": "image_urn",
  "description": "The unique reference number of the image.",
  "in": "path",
  "required": true,
  "type": "string",
  "example": "urn:eagleview.com:v4:spatial-data:raster:visual:ated:QIgDbfZw-kqKfTxbMQXwhw:0",
  "default": "urn:eagleview.com:v4:spatial-data:raster:visual:ated:QIgDbfZw-kqKfTxbMQXwhw:0",
  "pattern": "^(urn:eagleview\\.com:v\\d+:spatial-data:\\w+:\\w+:\\w+:[A-Za-z0-9-_]+):(\\d+)$"
}

Actual Behavior

{
  "name": "image_urn",
  "description": "The unique reference number of the image.",
  "in": "path",
  "required": true,
  "type": "string",
  "default": "urn:eagleview.com:v4:spatial-data:raster:visual:ated:QIgDbfZw-kqKfTxbMQXwhw:0"
}

Your Environment

$ uname -a
Linux 0664eeb787b5 5.14.0-1049-oem #56-Ubuntu SMP Fri Aug 12 10:23:08 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ protoc --version
libprotoc 25.3
Package Version
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway v2.19.1
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 v2.19.1
google.golang.org/protobuf/cmd/protoc-gen-go v1.32.0
@johanbrandhorst
Copy link
Collaborator

Hm, that's odd, in our example protobuf file you can see that the example and pattern both make it into the generated file:

message ErrorResponse {
string correlationId = 1 [(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
pattern: "^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$",
title: "x-correlation-id",
description: "Unique event identifier for server requests",
format: "uuid",
example: "\"2438ac3c-37eb-4902-adef-ed16b4431030\""
}];
ErrorObject error = 2;
}

"examplepbErrorResponse": {
"type": "object",
"properties": {
"correlationId": {
"type": "string",
"format": "uuid",
"example": "2438ac3c-37eb-4902-adef-ed16b4431030",
"description": "Unique event identifier for server requests",
"title": "x-correlation-id",
"pattern": "^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$"
},
"error": {
"$ref": "#/definitions/examplepbErrorObject"
}
}

Could you provide a test repo that reproduces the issue? Or better yet, make a contribution to our repo that exhibits the issue.

@pafrench
Copy link
Author

Please find attached a zip of a sample repo. that reproduces the issue.
imagery-warehouse-api-public-lite.zip

@johanbrandhorst
Copy link
Collaborator

Thank you, with all due respect, I'm not going to download a zip file from a stranger on the internet. Could you please provide a github repository or gist or something where I can view the contents? Thanks.

@pafrench
Copy link
Author

I've created a private repo here, and have sent you an invite.

@johanbrandhorst
Copy link
Collaborator

Sorry, that invitation expired, my bad, could you send it again please?

@johanbrandhorst
Copy link
Collaborator

OK I can reproduce the issue. Seems like we only render the example when the field in question is used in the request body, not when it's part of the path or the query. I think we should be able to support including the example and other fields for non-body fields too.

@johanbrandhorst johanbrandhorst changed the title Some openapiv2_field options don't generate anything Example and pattern annotations are not respect for query and path parameters Apr 7, 2024
@johanbrandhorst
Copy link
Collaborator

Would you be interested in contributing a fix for this?

@paul-french-ev
Copy link

paul-french-ev commented Apr 10, 2024

Would you be interested in contributing a fix for this?

I'd like to but honestly I don't have the spare time outside of work. However, we do have time set aside to work on innovation (exploration tasks). I could potentially work on this then (but we've just finished this iteration's task, so the next will be in a month or so).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants