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

Automatically annotate using x-go-type #4927

Open
NPellet opened this issue Nov 8, 2024 · 2 comments
Open

Automatically annotate using x-go-type #4927

NPellet opened this issue Nov 8, 2024 · 2 comments

Comments

@NPellet
Copy link

NPellet commented Nov 8, 2024

🚀 Feature

In a way I'm a bit surprised this hasn't been done / hasn't been requested.

Given the content of the proto file, it would be quite easy to generate the x-go-type annotation automatically for the OpenAPIv2 spec file, such that go-swagger can re-use the go type and not regenerate a new one. We use a monorepo, so for us this is super powerful.

I've managed to get it going using

option go_package = "<path/to/my/package>";

message <TypeName> {
   option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_schema) = {
        json_schema: {
          extensions: {
            key: "x-go-type"
            value: {
                struct_value: { 
                    fields: {
                        key: "type",
                        value: { string_value: "<TypeName>" }
                    },
                    fields: {
                        key: "import",
                        value: { 
                            struct_value: {
                                fields: {
                                    key: "package",
                                    value: { string_value: "<path/to/my/package>" }
                                } 
                            }
                        }
                    }
                }
            }
          }
        }
      }
}

So I guess I'm curious as to whether this was considered / rejected, and if PRs would be welcome here.

We wouldn't want to pollute JsonSchema of course, so I'd suggest this could go through another annotation.

@johanbrandhorst
Copy link
Collaborator

Hi Norman, thanks for your issue! I don't remember this having been requested before. I think it could make sense as a feature to save users having to supply these annotations themselves everywhere, but it should be behind a generator option, so it doesn't add annotations to users that don't want it. Would you be interested in helping implement this?

@NPellet
Copy link
Author

NPellet commented Nov 9, 2024

A generator option would be everything or nothing, though... Oh well I guess it would be enough

But the problem I will hit while doing so is that go-swagger expects the type to implement Validate(formats strfmt.Registry) error (instead of interface-checking at runtime), while [protoc-gen-validate](https://github.com/bufbuild/protoc-gen-validate) provides Validate() error

Ah that's a bit annoying. Not directly related to this issue though. I'll see if I can get go-swagger to assert on the interface instead

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

2 participants