question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][C#][csharp-netcore] maxLength + enum = generated code doesn't build

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When an object property features maxLength (and possibly other similar attributes?) alongside an enum, the generated code does not compile. Now, I know this is kind of weird to have both enum values and maxLength defined but the spec doesn’t prevent those for the reasons stated here. Which means that the validators won’t catch these, the generator will run fine, only at the final step of building a package the error would show up.

openapi-generator version

master @ e09409fbce

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: C# enum length repro
  version: v1
paths:
  '/hello':
    get:
      responses:
        '200':
          description: returns a greeting
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Greeting'

components:
  schemas:
    Greeting:
      type: object
      properties:
        reply:
          type: string
          maxLength: 20
          enum:
            - hi
            - howdy
Generation Details

See steps below.

Steps to reproduce
openapi-generator generate -i openapi.yaml -g csharp-netcore --library httpclient -o out/csharp_enum
cd out/csharp_enum 
dotnet build --configuration Release

produces:

(...)
out/csharp_enum/src/Org.OpenAPITools/Model/Greeting.cs(141,49): error CS1061: 'Greeting.ReplyEnum?' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'Greeting.ReplyEnum?' could be found (are you missing a using directive or an assembly reference?)
(...)

Build FAILED.
Related issues/PRs

None that I could find.

Suggest a fix

No idea… 🙇

@mandrean @frankyjuang @shibayan @Blackclaws @lucamazzanti @wing328 @prajon84

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Blackclawscommented, May 26, 2021

Yeah I think cleaning up the validation of those parameters is for the best. I’m not sure that there is another way to validate this. In general I think any OpenAPI spec where the enum values and the max/minLength don’t match should be invalid regardless of whether it fits the schema or not as it is essentially contradictory. The fix is simple and I’ll open a quick pull request.

0reactions
jfeltesse-mdsolcommented, May 26, 2021

I hear you but the point is, however non sensical or redundant an attribute may be, as long as the user feeds in a valid OpenAPI file the generator shouldn’t generate code that won’t compile.

In our case here, if the enum type is the best solution (again, not so familiar with C#), then some clean up of the undesirable attributes (minLength, maxLength, …) that we know will trip the generator might be an option?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MaxLengthEnforcement enum - services library - Dart API
API docs for the MaxLengthEnforcement enum from the services library, for the Dart programming language.
Read more >
How to create a table corresponding to enum in EF6 Code First?
I'd prefer a second table to be produced, the values of which would follow the definition of the enumerator in C# code. So,...
Read more >
Enumeration types - C# reference - Microsoft Learn
Learn about C# enumeration types that represent a choice or a combination of choices.
Read more >
Validating JSON schema with max length and enum
I would like to validate each of these jsons' schema, however using the ValidateRecord processor doesn't quite seem to do the job.
Read more >
Build Enumerations of Constants With Python's Enum
Python doesn't have an enum data type as part of its syntax. ... They also make your code robust by providing the following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found