Manweill / swagger-axios-codegen

swagger client to use axios and typescript
MIT License
306 stars 83 forks source link

Version - 0.15.0: Auto Generated Names Issue #168

Closed lucasdale99 closed 1 year ago

lucasdale99 commented 1 year ago

When I run npm run api, which cleans and runs codegen, it's shortening the service names and populating the view models with "_" in the names.

Example of the issue: image image

My Current fix for this is running it back to 0.13.3 which seems to fix the issue. Seen here: image

Please let me know if there is anything else I can do to help!

Package.json:

{
  "name": "belit",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:5000",
  "dependencies": {
    "@fortawesome/free-solid-svg-icons": "^6.2.0",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "@headlessui/react": "^1.7.3",
    "@heroicons/react": "^2.0.12",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.11.62",
    "@types/react": "^18.0.21",
    "@types/react-dom": "^18.0.6",
    "axios": "^1.1.3",
    "msw": "^0.47.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.4.2",
    "react-scripts": "5.0.1",
    "typescript": "^4.8.4",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "api": "npm run clean && npm run codegen",
    "clean": "rimraf ./Codegen/**",
    "codegen": "node ./codegen.ts",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@tailwindcss/forms": "^0.5.3",
    "autoprefixer": "^10.4.12",
    "postcss": "^8.4.16",
    "swagger-axios-codegen": "^0.15.0",
    "tailwindcss": "^3.1.8"
  }
}

Codegen.ts:

// @ts-ignore
const { codegen } = require("swagger-axios-codegen");

codegen({
    methodNameMode: "operationId",
    remoteUrl: "http://localhost:5000/swagger/v1/swagger.json",
    outputDir: "./src/Services",
    fileName: "index.ts",
    strictNullChecks: false,
    modelMode: "interface",
    multipleFileMode: true,
});

Startup Swagger Generation:

services.AddSwaggerGen(c =>
        {
            c.SchemaFilter<RequireNonNullablePropertiesSchemaFilter>();
            c.SupportNonNullableReferenceTypes();
            c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["action"]}");
            // // Set the comments path for the Swagger JSON and UI.
            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });

Schema Filter:

public class RequireNonNullablePropertiesSchemaFilter : ISchemaFilter
    {
        /// <summary>
        /// Add to model.Required all properties where Nullable is false.
        /// </summary>
        public void Apply(OpenApiSchema model, SchemaFilterContext context)
        {
            IEnumerable<string> additionalRequiredProps = model.Properties
                .Where(x => !x.Value.Nullable && !model.Required.Contains(x.Key))
                .Select(x => x.Key);
            foreach (string propKey in additionalRequiredProps)
            {
                model.Required.Add(propKey);
            }
        }
    }

Swashbuckle.AspNetCore.Swagger Version: 6.4.0 Swashbuckle.AspNetCore.SwaggerGen Version: 6.4.0 Swashbuckle.AspNetCore.SwaggerUI Version: 6.4.0