From ae57b25440220b6ace2658af7c021ee00757ec5e Mon Sep 17 00:00:00 2001 From: Zamir Khan Date: Mon, 10 Jan 2022 15:07:21 -0500 Subject: [PATCH 1/5] Add camelCase --- src/serverless-openapi-typescript.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serverless-openapi-typescript.ts b/src/serverless-openapi-typescript.ts index 2b48445..065c209 100644 --- a/src/serverless-openapi-typescript.ts +++ b/src/serverless-openapi-typescript.ts @@ -2,7 +2,7 @@ import type Serverless from "serverless"; import fs from "fs"; import yaml from "js-yaml"; import {SchemaGenerator, createGenerator} from "ts-json-schema-generator"; -import {upperFirst, mergeWith, set, isArray, get, isEmpty } from "lodash" ; +import {upperFirst, camelCase, mergeWith, set, isArray, get, isEmpty } from "lodash" ; import {ApiGatewayEvent} from "serverless/plugins/aws/package/compile/events/apiGateway/lib/validate"; interface Options { @@ -138,7 +138,7 @@ export default class ServerlessOpenapiTypeScript { } setModels(httpEvent, functionName) { - const definitionPrefix = `${this.serverless.service.custom.documentation.apiNamespace}.${upperFirst(functionName)}`; + const definitionPrefix = `${this.serverless.service.custom.documentation.apiNamespace}.${upperFirst(camelCase(functionName))}`; const method = httpEvent.method.toLowerCase(); switch (method) { case 'delete': From 861af8baa688e0644577972b6e4175533e673fb0 Mon Sep 17 00:00:00 2001 From: Zamir Khan Date: Mon, 10 Jan 2022 15:18:39 -0500 Subject: [PATCH 2/5] Add test for hyphenated functions --- .../expect-openapi-hyphenated-functions.yml | 71 +++++++++++++++++++ test/serverless-hyphenated-functions/api.d.ts | 14 ++++ .../resources/serverless.yml | 55 ++++++++++++++ test/serverless-openapi-typescript.spec.ts | 1 + 4 files changed, 141 insertions(+) create mode 100644 test/fixtures/expect-openapi-hyphenated-functions.yml create mode 100644 test/serverless-hyphenated-functions/api.d.ts create mode 100644 test/serverless-hyphenated-functions/resources/serverless.yml diff --git a/test/fixtures/expect-openapi-hyphenated-functions.yml b/test/fixtures/expect-openapi-hyphenated-functions.yml new file mode 100644 index 0000000..2f558ff --- /dev/null +++ b/test/fixtures/expect-openapi-hyphenated-functions.yml @@ -0,0 +1,71 @@ +openapi: 3.1.0 +components: + schemas: + ProjectApi.CreateFunc.Request.Body: + type: 'null' + ProjectApi.CreateFunc.Response: + type: 'null' + ProjectApi.GetFunc.Response: + type: 'null' +info: + title: Project + description: DummyDescription + version: 3a5b57df-54e7-4dd2-9423-d4674809c816 +paths: + /create: + post: + operationId: create-func + summary: Create Function + description: | + Create Function1 + Create Function2 + Create Function3 + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectApi.CreateFunc.Request.Body' + description: '' + parameters: [] + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectApi.CreateFunc.Response' + tags: + - FooBarTitle + /delete: + delete: + operationId: delete-all-func + summary: Delete Function + description: Delete + parameters: [] + responses: + '204': + description: Status 204 Response + content: {} + tags: + - Project + /get: + get: + operationId: get_func + summary: Get Function + parameters: [] + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ProjectApi.GetFunc.Response' + tags: + - BazTitle +tags: + - name: Project + description: DummyDescription + - name: FooBarTitle + description: FooBarDescription + - name: BazTitle + description: BazDescription diff --git a/test/serverless-hyphenated-functions/api.d.ts b/test/serverless-hyphenated-functions/api.d.ts new file mode 100644 index 0000000..956ff6e --- /dev/null +++ b/test/serverless-hyphenated-functions/api.d.ts @@ -0,0 +1,14 @@ +export namespace ProjectApi { + + export namespace CreateFunc { + export namespace Request { + export type Body = void + } + + export type Response = void + } + + export namespace GetFunc { + export type Response = void + } +} diff --git a/test/serverless-hyphenated-functions/resources/serverless.yml b/test/serverless-hyphenated-functions/resources/serverless.yml new file mode 100644 index 0000000..2007dfd --- /dev/null +++ b/test/serverless-hyphenated-functions/resources/serverless.yml @@ -0,0 +1,55 @@ +service: serverless-openapi-typescript-demo +provider: + name: aws + +plugins: + - ../node_modules/@conqa/serverless-openapi-documentation + - ../src/index + +custom: + documentation: + title: 'Project' + description: DummyDescription + apiNamespace: ProjectApi + tags: + - name: FooBarTitle + description: FooBarDescription + - name: BazTitle + description: BazDescription + + +functions: + create-func: + handler: handler.create + events: + - http: + documentation: + summary: "Create Function" + tag: FooBarTitle + description: | + Create Function1 + Create Function2 + Create Function3 + path: create + method: post + + delete-all-func: + handler: handler.delete + events: + - http: + documentation: + summary: "Delete Function" + description: "Delete" + path: delete + method: delete + + get_func: + handler: handler.update + events: + - http: + documentation: + summary: "Get Function" + tag: BazTitle + path: get + method: get + diff --git a/test/serverless-openapi-typescript.spec.ts b/test/serverless-openapi-typescript.spec.ts index 73abdbc..b74e331 100644 --- a/test/serverless-openapi-typescript.spec.ts +++ b/test/serverless-openapi-typescript.spec.ts @@ -14,6 +14,7 @@ describe('ServerlessOpenapiTypeScript', () => { describe.each` testCase | projectName ${'Custom Tags'} | ${'custom-tags'} + ${'Hyphenated Functions'} | ${'hyphenated-functions'} ${'Full Project'} | ${'full'} `('when using $testCase', ({projectName}) => { From 8a90539b8eba3a97c9e411d853038a36c01c12c0 Mon Sep 17 00:00:00 2001 From: Zamir Khan Date: Mon, 10 Jan 2022 15:19:29 -0500 Subject: [PATCH 3/5] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4d83e9c..6751025 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-openapi-typescript", - "version": "1.0.5", + "version": "1.0.6", "description": "An extension of @conqa/serverless-openapi-documentation that also generates your OpenAPI models from TypeScript", "main": "dist/index.js", "scripts": { From d516664ec58dee531c7b2440dd183321cafee9c8 Mon Sep 17 00:00:00 2001 From: Zamir Khan Date: Mon, 17 Jan 2022 14:53:58 -0500 Subject: [PATCH 4/5] Add special character handling to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 14ecd22..ad19cd0 100644 --- a/README.md +++ b/README.md @@ -330,7 +330,7 @@ The `requestModels` property allows you to define models for the HTTP Request of These will be autogenerated for you from TypeScript **by convention**. The plugin will autogenerate a request model for every `put`, `patch` or `post` method based on the TypeScript type found by convention. The look strategy will be by: -`${serverless.custom.documentation.apiNamespace}.${upper-case-name-of-function}.Request.Body` +`${serverless.custom.documentation.apiNamespace}.${upper-case-name-of-function}.Request.Body`. Note that any underscores (`_`) or hyphens (`_`) in function names will be excluded in the resolved type (e.g. the Serverless function `my_get-function` will resolve to `MyGetFunction` in Typescript) For example, the following function: ```yaml From 811579b5cbf8bb4244e659ac3442525cb1100470 Mon Sep 17 00:00:00 2001 From: Zamir Khan Date: Mon, 17 Jan 2022 14:56:50 -0500 Subject: [PATCH 5/5] Change underscore to hyphen --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ad19cd0..56bcadd 100644 --- a/README.md +++ b/README.md @@ -330,7 +330,7 @@ The `requestModels` property allows you to define models for the HTTP Request of These will be autogenerated for you from TypeScript **by convention**. The plugin will autogenerate a request model for every `put`, `patch` or `post` method based on the TypeScript type found by convention. The look strategy will be by: -`${serverless.custom.documentation.apiNamespace}.${upper-case-name-of-function}.Request.Body`. Note that any underscores (`_`) or hyphens (`_`) in function names will be excluded in the resolved type (e.g. the Serverless function `my_get-function` will resolve to `MyGetFunction` in Typescript) +`${serverless.custom.documentation.apiNamespace}.${upper-case-name-of-function}.Request.Body`. Note that any underscores (`_`) or hyphens (`-`) in function names will be excluded in the resolved type (e.g. the Serverless function `my_get-function` will resolve to `MyGetFunction` in Typescript) For example, the following function: ```yaml