Search code examples
typescriptswaggeropenapiopenapi-generator

Swagger openapi-typescript-codegen change casing for one model


I know you can change the casing for your project According to the swagger-codegen FAQ you are able to change the casing as such:

modelPropertyNaming Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name (Default: camelCase)

by using it in your query:

java -jar openapi-generator-cli-4.1.3.jar generate -i C:\Users\ASUS\Desktop\OpenAPICodegen\swagger.json -g typescript-angular -o C:\Users\ASUS\Desktop\OpenAPICodegen\dist --additional-properties modelPropertyNaming=origional

or

dotnet swagger tofile --output ./wwwroot/swagger/v1/swagger.yaml --yaml $(OutputPath)$(AssemblyName).dll v1 --additional-properties modelPropertyNaming=origional

Q: How can I do it for 1 specific model?

In my project all my models in .NET go from PascalCase to camelCase which is all good. But one of my models represents a model that is being fetched from a JSON file. hence I need only one (1) model to have PascalCase while keeping the others in camelCase. How can i achieve this?


Solution

  • Since there hasn't been much activity i'll post my own answer:


    #1 You have to make your own SchemaFilter

    
    using Microsoft.OpenApi.Models;
    using Swashbuckle.AspNetCore.SwaggerGen;
    
    namespace FAKD.Server.Services
    {
        public class PascalCaseSchemaFilter<T> : ISchemaFilter
            where T : class
        {
            public void Apply(OpenApiSchema schema, SchemaFilterContext context)
            {
                var properties = context.Type.GetProperties();
                foreach (var property in properties)
                {
                    if (context.Type == typeof(T))
                    {
                        var propertyName = property.Name;
                        schema.Properties = schema.Properties.ToDictionary(
                            d => d.Key.Substring(0, 1).ToUpper() + d.Key.Substring(1),
                            d => d.Value
                        );
                    }
                }
            }
        }
    }
    
    

    #2 Add filter to AddSwaggerGen Options

    builder.Services.AddSwaggerGen(spec =>
    {
        var filePath = Path.Combine(AppContext.BaseDirectory, "backend.xml");
        spec.IncludeXmlComments(filePath);
        spec.DescribeAllParametersInCamelCase();
        spec.SupportNonNullableReferenceTypes();
        var url = builder.Configuration.GetValue<string>("OpenApi:BaseUrl");
        spec.AddServer(new OpenApiServer { Url = url, Description = "default" });
    
    
        spec.SchemaFilter<PascalCaseSchemaFilter<MYMODELHERE>>();
    
    });
    

    #3 Profit

          "ModelA": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "format": "uuid"
              },
              "type": {
                "type": "string"
              },
              "identificator": {
                "type": "string"
              },
              "approvedBy": {
                "type": "string"
              },
              "reviewed": {
                "type": "boolean"
              },
              "start": {
                "type": "integer",
                "format": "int64"
              },
              "end": {
                "type": "integer",
                "format": "int64"
              },
              "aCount": {
                "type": "integer",
                "format": "int32"
              }
            },
            "additionalProperties": false
          },
          "MYMODELHERE": {
            "type": "object",
            "properties": {
              "Id": {
                "type": "string",
                "format": "uuid"
              },
              "Type": {
                "type": "string"
              },
              "Identificator": {
                "type": "string"
              },
              "ApprovedBy": {
                "type": "string"
              },