OpenAPITools / OpenAPITools/openapi-generator

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

Open
#14,895 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the protobuf-schema generator with the linked test OpenAPI specification and inspect the generated models, especially one_analyzer_key.proto and analyzer_specific_data.proto. Trace how oneOf fields become messages, then verify that the redundant OneAnalyzerKey file and message are absent while the intended AnalyzerSpecificData and OneAnalyzerData messages remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.