openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [CSharp] Skip model properties in schemas with empty names ""

Open LazaroOnline opened this issue 1 year ago • 2 comments

Bug Report Checklist

  • [v] Have you provided a full/minimal spec to reproduce the issue?
  • [v] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?

Not with master, but with the latest released version in npm.

  • [v] Have you searched for related issues/PRs?
  • [v] What's the actual output vs expected output?
Description

In CSharp code generation, there is a build error in generated code due to model properties with empty names in the schema, like this one "": { "type": "string" } (bad practice, but it is out of my control, I didn't define the json schema, just generating code for it), this would not happen if the code generator just skipped these properties completely.

openapi-generator version

openapi-generator-cli version Did set selected version to 7.8.0

Steps to reproduce
openapi-generator-cli generate -g csharp --skip-validate-spec  -i https://raw.githubusercontent.com/PagerDuty/api-schema/5d679f79622d30b767c777be5f2e9787ef21e092/reference/REST/openapiv3.json

In the code generated, if you open the Notification.cs file, you will find multiple build errors due to the property code missing the name like this:

  • public string { get; set; }
  • public Notification(..., string = default(string))
  • this. = ;
  • sb.Append(" : ").Append().Append("\n");
Suggest a fix

When reading a json schema with empty property names, just show a warning to the console, but skip it during code generation specially when using the flag --skip-validate-spec.

LazaroOnline avatar Aug 22 '24 03:08 LazaroOnline

https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#name-mapping

you may want to try this option to map the empty string to something else (e.g. Unnamed)

wing328 avatar Aug 22 '24 06:08 wing328

I tried the custom name mappings but it resulted in the same code with the same error.
I guess the mappings only work with actual non-empty names:

Tested with:

openapi-generator-cli generate -g csharp --skip-validate-spec  -i https://raw.githubusercontent.com/PagerDuty/api-schema/5d679f79622d30b767c777be5f2e9787ef21e092/reference/REST/openapiv3.json --name-mappings =MISSINGNAME, --parameter-name-mappings =MISSINGNAMEPROP
openapi-generator-cli generate -g csharp --skip-validate-spec  -i https://raw.githubusercontent.com/PagerDuty/api-schema/5d679f79622d30b767c777be5f2e9787ef21e092/reference/REST/openapiv3.json --name-mappings ""=MISSINGNAME, --parameter-name-mappings ""=MISSINGNAMEPROP

LazaroOnline avatar Aug 22 '24 15:08 LazaroOnline