Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -86,6 +87,7 @@ const {
name,
templates,
unionEnums,
addReadonly,
routeTypes,
client,
defaultAsSuccess,
Expand Down Expand Up @@ -119,6 +121,7 @@ generateApi({
defaultResponseType: defaultResponse,
unwrapResponseData: unwrapResponseData,
generateUnionEnums: unionEnums,
addReadonly,
generateResponses: responses,
extractRequestParams: !!extractRequestParams,
extractRequestBody: !!extractRequestBody,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const config = {
generateClient: true,
/** CLI flag */
generateUnionEnums: false,
/** CLI flag */
addReadonly: false,
enumNamesAsValues: false,
/** parsed swagger schema from getSwaggerObject() */

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -66,6 +67,7 @@ module.exports = {
generateResponses,
templates,
generateUnionEnums,
addReadonly,
moduleNameIndex,
moduleNameFirstTag,
prettierOptions,
Expand Down
9 changes: 8 additions & 1 deletion src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(""),
};
});

Expand Down
61 changes: 61 additions & 0 deletions tests/spec/readonly/schema.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
}
}
}
18 changes: 18 additions & 0 deletions tests/spec/readonly/schema.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions tests/spec/readonly/test.js
Original file line number Diff line number Diff line change
@@ -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}`));
});
});