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

protoc-gen-openapi: Select the correct schemas that correspond to the messages used #414

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zaakn
Copy link

@zaakn zaakn commented Nov 9, 2023

fixes #392

Before fixing this issue, if fq_schema_naming = false and there were duplicates of schema names traversed and formatted from all imported proto files, the wrong message could be selected, even if it was not actually used.

Now, when a message of fully-qualified schema named foo.Contact is going to be generatd, at the same time, a duplicate short schema like Contact formatted from bar.Contact is found (used), so the fully-qualified foo.Contact will be used.


sample:

# bar.proto
syntax = "proto3";

package bar;

option go_package = "example.com/pkg/barpb";

import "google/api/annotations.proto";
import "foo.proto";

message GetBookReqest {
	string id = 1;
}

message Book {
	string id = 1;
	string name = 2;
	string shelf = 3;
}

service Library {
	rpc GetBook(GetBookReqest) returns (Book) {
		option (google.api.http) = {
			get: "/book/bar"
		};
	}
	rpc GetFooBook(GetBookReqest) returns (foo.Book) {
		option (google.api.http) = {
			get: "/book/foo"
		};
	}
}
# foo.proto
syntax = "proto3";

package foo;

option go_package = "example.com/pkg/foopb";

message Book {
	string id = 1;
	string name = 2;
	string publisher = 3;
}

protoc arguments:
--openapi_opt naming=proto,fq_schema_naming=false,default_response=false

Before:
# ellipsis ...
paths:
    /book/bar:
        get:
            # ellipsis ...
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Book'
    /book/foo:
        get:
            # ellipsis ...
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Book'
components:
    schemas:
        Book:
            type: object
            properties:
                id:
                    type: string
                name:
                    type: string
                publisher:
                    type: string

After:

# ellipsis ...
paths:
    /book/bar:
        get:
            # ellipsis ...
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Book'
    /book/foo:
        get:
            # ellipsis ...
            responses:
                "200":
                    description: OK
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/foo.Book'
components:
    schemas:
        Book:
            type: object
            properties:
                id:
                    type: string
                name:
                    type: string
                shelf:
                    type: string
        foo.Book:
            type: object
            properties:
                id:
                    type: string
                name:
                    type: string
                publisher:
                    type: string

@zaakn zaakn requested a review from a team as a code owner November 9, 2023 15:00
@zaakn zaakn changed the title protoc-gen-openapi: Select the corret schemas that correspond to the messages used protoc-gen-openapi: Select the correct schemas that correspond to the messages used Nov 9, 2023
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.

[proto-gen-openapi] Wrong message included when message with same name exists in imported proto
1 participant