Search code examples
c#.netopenapiopenapi-generatornswag

Technology for generating a list of schemas in OpenApi 3.0 yaml format based on C# types


I need to generate type schemas in OpenApi yaml format. I am considering any technology that can be used to manually create an OpenApi 3.0 document without generation based on ASP.NET Core controllers.

I have some list of types from which schemas should be generated and put in Components/Schemas for reuse. And those types, which are for example base types for the one on which the schema is generated should also eventually be in Components/Schemas.

I've tried a few technologies: Microsoft.OpenApi - currently has no functionality for generating type-based schemas

NSwag, NJSonSchema - I could not find a way to generate a list of schemas without Definitions list.

JsonSchema.FromType<TestModel>()

title: TestModel
definitions:
  NestedObject:
    type: object
    additionalProperties: false
    properties:
      Name:
        type:
        - null
        - string
      Age:
        type: integer
        format: int32
  Base:
    type: object
    additionalProperties: false
    properties:
      BaseProperty:
        oneOf:
        - {}
        - type: null
allOf:
- $ref: '#/definitions/Base'
- type: object
  description: Тестовая модель
  additionalProperties: false
  properties:
    Id:
      type: string
      format: guid
    NestedObject:
      oneOf:
      - type: null
      - $ref: '#/definitions/NestedObject'

Swashbuckle - This technology library has a Swashbuckle.Swagger.SchemaRegistry class that works the way I need it to. The only disadvantage, as far as I understood, this library allows you to create a document and schemes for it only in swagger 2.0 format, which is not suitable for me.

Is there any way to generate a list of schemas along the same lines as Swashbuckle, where some registrar is passed C# classes and after one list you can get schemas of all registered types and their nested, base types, but in OpenApi 3.0 format using NSwag or any other technology for .net?


Solution

  • While further exploring the NSwag source code, I came across the OpenApiSchemaResolver type.

    This code solves my problem:

    var settings = new NewtonsoftJsonSchemaGeneratorSettings()
    {
        SchemaType = SchemaType.OpenApi3,
        AllowReferencesWithProperties = true
    };
    var generator = new JsonSchemaGenerator(settings);
    
    generator.GenerateWithReferenceAndNullability<JsonSchema>(
        typeof(TestModel).ToContextualType(), 
        false,
        new OpenApiSchemaResolver(nSwagDoc, settings));
    

    Methods of JsonSchemaGenerator class that accept parameters of JsonSchemaResolver type just allow to generate schemes for C# type and all its nested, base types, while placing them in OpenApi document and with correct references.

    Test types:

    class TestModel : Base
    {
        public Guid Id { get; set; }
        public NestedObject NestedObject { get; set; }
    }
    
    class NestedObject
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    
    class Base
    {
        public object BaseProperty { get; set; }
    }
    

    OpenApi schemas in document (Components/Schemas):

    ...
    
    NestedObject:
      type: object
      additionalProperties: false
      properties:
        Name:
          type: string
          nullable: true
        Age:
          type: integer
          format: int32
    Base:
      type: object
      additionalProperties: false
      properties:
        BaseProperty:
          nullable: true
    responses:
      OK:
        description: Successful operation
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/testItemResult'
    
    ...
    

    I hope it will be useful to someone. I apologize if I am not quite clear in the question I write in English with a translator.