TownSuite / TownSuite.Web.SSV3Adapter

Basic service stack v3 facade.
Other
1 stars 2 forks source link

Swagger/OpenApi OAS 3.0 support + Microsoft.OpenApi.Models #1

Closed majorsilence closed 6 months ago

majorsilence commented 2 years ago

Is your feature request related to a problem? Please describe.

Need the ability to output oas 3.0 for automatic tooling purposes.

Describe the solution you'd like

Use the Microsoft.OpenApi.Models package

https://www.nuget.org/packages/Microsoft.OpenApi/

<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />

For further details on the schema: Use the open api specification 3.0 doc to create a functioning implementation.
https://swagger.io/specification/

Additional context

https://github.com/TownSuite/TownSuite.Web.SSV3Adapter/blob/main/TownSuite.Web.SSV3Adapter/Swagger.cs generates swagger paste v2 json.

The code may look something like this:

using Microsoft.OpenApi.Models;

var swaggerDoc= new OpenApiDocument();
swaggerDoc.Info = new OpenApiInfo()
{
    Title = "TownSuite.SSV3Adapter",
    Version = "v1"
};
swaggerDoc.Components = new OpenApiComponents();
swaggerDoc.Components.Schemas = new Dictionary<string, OpenApiSchema>();
swaggerDoc.Paths = new OpenApiPaths();

foreach (var something in somethings)
{
  var schema = new OpenApiSchema
  {
      Type = "object",
      Properties = new Dictionary<string, OpenApiSchema>()
  };

  var props = something.GetType().GetProperties();
  foreach (var prop in props)
  {
      schema.Properties.Add(prop.Name,
          new OpenApiSchema { Type =  GetSwaggerType(prop.GetType()) });
  }

 // Add the schema to the Swagger document
   string endpointName = something.GetType().Name.ToLower();
   string theNamespace = something.....;
    swaggerDoc.Paths.Add($"/The/Base/Path/{endpointName}", new OpenApiPathItem()
    {
        Operations = new Dictionary<OperationType, OpenApiOperation>()
        {
            [OperationType.Post] = new OpenApiOperation()
            {
                Responses = new OpenApiResponses()
                {
                    ["200"] = new OpenApiResponse()
                    {
                        Description = "Success",
                        Content = new Dictionary<string, OpenApiMediaType>()
                        {
                            ["application/json"] = new OpenApiMediaType()
                            {
                                Schema = new OpenApiSchema()
                                {
                                    Reference = new OpenApiReference()
                                    {
                                        Id = $"{theNamespace}.{endpointName}",
                                        Type = ReferenceType.Schema
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });
    swaggerDoc.Components.Schemas.Add($"{theNamespace}.{endpointName}",
        schema);

}

private string GetSwaggerType(Type type)
{
    if (type == typeof(string))
    {
        return "string";
    }

    if (type == typeof(int))
    {
        return "integer";
    }

    if (type == typeof(bool))
    {
        return "boolean";
    }

    if (type == typeof(DateTime))
    {
        return "string";
    }

    if (type == typeof(decimal) || type == typeof(double) || type == typeof(float))
    {
        return "number";
    }

    return "object";
}