Skip to content

Commit 0a3d332

Browse files
authored
304 Support readOnly properties (#365)
1 parent bf6fa61 commit 0a3d332

File tree

8 files changed

+115
-1
lines changed

8 files changed

+115
-1
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ program
3737
false,
3838
)
3939
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
40+
.option("--add-readonly", "generate readonly properties", false)
4041
.option("--route-types", "generate type definitions for API routes", false)
4142
.option("--no-client", "do not generate an API class", false)
4243
.option("--enum-names-as-values", "use values in 'x-enumNames' as enum values (not only as keys)", false)
@@ -75,6 +76,7 @@ const {
7576
name,
7677
templates,
7778
unionEnums,
79+
addReadonly,
7880
routeTypes,
7981
client,
8082
defaultAsSuccess,
@@ -113,6 +115,7 @@ generateApi({
113115
disableThrowOnError: disableThrowOnError,
114116
sortTypes: sortTypes,
115117
generateUnionEnums: unionEnums,
118+
addReadonly,
116119
generateResponses: responses,
117120
extractRequestParams: !!extractRequestParams,
118121
extractRequestBody: !!extractRequestBody,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test:--default-as-success": "node tests/spec/defaultAsSuccess/test.js",
2424
"test:--templates": "node tests/spec/templates/test.js",
2525
"test:--union-enums": "node tests/spec/unionEnums/test.js",
26+
"test:--add-readonly": "node tests/spec/readonly/test.js",
2627
"test:--responses": "node tests/spec/responses/test.js",
2728
"test:specProperty": "node tests/spec/specProperty/test.js",
2829
"test:--module-name-index": "node tests/spec/moduleNameIndex/test.js",

src/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const config = {
1414
generateClient: true,
1515
/** CLI flag */
1616
generateUnionEnums: false,
17+
/** CLI flag */
18+
addReadonly: false,
1719
enumNamesAsValues: false,
1820
/** parsed swagger schema from getSwaggerObject() */
1921

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = {
4040
generateClient = config.generateClient,
4141
httpClientType = config.httpClientType,
4242
generateUnionEnums = config.generateUnionEnums,
43+
addReadonly = config.addReadonly,
4344
moduleNameIndex = config.moduleNameIndex,
4445
moduleNameFirstTag = config.moduleNameFirstTag,
4546
extractRequestParams = config.extractRequestParams,
@@ -70,6 +71,7 @@ module.exports = {
7071
generateResponses,
7172
templates,
7273
generateUnionEnums,
74+
addReadonly,
7375
moduleNameIndex,
7476
moduleNameFirstTag,
7577
prettierOptions,

src/schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const getObjectTypeContent = (schema) => {
137137
const nullable = !!(rawTypeData.nullable || property.nullable);
138138
const fieldName = isValidName(name) ? name : `"${name}"`;
139139
const fieldValue = getInlineParseContent(property);
140+
const readOnly = property.readOnly;
140141

141142
return {
142143
$$raw: property,
@@ -160,7 +161,13 @@ const getObjectTypeContent = (schema) => {
160161
isNullable: nullable,
161162
name: fieldName,
162163
value: fieldValue,
163-
field: _.compact([fieldName, !required && "?", ": ", fieldValue]).join(""),
164+
field: _.compact([
165+
readOnly && config.addReadonly && "readonly ",
166+
fieldName,
167+
!required && "?",
168+
": ",
169+
fieldValue,
170+
]).join(""),
164171
};
165172
});
166173

tests/spec/readonly/schema.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore",
6+
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
7+
"termsOfService": "http://swagger.io/terms/",
8+
"contact": {
9+
"name": "Swagger API Team"
10+
},
11+
"license": {
12+
"name": "MIT"
13+
}
14+
},
15+
"host": "petstore.swagger.io",
16+
"basePath": "/api",
17+
"schemes": ["http"],
18+
"consumes": ["application/json"],
19+
"produces": ["application/json"],
20+
"paths": {
21+
"/pets": {
22+
"get": {
23+
"description": "Returns all pets from the system that the user has access to",
24+
"produces": ["application/json"],
25+
"responses": {
26+
"200": {
27+
"description": "A list of pets.",
28+
"schema": {
29+
"type": "array",
30+
"items": {
31+
"$ref": "#/definitions/Pet"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
},
39+
"definitions": {
40+
"Pet": {
41+
"type": "object",
42+
"required": ["id", "name"],
43+
"properties": {
44+
"id": {
45+
"type": "integer",
46+
"readOnly": true,
47+
"format": "int64"
48+
},
49+
"name": {
50+
"type": "string"
51+
},
52+
"tag": {
53+
"type": "string"
54+
},
55+
"multiple": {
56+
"type": ["string", "number"]
57+
}
58+
}
59+
}
60+
}
61+
}

tests/spec/readonly/schema.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
/*
4+
* ---------------------------------------------------------------
5+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
6+
* ## ##
7+
* ## AUTHOR: acacode ##
8+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
9+
* ---------------------------------------------------------------
10+
*/
11+
12+
export interface Pet {
13+
/** @format int64 */
14+
readonly id: number;
15+
name: string;
16+
tag?: string;
17+
multiple?: string | number;
18+
}

tests/spec/readonly/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { generateApiForTest } = require("../../helpers/generateApiForTest");
2+
const { resolve } = require("path");
3+
const validateGeneratedModule = require("../../helpers/validateGeneratedModule");
4+
const createSchemaInfos = require("../../helpers/createSchemaInfos");
5+
6+
const schemas = createSchemaInfos({ absolutePathToSchemas: resolve(__dirname, "./") });
7+
8+
schemas.forEach(({ absolutePath, apiFileName }) => {
9+
generateApiForTest({
10+
testName: "--route-types option test",
11+
silent: true,
12+
name: apiFileName,
13+
input: absolutePath,
14+
output: resolve(__dirname, "./"),
15+
addReadonly: true,
16+
generateClient: false,
17+
}).then(() => {
18+
validateGeneratedModule(resolve(__dirname, `./${apiFileName}`));
19+
});
20+
});

0 commit comments

Comments
 (0)