RicoSuter / NSwag

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

Rename parameters on NSwag.MSBuild #4211

Open shidcordero opened 1 year ago

shidcordero commented 1 year ago

I got a bit weird swagger.spec from other developer which mainly has 2 variable that uses similar name but with @ at the start. to give an example:

openapi: 3.0.3
info:
  title: Sample Webservice
  description: Webservice for Sample project
  version: 1.14.0
paths:
  /api/ws/factory:
    get:
      operationId: ws-getFactoryCollection
      tags:
        - Factory
      responses:
        '200':
          description: Factory collection
          content:
            application/ld+json:
              schema:
                type: object
                properties:
                  hydra:member:
                    type: array
                    items:
                      $ref: '#/components/schemas/Factory.jsonld-ws-factory.read'
                  hydra:totalItems:
                    type: integer
                    minimum: 0
                  hydra:view:
                    type: object
                    properties:
                      '@id':
                        type: string
                        format: iri-reference
                      '@type':
                        type: string
                      hydra:first:
                        type: string
                        format: iri-reference
                      hydra:last:
                        type: string
                        format: iri-reference
                      hydra:next:
                        type: string
                        format: iri-reference
                  hydra:search:
                    type: object
                    properties:
                      '@type':
                        type: string
                      hydra:template:
                        type: string
                      hydra:variableRepresentation:
                        type: string
                      hydra:mapping:
                        type: array
                        items:
                          type: object
                          properties:
                            '@type':
                              type: string
                            variable:
                              type: string
                            property:
                              type: string
                              nullable: true
                            required:
                              type: boolean
                required:
                  - hydra:member
      summary: Retrieves the collection of Factory resources.
      description: Retrieves the collection of Factory resources.
      parameters:
        - name: page
          in: query
          description: The collection page number
          required: false
          deprecated: false
          schema:
            type: integer
            default: 1
          style: form
        - name: itemsPerPage
          in: query
          description: The number of items per page
          required: false
          deprecated: false
          schema:
            type: integer
            default: 30
            minimum: 0
          style: form
        - name: pagination
          in: query
          description: Enable or disable pagination
          required: false
          deprecated: false
          schema:
            type: boolean
          style: form
        - name: order[name]
          in: query
          description: ''
          required: false
          deprecated: false
          schema:
            type: string
            enum:
              - asc
              - desc
          style: form
      deprecated: false
    post:
      operationId: ws-postFactoryCollection
      tags:
        - Factory
      responses:
        '201':
          description: Factory resource created
          content:
            application/ld+json:
              schema:
                $ref: '#/components/schemas/Factory.jsonld-ws-factory.read'
          links:
            GetFactoryItem:
              operationId: getFactoryItem
              parameters:
                id: $response.body#/id
              requestBody: []
              description: >-
                The `id` value returned in the response can be used as the `id`
                parameter in `GET /api/ws/factory/{id}`.
        '400':
          description: Invalid input
      summary: Creates a Factory resource.
      description: Creates a Factory resource.
      parameters: []
      requestBody:
        description: The new Factory resource
        content:
          application/ld+json:
            schema:
              $ref: '#/components/schemas/Factory.jsonld-ws-factory.write'
        required: true
      deprecated: false
    parameters: []
components:
  schemas:
    Factory.jsonld-material.read:
      type: object
      description: ''
      properties:
        '@context':
          readOnly: true
          type: string
        '@id':
          readOnly: true
          type: string
        '@type':
          readOnly: true
          type: string
        id:
          readOnly: true
          type: integer
        name:
          type: string
        code:
          type: integer
    Factory.jsonld-ws-compound-material.input:
      type: object
      description: ''
      properties:
        id:
          readOnly: true
          type: integer
        name:
          type: string
        code:
          type: integer
    Factory.jsonld-ws-factory.read:
      type: object
      description: ''
      properties:
        '@context':
          readOnly: true
          type: string
        '@id':
          readOnly: true
          type: string
        '@type':
          readOnly: true
          type: string
        id:
          readOnly: true
          type: integer
        externalId:
          type: integer
        name:
          type: string
        code:
          type: integer
    Factory.jsonld-ws-factory.write:
      type: object
      description: ''
      required:
        - externalId
        - name
        - code
      properties:
        externalId:
          type: integer
        name:
          type: string
        code:
          type: integer
security: []
tags: []

You can also copy and paste the yaml and paste it here https://editor.swagger.io/

Notice that the "hydra:member" property has both id and @id, when using the NSwag to generate a CsharpClient, it will make those property into the same variable name:

[System.Text.Json.Serialization.JsonPropertyName("@id")]
public int Id { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("id")]
public string Id { get; set; }

NOTE: this is not specifically for Id, there are some properties also that has @ at the beginning.

Is there a way to rename those variables that has @ on their names, for example the @Id will rename the variable to KeyId or StringId. I'm currently using NSwag.MSBuild but open to code level generation if needed.

gschuermans commented 2 months ago

We are struggling with the same issue. Did you find a solution by any chance?