4

I'm using swagger codegen 3.0.21 for generating a C# api client.

The generators creates for all referenced classes additional classes in /Model beginning with AllOf[Classname].

Why does Swagger CodeGen generate these classes? What is their porpuse? They also have multiple errors with my sepcification.

Relevant changed part of the generated json:

"components": {
  "schemas": {
    "MyClass": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "propertyTwo": {
            "type": "integer",
            "format": "int64"
          },
          "propertyThree": {
            "allOf": [
              {
                "$ref": "#/components/schemas/MySecondClass"
              }
            ],
            "nullable": true
          },
          "propertyFour": {
            "allOf": [
              {
                "$ref": "#/components/schemas/MyThirdClass"
              }
            ],
            "nullable": true
          },
          "propertyFive": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/MyFourthClass"
            },
            "nullable": true
          },
          "propertySix": {
            "type": "boolean"
          }
        },
        "additionalProperties": false
    },
  }
}

The constructor for MyClass is generated as following:

public MyClass(long? id = default(long?), long? propertyTwo = default(long?),  propertyThree = default(),  propertyFour = default(), List<MyThirdClass> propertyFive = default(List<MyFourthClass>), bool? propertySix = default(bool?))

As you can see the datatypes for propertyThree and propertyFour are just empty spaces.

  • 1
    Most likely those `allOf` definitions are defined inline in your OpenAPI file, rather than as separate named schemas. Can you post your OpenAPI YAML/JSON file? – Helen Sep 23 '20 at 15:55
  • Added a changed version of the relevant part in the specification. The json was generated with Swagger 3.24.3. The classes are normally generated in Swagger CodeGen 3.0.18., but with the schown problem in the constructor. Swagger CodeGen 3.0.19 - 21 creates the classes AllOfMyThirdClass and AllOfMyFourthClass, also with the problem in the constructor and other additional ones. – RumelDilDumpel Sep 23 '20 at 21:18
  • 2
    You may want to give [OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) (a community fork of Swagger Codegen start by the top contributors) a try. It comes with a `csharp-netcore` generator which seems to do a better job with allOf. – NickSim Jan 13 '21 at 01:56

1 Answers1

1

We ran into the same issue. For generating a csharp client/models we actually use https://github.com/RicoSuter/NSwag/wiki/NSwagStudio which also provide you a lot of options and does generate the models properly

However for a client we needed a java generator as well and nswagstudio only supports csharp and typescript. I tried the suggestion of @NickSim that worked as a charm.

Maarten Kieft
  • 6,806
  • 3
  • 29
  • 34