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

Code generated by C# generator does not compile for OpenAPI schemas with polymorphism/inheritance (version 13.15.10) #3993

Open
danglund opened this issue Apr 28, 2022 · 1 comment

Comments

@danglund
Copy link

danglund commented Apr 28, 2022

I am trying to consume an OpenAPI 3.0 that is defined by a vendor. Thus, I don't have the option to modify the OpenAPI specification. My goal is to generate a .NET Core C# client based on their specification. In Visual Studio I have added the vendor's OpenAPI specification which generates the code for the C# client. The generated code refers to a class called Anonymous that is undefined. Thus, the code does not compile unless I stub these Anonymous classes.

Here is a simplified OpenAPI specification that reproduces the problem. I downloaded the NSwag code and added a unit test to verify the behavior I see. The API contains the following types: Pet, Value, SingleValue, MultiValue, ValueEntry.

{
  "openapi": "3.0.0",
  "components": {
    "schemas": {

      "Pet": {
        "type": "object",
        "properties": {
          "values": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/components/schemas/Value"
            }
          }
        }
      },

      "Value": {
        "type": "object",
        "oneOf": [
          {
            "$ref": "#/components/schemas/SingleValue"
          },
          {
            "$ref": "#/components/schemas/MultiValue"
          }
        ]
      },

      "SingleValue": {
        "type": "object",
        "properties": {
          "value": {
            "$ref": "#/components/schemas/ValueEntry"
          }
        },
        "additionalProperties": false
      },

      "MultiValue": {
        "type": "object",
        "properties": {
          "values": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ValueEntry"
            }
          }
        },
        "additionalProperties": false
      },

      "ValueEntry": {
        "type": "object",
        "properties": {
          "value": {
            "type": "string"
          }
        }
      }

    }
  }
}

Here is the unit test I have created.

        [Fact]
        public async Task When_generating_csharp_with_inheritance_code_is_generated_without_anonymous_classes_petstore_4_example()
        {
            // Arrange
            var json = await File.ReadAllTextAsync(@".\Data\petstore-4.json");
            try
            {
                var document = await OpenApiDocument.FromJsonAsync(json);
                
                // Act
                var settings = new CSharpClientGeneratorSettings();
                var generator = new CSharpClientGenerator(document, settings);
                var code = generator.GenerateFile();
                
                // Assert
                Assert.Contains("public partial class Pet", code);
                Assert.Contains("public partial class Value", code);
                Assert.Contains("public partial class SingleValue", code); 
                Assert.Contains("public partial class MultiValue", code);
                Assert.Contains("public partial class ValueEntry", code);

                Assert.DoesNotContain("public System.Collections.Generic.IDictionary<string, Anonymous> Values", code);
            }
            catch (Exception e)
            {
                var msg = e.Message;
            }
        }

I would expect the generated code to contain SingleValue, which it doesn't. Then, more critically, it generates a dictionary of type <string, Anonymous> and this fails to compile because Anonymous is unknown.

Does my mock API make sense? It replicates the exact problem I get with the fullscale API that I have no control over. Are there any options I have overlooked in the CSharpClientGeneratorSettings?

@foltram
Copy link

foltram commented Jul 16, 2022

I don't think so. There are a few issues open over on the NJsonSchema repo to attempt to handle this problem (which trickles down to codegens). The inheritance structure of the oneOf is not handled with OpenAPI 3.0+ with Nswag.

RicoSuter/NJsonSchema#839

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

No branches or pull requests

2 participants