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

Placement for unique items in array type is located in the wrong place #4614

Closed
venezia opened this issue Aug 8, 2024 · 1 comment · Fixed by #4619
Closed

Placement for unique items in array type is located in the wrong place #4614

venezia opened this issue Aug 8, 2024 · 1 comment · Fixed by #4619

Comments

@venezia
Copy link

venezia commented Aug 8, 2024

🐛 Bug Report

This bug relates to protoc-gen-openapiv2.

It looks like uniqueItems option with array type is placed in the wrong place when compiled to json. Current formatting places the field within the items field of the property object. But according to the spec: it should be in the root property object.

The problem is located here: https://github.com/grpc-ecosystem/grpc-gateway/blob/main/protoc-gen-openapiv2/internal/genopenapi/template.go#L2939 - where the unique field is placed within the items object instead of the root object

To Reproduce

  1. Create in your proto a repeated string field with annotations like:
  repeated string url = 1 [
    (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
      min_length: 1,
      max_length: 512,
      max_items: 3
      unique_items: true,
      description: "A cool set of URLs",
      example: "[\"https://www.example.com/\", \"https://www.github.com/\"]",
  }];
  1. Run the generator
  2. See that the output has:
"url": {
  "type": "array",
  "example": [
    "https://www.example.com/",
    "https://www.google.com/"
  ],
  "items": {
    "type": "string",
    "maxLength": 512,
    "minLength": 1
    "uniqueItems": true,
  },
  "description": "a cool set of URLs",
  "maxItems": 3
},

Expected behavior

"url": {
  "type": "array",
  "example": [
    "https://www.example.com/",
    "https://www.google.com/"
  ],
  "items": {
    "type": "string",
    "maxLength": 512,
    "minLength": 1
  },
  "uniqueItems": true,
  "description": "a cool set of URLs",
  "maxItems": 3
},

Using the rendered swagger output in editor.swagger.io I would expect to see an error when I put in two duplicate entries

Actual Behavior

Using the rendered swagger output in editor.swagger.io I see no error when I put in two duplicate entries, manually adjusting the output to the "expected output" I do get an error from editor.swagger.io

Your Environment

Was using grpc gateway 2.19.1

@johanbrandhorst
Copy link
Collaborator

Yep, this seems wrong, thanks for the report! Would you like to contribute a fix? Looks like we need to lift UniqueItems into openapiParameterObject: https://github.com/grpc-ecosystem/grpc-gateway/blob/main/protoc-gen-openapiv2/internal/genopenapi/types.go#L154 and remove it from openapiItemsObject.

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

Successfully merging a pull request may close this issue.

2 participants