From f08bebbb81c66267a06025338d4009e1c6ac4cf2 Mon Sep 17 00:00:00 2001 From: Kasper Moskwiak Date: Tue, 7 Jun 2022 09:14:58 +0200 Subject: [PATCH] 304 Support readOnly properties --- index.js | 3 ++ package.json | 1 + src/config.js | 2 ++ src/index.js | 2 ++ src/schema.js | 9 ++++- tests/spec/readonly/schema.json | 61 +++++++++++++++++++++++++++++++++ tests/spec/readonly/schema.ts | 18 ++++++++++ tests/spec/readonly/test.js | 20 +++++++++++ 8 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 tests/spec/readonly/schema.json create mode 100644 tests/spec/readonly/schema.ts create mode 100644 tests/spec/readonly/test.js diff --git a/index.js b/index.js index a42b61a50..f3052b9d3 100755 --- a/index.js +++ b/index.js @@ -38,6 +38,7 @@ program false, ) .option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false) + .option("--add-readonly", "generate readonly properties", false) .option("--route-types", "generate type definitions for API routes", false) .option("--no-client", "do not generate an API class", false) .option( @@ -86,6 +87,7 @@ const { name, templates, unionEnums, + addReadonly, routeTypes, client, defaultAsSuccess, @@ -119,6 +121,7 @@ generateApi({ defaultResponseType: defaultResponse, unwrapResponseData: unwrapResponseData, generateUnionEnums: unionEnums, + addReadonly, generateResponses: responses, extractRequestParams: !!extractRequestParams, extractRequestBody: !!extractRequestBody, diff --git a/package.json b/package.json index 86e118e37..417f81797 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "test:--default-as-success": "node tests/spec/defaultAsSuccess/test.js", "test:--templates": "node tests/spec/templates/test.js", "test:--union-enums": "node tests/spec/unionEnums/test.js", + "test:--add-readonly": "node tests/spec/readonly/test.js", "test:--responses": "node tests/spec/responses/test.js", "test:specProperty": "node tests/spec/specProperty/test.js", "test:--module-name-index": "node tests/spec/moduleNameIndex/test.js", diff --git a/src/config.js b/src/config.js index b9f3d63a5..55dd2d178 100644 --- a/src/config.js +++ b/src/config.js @@ -14,6 +14,8 @@ const config = { generateClient: true, /** CLI flag */ generateUnionEnums: false, + /** CLI flag */ + addReadonly: false, enumNamesAsValues: false, /** parsed swagger schema from getSwaggerObject() */ diff --git a/src/index.js b/src/index.js index 6ccbdd3ab..3aaa16a11 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,7 @@ module.exports = { generateClient = config.generateClient, httpClientType = config.httpClientType, generateUnionEnums = config.generateUnionEnums, + addReadonly = config.addReadonly, moduleNameIndex = config.moduleNameIndex, moduleNameFirstTag = config.moduleNameFirstTag, extractRequestParams = config.extractRequestParams, @@ -66,6 +67,7 @@ module.exports = { generateResponses, templates, generateUnionEnums, + addReadonly, moduleNameIndex, moduleNameFirstTag, prettierOptions, diff --git a/src/schema.js b/src/schema.js index 8bd40ce77..8288c6c0b 100644 --- a/src/schema.js +++ b/src/schema.js @@ -137,6 +137,7 @@ const getObjectTypeContent = (schema) => { const nullable = !!(rawTypeData.nullable || property.nullable); const fieldName = isValidName(name) ? name : `"${name}"`; const fieldValue = getInlineParseContent(property); + const readOnly = property.readOnly; return { $$raw: property, @@ -159,7 +160,13 @@ const getObjectTypeContent = (schema) => { isNullable: nullable, name: fieldName, value: fieldValue, - field: _.compact([fieldName, !required && "?", ": ", fieldValue]).join(""), + field: _.compact([ + readOnly && config.addReadonly && "readonly ", + fieldName, + !required && "?", + ": ", + fieldValue, + ]).join(""), }; }); diff --git a/tests/spec/readonly/schema.json b/tests/spec/readonly/schema.json new file mode 100644 index 000000000..283278d59 --- /dev/null +++ b/tests/spec/readonly/schema.json @@ -0,0 +1,61 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "Swagger API Team" + }, + "license": { + "name": "MIT" + } + }, + "host": "petstore.swagger.io", + "basePath": "/api", + "schemes": ["http"], + "consumes": ["application/json"], + "produces": ["application/json"], + "paths": { + "/pets": { + "get": { + "description": "Returns all pets from the system that the user has access to", + "produces": ["application/json"], + "responses": { + "200": { + "description": "A list of pets.", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Pet" + } + } + } + } + } + } + }, + "definitions": { + "Pet": { + "type": "object", + "required": ["id", "name"], + "properties": { + "id": { + "type": "integer", + "readOnly": true, + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + }, + "multiple": { + "type": ["string", "number"] + } + } + } + } +} diff --git a/tests/spec/readonly/schema.ts b/tests/spec/readonly/schema.ts new file mode 100644 index 000000000..a23fad81e --- /dev/null +++ b/tests/spec/readonly/schema.ts @@ -0,0 +1,18 @@ +/* eslint-disable */ +/* tslint:disable */ +/* + * --------------------------------------------------------------- + * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## + * ## ## + * ## AUTHOR: acacode ## + * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## + * --------------------------------------------------------------- + */ + +export interface Pet { + /** @format int64 */ + readonly id: number; + name: string; + tag?: string; + multiple?: string | number; +} diff --git a/tests/spec/readonly/test.js b/tests/spec/readonly/test.js new file mode 100644 index 000000000..644074eb6 --- /dev/null +++ b/tests/spec/readonly/test.js @@ -0,0 +1,20 @@ +const { generateApiForTest } = require("../../helpers/generateApiForTest"); +const { resolve } = require("path"); +const validateGeneratedModule = require("../../helpers/validateGeneratedModule"); +const createSchemaInfos = require("../../helpers/createSchemaInfos"); + +const schemas = createSchemaInfos({ absolutePathToSchemas: resolve(__dirname, "./") }); + +schemas.forEach(({ absolutePath, apiFileName }) => { + generateApiForTest({ + testName: "--route-types option test", + silent: true, + name: apiFileName, + input: absolutePath, + output: resolve(__dirname, "./"), + addReadonly: true, + generateClient: false, + }).then(() => { + validateGeneratedModule(resolve(__dirname, `./${apiFileName}`)); + }); +});