diff --git a/README.md b/README.md index 14ecd22..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` +`${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 diff --git a/package.json b/package.json index 5411cfa..754a57e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-openapi-typescript", - "version": "1.0.9", + "version": "1.0.10", "description": "An extension of @conqa/serverless-openapi-documentation that also generates your OpenAPI models from TypeScript", "main": "dist/index.js", "scripts": { diff --git a/src/serverless-openapi-typescript.ts b/src/serverless-openapi-typescript.ts index f73fee9..c23186a 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': 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}) => {