swagger-api / swagger-api/swagger-codegen

[c#] invalid c# generated for AnyValue

Open
#11,035 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

[c#] invalid c# generated for AnyValue

Description

Hello!

I'm kinda new to a lot of this...

I need to build a c# client library for the open source Gluu 4.2.3 server SCIM2 API.

Specifically: I'm trying to build a c# client library with VS2015 and .NET 4.7.2 to use with SCIM gluu-scim-openapi.yaml using app.swaggerhub.com

I've also created a bug report in the SCIM project.

I also created a Case #00480040 with swagger who told me:

"SwaggerHub exposes the open-source Codegen library as-is (https://github.com/swagger-api/swagger-codegen). SmartBear maintains the Codegen engine, but the specific language templates are developed and maintained by the open-source community. Unfortunately, the issue you came across is specific to a language and thus needs to be tracked and fixed by the community. Please file a ticket for the issue in the Codegen project"

What happens is the c# is generated, but I get compile errors:

1>C:\Users\scott\Documents\Visual Studio 2010\gluu-scim-server-4.2.2-csharp-client\src\IO.Swagger\Model\PatchOperation.cs(64,119,64,120): error CS1001: Identifier expected
1>C:\Users\scott\Documents\Visual Studio 2010\gluu-scim-server-4.2.2-csharp-client\src\IO.Swagger\Model\PatchOperation.cs(64,129,64,130): error CS1031: Type expected
1>C:\Users\scott\Documents\Visual Studio 2010\gluu-scim-server-4.2.2-csharp-client\src\IO.Swagger\Model\PatchOperation.cs(91,23,91,24): error CS1519: Invalid token '{' in class, struct, or interface member declaration
1>C:\Users\scott\Documents\Visual Studio 2010\gluu-scim-server-4.2.2-csharp-client\src\IO.Swagger\Model\PatchOperation.cs(91,28,91,29): error CS1519: Invalid token ';' in class, struct, or interface member declaration
1>C:\Users\scott\Documents\Visual Studio 2010\gluu-scim-server-4.2.2-csharp-client\src\IO.Swagger\Model\PatchOperation.cs(91,33,91,34): error CS1519: Invalid token ';' in class, struct, or interface member declaration
1>C:\Users\scott\Documents\Visual Studio 2010\gluu-scim-server-4.2.2-csharp-client\src\IO.Swagger\Model\PatchOperation.cs(184,1,184,2): error CS1022: Type or namespace definition, or end-of-file expected
1>
1>Build FAILED.

And indeed it looks a bit wrong:

        public PatchOperation(OperationEnum operation = default(OperationEnum), string path = default(string),  value = default())
        {
            // to ensure "operation" is required (not null)
            if (operation == null)
            {
                throw new InvalidDataException("operation is a required property for PatchOperation and cannot be null");
            }
            else
            {
                this.Operation = operation;
            }
            this.Path = path;
            this.Value = value;
        }
Swagger-codegen version

using https://app.swaggerhub.com

Swagger declaration file content or url
    PatchOperation:
      required:
      - operation
      type: object
      properties:
        operation:
          type: string
          enum:
          - add
          - remove
          - replace
        path:
          type: string
          description: Required when type is remove, optional otherwise
        value:
          $ref: '#/components/schemas/AnyValue'
          description: Only required when type is add or replace
      description: See section 3.5.2 of RFC 7644

I don't know anything about YAML - but value seems to be a reference to this part:

components:
  schemas:
    AnyValue:
      description: Can be any value - string, number, boolean, array or object

And that is almost an exact copy/paste from the swagger docs:
https://swagger.io/docs/specification/data-models/data-types/

So this YAML seems to match the swagger docs but it fails to build c# client libraries. That looks like a bug in swagger to me, rather than a bug in the Gluu Server SCIM2 YAML.

Command line used for generation

using https://app.swaggerhub.com

Steps to reproduce

use https://app.swaggerhub.com to generate c# client library, compile with VS2015

Related issues/PRs

I've also created a bug report in the SCIM project.

I also created a Case #00480040 with swagger who sent me here.

Suggest a fix/enhancement

I tried replacing the definition with:

components:
  schemas:
    AnyValue:
      anyOf:
        - type: string
        - type: number
        - type: boolean
        - type: array
        - type: object
      description: Can be any value - string, number, boolean, array or object

uploaded to https://app.swaggerhub.com/ downloaded the c# client library, extract ZIP, open in VS2015 and builds OK.

So that is a workaround, but the 'original' code matches the swagger documentation exactly, but fails to build in c# - so it looks to me like a swagger bug.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the C# client from the linked OpenAPI YAML and inspect the generated src/IO.Swagger/Model/PatchOperation.cs, especially the AnyValue parameter. Compare the output for the original AnyValue schema with the anyOf workaround, then compile the client in VS2015. Done means the original schema generates valid C# without the reported compiler errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.