RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

TypeScript Client generator is generating PascalCase model prop references instead of camelCase - resulting in undefined values #4777

Open VladuBogdanNicolae opened 8 months ago

VladuBogdanNicolae commented 8 months ago

Hi, I did come across an issue after migrating our app project to .NET 8. The type script client generator is now incorrectly referencing the model properties as PascalCase rather than camelCase resulting in undefined objects.

Our web api is correctly passing the properties as camelCase, the typescript client generated the API model props in the same way (camelCase), but the code referencing the model props from the http response is using PascalCase, please see:

image

I do use MSBuild and the NSwagExe_Net80 command based on nswag.json file generated via NSwagStudio:

<Target Name="NSwag" AfterTargets="PostBuildEvent">
  <Message Importance="high" Text=" " />
  <Message Importance="high" Text="Running NSwag generate..." />
  <Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=NSwag" Command="$(NSwagExe_Net80) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>

Used packages: image

Minimal Api config:

services.AddOpenApiDocument(config =>
 {
      if (swaggerOptions != null)
      {
          config.DocumentName = "v1";
          config.Title = swaggerOptions.Title;
          config.Version = "v1";

          config.AddSecurity(swaggerOptions.Scheme, Enumerable.Empty<string>(), new OpenApiSecurityScheme
          {
              Type = OpenApiSecuritySchemeType.ApiKey,
              In = OpenApiSecurityApiKeyLocation.Header,
              Name = HeaderNames.Authorization,
              Scheme = JwtBearerDefaults.AuthenticationScheme,
              BearerFormat = "JWT",
              Description = "Copy 'Bearer ' + valid JWT token into field"
          });

          config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor(swaggerOptions.Scheme));
      }
  });

if (app.Environment.IsDevelopment())
{
    app.UseOpenApi();
    app.UseSwaggerUi();
}

Please help...what can I do about this, it worked perfectly before in .NET 7 before the migration? Thank you.

krzyhan commented 8 months ago

Can you show openapi doc with schema part?

I'm asking because in the default example, everything is working fine:

image

(maybe you have different settings?)

I had similar issue, with camelCase but that's because of some settings;

you can always override by ex.

document.ApplySettings(CreateNewtonsoftSchemaWithCamelCaseStrategy(), null);

 private static NewtonsoftJsonSchemaGeneratorSettings CreateNewtonsoftSchemaWithCamelCaseStrategy()
     => new()
     {
         SchemaType = SchemaType.Swagger2,
         SerializerSettings = new JsonSerializerSettings
         {
             ContractResolver = new CamelCasePropertyNamesContractResolver
             {
                 NamingStrategy = new CamelCaseNamingStrategy()
             },
             ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
         }
     };