Skip to content

[BUG][protobuf-schema] Use of OneOf leads to redundant message creation  #14895

@dchinmaya

Description

@dchinmaya

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When OpenApi spec 3.0 OneOf is encountered by the protobuf-schema generator, an unneeded message with the field name of the oneOf is created in the output. If the field name coincides with the type name (except Uppercase starting letter) then the correct message gets overwritten with the redundant message leading to erroneous schema generation.

openapi-generator version
% openapi-generator version
6.4.0
OpenAPI declaration file content or url

test openapi spec: https://gist.github.com/dchinmaya/292097a632da30d1580542652cbfdf88

Excerpt below

        "schemas": {
            "AnalyzerSpecificData": {
                "oneOf": [
                    {
                        "type": "object",
                        "title": "oneAnalyzerKey",
                        "properties": {
                            "oneAnalyzerKey": {
                                "$ref": "#/components/schemas/OneAnalyzerData"
                            }
                        },
                        "required": [
                            "oneAnalyzerKey"
                        ]
                    },
                    {
                        "type": "object",
                        "title": "twoAnalyzerKey",
                        "properties": {
                            "twoAnalyzerKey": {
                                "$ref": "#/components/schemas/TwoAnalyzerData"
                            }
                        },
                        "required": [
                            "twoAnalyzerKey"
                        ]
                    }
                ]
            },
Generation Details

Execute

openapi-generator generate -i testapispec.json -g protobuf-schema -o genschemas
Steps to reproduce
  1. Run the above command with the linked test api spec
  2. View the created proto files
 % ls -lrth  genschemas/models/
total 48
-rw-r--r--  1 chinmay  wheel   612B Mar  6 17:00 analyzer_specific_data.proto
-rw-r--r--  1 chinmay  wheel   418B Mar  6 17:00 one_analyzer_data.proto
-rw-r--r--  1 chinmay  wheel   417B Mar  6 17:00 one_analyzer_key.proto
-rw-r--r--  1 chinmay  wheel   420B Mar  6 17:00 two_analyzer_data.proto
-rw-r--r--  1 chinmay  wheel   417B Mar  6 17:00 two_analyzer_key.proto
-rw-r--r--  1 chinmay  wheel   364B Mar  6 17:00 two_analyzer_metadata.proto
  1. The one_analyzer_key.proto (and two_analyzer_key.proto) is the redundant proto that is generated. There is no type "OneAnalyzerKey" in the spec anywhere. It is the name of a field in the OneOf of type OneAnalyzerData. There should be OneAnalyzerData and AnalyzerSpecificData but there should not be a message with OneAnalyzerKey.

one_analyzer_key.proto

syntax = "proto3";
package openapitools;
import public "models/one_analyzer_data.proto";

message OneAnalyzerKey {
  OneAnalyzerData oneAnalyzerKey = 299888019;
}

analyzer_specific_data.proto

syntax = "proto3";
package openapitools;

import public "models/one_analyzer_data.proto";
import public "models/one_analyzer_key.proto";
import public "models/two_analyzer_data.proto";
import public "models/two_analyzer_key.proto";

message AnalyzerSpecificData {
  OneAnalyzerData oneAnalyzerKey = 299888019;
  TwoAnalyzerData twoAnalyzerKey = 180391406;
}

one_analyzer_data.proto

syntax = "proto3";
package openapitools;
import public "models/todo_object_mapping.proto";

message OneAnalyzerData {
  TODO_OBJECT_MAPPING metadata = 450004177;
}

Expected

one_analyzer_key.proto to not exist. message OneAnalyzerKey to not exist.

Actual

one_analyzer_key.proto wrongly generated. message OneAnalyzerKey exists. If the field name was the same as the type except the upper case starting letter, ie if we wanted to generate OneAnalyzerData oneAnalyzerData = 123 then the above generation leads to issues and the actual OneAnalyzerData message will be overwritten with the bad OneAnalyzerData.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions