Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to render Typescript template in Pascal Case #813

Closed
cwoolum opened this issue Jun 21, 2017 · 12 comments
Closed

Add ability to render Typescript template in Pascal Case #813

cwoolum opened this issue Jun 21, 2017 · 12 comments

Comments

@cwoolum
Copy link

cwoolum commented Jun 21, 2017

I am currently trying to use NSWAG to generate clients for a legacy API I am working with. The problem is that there are a bunch of existing clients who already work with these services so I cannot change the serialization. Additionally, we have some OData API's that our users consume outside of swagger so the users would expect the casing to be the same as our standard API's. Is it possible to render the client using the formatting that the API outputs?

@cwoolum
Copy link
Author

cwoolum commented Jun 21, 2017

This would only be for generated models. I would expect method names to follow normal camel case conventions

@RicoSuter
Copy link
Owner

Can you provide some samples

@cwoolum
Copy link
Author

cwoolum commented Jun 21, 2017

Here's a basic example of a generated swagger doc.

{
  "swagger": "2.0",
  "info": {
    "version": "Latest",
    "title": "Legacy APIs - Latest"
  },
  "host": "localhost",
  "basePath": "/",
  "schemes": [
    "https"
  ],
  "paths": {
    "/api/public-issues/create": {
      "post": {
        "tags": [
          "issues"
        ],
        "operationId": "IssuesPublicApi_CreateIssue",
        "consumes": [
          "application/json",
          "text/json",
          "application/x-www-form-urlencoded"
        ],
        "produces": [
          "application/json",
          "text/json"
        ],
        "parameters": [
          {
            "name": "apiToken",
            "in": "query",
            "required": true,
            "type": "string"
          },
          {
            "name": "dto",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Model.Quality.Issues.DTO.PublicAPI.IssuePublicApiDTO"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/Model.DTOs.General.SavedDTO"
            }
          }
        },
        "deprecated": false,
        "x-spx-authorization-required": true,
        "x-spx-version": "v0"
      }
    },
   
    "/api/public-issues/example": {
      "get": {
        "tags": [
          "issues"
        ],
        "operationId": "IssuesPublicApi_GetExample",
        "consumes": [],
        "produces": [
          "application/json",
          "text/json"
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/Model.Quality.Issues.DTO.PublicAPI.IssuePublicApiDTO"
            }
          }
        },
        "deprecated": false,
        "x-spx-authorization-required": true,
        "x-spx-version": "v0"
      }
    },

  },
  "definitions": {
    "System.Object": {
      "type": "object",
      "properties": {}
    },
    "Model.Quality.Issues.DTO.PublicAPI.IssuePublicApiDTO": {
      "type": "object",
      "properties": {
        "Title": {
          "type": "string"
        },
        "Type": {
          "type": "string"
        },
        "SubType": {
          "type": "string"
        },
       
        "AssignedToADUsername": {
          "type": "string"
        },
        "Department": {
          "type": "string"
        },
        "Summary": {
          "type": "string"
        },
        "Site": {
          "type": "string"
        },
        "DispositionLocation": {
          "type": "string"
        },
       
        "QuantityInspected": {
          "type": "string"
        },
        "QuantityDefective": {
          "type": "string"
        },
        "QuantityUnits": {
          "type": "string"
        },
        "LotSize": {
          "type": "string"
        },
        "PurchaseOrder": {
          "type": "string"
        },
        "SupplierCode": {
          "type": "string"
        },
        "SubTierSupplierCode": {
          "type": "string"
        },
        "VendorPartSerialNumber": {
          "type": "string"
        },
        "FileAttachmentIDs": {
          "type": "array",
          "items": {
            "format": "int32",
            "type": "integer"
          }
        },
       
        "Subscribers": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
       
        "CauseCode": {
          "type": "string"
        },
        "RootCause": {
          "type": "string"
        },
        "PermanentFix": {
          "type": "string"
        },
        "IsSimulation": {
          "type": "boolean"
        }
      }
    },
   
  },
  "securityDefinitions": {
    "apiKey": {
      "type": "apiKey",
      "description": "API Token Authentication",
      "name": "api_key",
      "in": "header"
    }
  }
}

@RicoSuter
Copy link
Owner

Ok... If you use the gen in c# you can provide a custom PropertxNameGenerator so that the names are upper camel case...

@cwoolum
Copy link
Author

cwoolum commented Jun 21, 2017

Do you mean generate a c# client first and then convert that to typescript? The swagger doc is correct. The properties created from NSwag are cased incorrectly for my case

@RicoSuter
Copy link
Owner

You have to write your own cmd line tool and use the nswag libraries. This way you can customize the names. This is currently not possible via nswag.exe or nswagstudio..

@Wholteza
Copy link

Wholteza commented Jan 8, 2019

Hi @cwoolum , did you solve your problem?
I'm in the same seat and i cannot seem to figure out how and where to use the PropertyNameGenerator. I have created one but i need to know in what settings to put it and how to use that setting with my generator. Could you share a snippet maybe? Thanks!

@RicoSuter
Copy link
Owner

A custom generator cannot be used in nswagstudio or cli, you need to write your own cmd app

@RicoSuter
Copy link
Owner

Related: #924

@NickFranceschina
Copy link

NickFranceschina commented Jan 8, 2019

I spent the majority of my morning today trying to make this happen... digging around trying to figure out how NSwag was working... realizing it was calling over to @RSuter 's other project https://github.com/RSuter/NJsonSchema to generate the actual DTO classes... and I couldn't figure out if the resolvers over there could take an option for property formatting.... and then I gave up and resigned to the fact that I must use camelCase property names

FYI for anyone reading this... https://github.com/OpenAPITools/openapi-generator gives you the options to control property name generation: camelCase, PascalCase, or original (what's in the spec).... but that project is a whole other animal (I like that NSwag generates a single file)

@RicoSuter
Copy link
Owner

For c# and ts generation, a custom property name generator is the way to go:
https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs#L38

If you want to change the casing in the serialized properties/the spec, you need to provide a custom SerializerSettings with a “special” contract resolver

@sm-cdecker
Copy link

I'm not seeing any field or anything in the documentation about how to use/implement a custom property name generator. Can you elaborate a bit more? (I'm also not seeing an option in NSwagStudio for this)

I'm REALLY just looking for the casing on my properties to match the casing of my C# properties 1:1, as this would make it a lot easier to jump around between code files AND would let me eliminate all of the transformation code from my generated code. (which would in turn let me reduce the size of the generated file by a lot)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants