Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add title to schema output #2000

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ const handlebarsPlugin = () => ({
enumerator: true,
escapeComment: true,
escapeDescription: true,
escapeTitle: true,
camelCase: true,
},
});
1 change: 1 addition & 0 deletions src/client/interfaces/Schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Schema {
title: string | null;
isDefinition: boolean;
isReadOnly: boolean;
isRequired: boolean;
1 change: 1 addition & 0 deletions src/openApi/v2/interfaces/OpenApiParameter.d.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import type { OpenApiSchema } from './OpenApiSchema';
*/
export interface OpenApiParameter extends OpenApiReference, WithEnumExtension, WithNullableExtension {
name: string;
title?: string;
in: 'path' | 'query' | 'header' | 'formData' | 'body';
description?: string;
required?: boolean;
1 change: 1 addition & 0 deletions src/openApi/v2/interfaces/OpenApiResponse.d.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import type { OpenApiSchema } from './OpenApiSchema';
*/
export interface OpenApiResponse extends OpenApiReference {
description: string;
title: string;
schema?: OpenApiSchema & OpenApiReference;
headers?: Dictionary<OpenApiHeader>;
examples?: OpenApiExample;
1 change: 1 addition & 0 deletions src/openApi/v2/parser/getModel.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ export const getModel = (
): Model => {
const model: Model = {
name,
title: definition.title || null,
export: 'interface',
type: 'any',
base: 'any',
1 change: 1 addition & 0 deletions src/openApi/v2/parser/getModelComposition.ts
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ export const getModelComposition = (
if (properties.length) {
composition.properties.push({
name: 'properties',
title: '',
export: 'interface',
type: 'any',
base: 'any',
2 changes: 2 additions & 0 deletions src/openApi/v2/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
const model = getType(property.$ref);
models.push({
name: escapeName(propertyName),
title: property.title || null,
export: 'reference',
type: model.type,
base: model.base,
@@ -52,6 +53,7 @@ export const getModelProperties = (openApi: OpenApi, definition: OpenApiSchema,
const model = getModel(openApi, property);
models.push({
name: escapeName(propertyName),
title: property.title || null,
export: model.export,
type: model.type,
base: model.base,
1 change: 1 addition & 0 deletions src/openApi/v2/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
prop: parameter.name,
export: 'interface',
name: getOperationParameterName(parameter.name),
title: parameter.title || null,
type: 'any',
base: 'any',
template: null,
1 change: 1 addition & 0 deletions src/openApi/v2/parser/getOperationResponse.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ export const getOperationResponse = (
const operationResponse: OperationResponse = {
in: 'response',
name: '',
title: response.title || null,
code: responseCode,
description: response.description || null,
export: 'generic',
1 change: 1 addition & 0 deletions src/openApi/v2/parser/getOperationResults.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export const getOperationResults = (operationResponses: OperationResponse[]): Op
operationResults.push({
in: 'response',
name: '',
title: '',
code: 200,
description: '',
export: 'generic',
2 changes: 2 additions & 0 deletions src/openApi/v2/parser/getRef.spec.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ describe('getRef', () => {
paths: {},
definitions: {
Example: {
title: 'Example model',
description: 'This is an Example model ',
type: 'integer',
},
@@ -26,6 +27,7 @@ describe('getRef', () => {
}
)
).toEqual({
title: 'Example model',
description: 'This is an Example model ',
type: 'integer',
});
2 changes: 2 additions & 0 deletions src/openApi/v2/parser/getServices.spec.ts
Original file line number Diff line number Diff line change
@@ -14,9 +14,11 @@ describe('getServices', () => {
tags: [],
responses: {
200: {
title: 'x',
description: 'x',
},
default: {
title: 'Default',
description: 'default',
},
},
1 change: 1 addition & 0 deletions src/openApi/v3/interfaces/OpenApiParameter.d.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import type { OpenApiSchema } from './OpenApiSchema';
*/
export interface OpenApiParameter extends OpenApiReference {
name: string;
title?: string;
in: 'path' | 'query' | 'header' | 'formData' | 'cookie';
description?: string;
required?: boolean;
1 change: 1 addition & 0 deletions src/openApi/v3/interfaces/OpenApiResponse.d.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import type { OpenApiReference } from './OpenApiReference';
*/
export interface OpenApiResponse extends OpenApiReference {
description: string;
title: string;
headers?: Dictionary<OpenApiHeader>;
content?: Dictionary<OpenApiMediaType>;
links?: Dictionary<OpenApiLink>;
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ export const getModel = (
): Model => {
const model: Model = {
name,
title: definition.title || null,
export: 'interface',
type: 'any',
base: 'any',
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModelComposition.ts
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ export const getModelComposition = (
if (properties.length) {
composition.properties.push({
name: 'properties',
title: '',
export: 'interface',
type: 'any',
base: 'any',
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ export const getModelProperties = (
| 'properties'
> = {
name: escapeName(propertyName),
title: property.title || null,
description: property.description || null,
deprecated: property.deprecated === true,
isDefinition: false,
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getModels.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ export const getModels = (openApi: OpenApi): Model[] => {
const schema = definition.schema;
if (schema) {
const model = getModel(openApi, schema, true, definitionType.base.replace(reservedWords, '_$1'));
model.title = definition.title || null;
model.description = definition.description || null;
model.deprecated = definition.deprecated;
models.push(model);
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getOperationParameter.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
prop: parameter.name,
export: 'interface',
name: getOperationParameterName(parameter.name),
title: parameter.title || null,
type: 'any',
base: 'any',
template: null,
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getOperationRequestBody.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ export const getOperationRequestBody = (openApi: OpenApi, body: OpenApiRequestBo
export: 'interface',
prop: 'requestBody',
name: 'requestBody',
title: null,
type: 'any',
base: 'any',
template: null,
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getOperationResponse.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ export const getOperationResponse = (
const operationResponse: OperationResponse = {
in: 'response',
name: '',
title: response.title || null,
code: responseCode,
description: response.description || null,
export: 'generic',
1 change: 1 addition & 0 deletions src/openApi/v3/parser/getOperationResults.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export const getOperationResults = (operationResponses: OperationResponse[]): Op
operationResults.push({
in: 'response',
name: '',
title: '',
code: 200,
description: '',
export: 'generic',
2 changes: 2 additions & 0 deletions src/openApi/v3/parser/getServices.spec.ts
Original file line number Diff line number Diff line change
@@ -14,9 +14,11 @@ describe('getServices', () => {
tags: [],
responses: {
200: {
title: 'x',
description: 'x',
},
default: {
title: 'Default',
description: 'default',
},
},
6 changes: 6 additions & 0 deletions src/templates/partials/schemaArray.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
type: 'array',
{{#if title}}
title: '{{{escapeTitle title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
{{#if link}}
contains: {{>schema link}},
{{else}}
3 changes: 3 additions & 0 deletions src/templates/partials/schemaComposition.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
type: '{{export}}',
{{#if title}}
title: '{{{escapeTitle title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
6 changes: 6 additions & 0 deletions src/templates/partials/schemaDictionary.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
type: 'dictionary',
{{#if title}}
title: '{{{escapeTitle title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
{{#if link}}
contains: {{>schema link}},
{{else}}
3 changes: 3 additions & 0 deletions src/templates/partials/schemaGeneric.hbs
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@
{{#if type}}
type: '{{{type}}}',
{{/if}}
{{#if title}}
title: '{{{escapeTitle title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
3 changes: 3 additions & 0 deletions src/templates/partials/schemaInterface.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
{{#if title}}
title: '{{{escapeTitle title}}}',
{{/if}}
{{#if description}}
description: `{{{escapeDescription description}}}`,
{{/if}}
3 changes: 3 additions & 0 deletions src/utils/getModelNames.spec.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ describe('getModelNames', () => {
const john: Model = {
export: 'interface',
name: 'John',
title: null,
type: 'John',
base: 'John',
template: null,
@@ -23,6 +24,7 @@ describe('getModelNames', () => {
const jane: Model = {
export: 'interface',
name: 'Jane',
title: null,
type: 'Jane',
base: 'Jane',
template: null,
@@ -40,6 +42,7 @@ describe('getModelNames', () => {
const doe: Model = {
export: 'interface',
name: 'Doe',
title: null,
type: 'Doe',
base: 'Doe',
template: null,
1 change: 1 addition & 0 deletions src/utils/registerHandlebarHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ describe('registerHandlebarHelpers', () => {
expect(helpers).toContain('enumerator');
expect(helpers).toContain('escapeComment');
expect(helpers).toContain('escapeDescription');
expect(helpers).toContain('escapeTitle');
expect(helpers).toContain('camelCase');
});
});
4 changes: 4 additions & 0 deletions src/utils/registerHandlebarHelpers.ts
Original file line number Diff line number Diff line change
@@ -101,6 +101,10 @@ export const registerHandlebarHelpers = (root: {
return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
});

Handlebars.registerHelper('escapeTitle', function (value: string): string {
return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
});

Handlebars.registerHelper('camelCase', function (value: string): string {
return camelCase(value);
});
3 changes: 3 additions & 0 deletions src/utils/sortModelsByName.spec.ts
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ describe('sortModelsByName', () => {
const john: Model = {
export: 'interface',
name: 'John',
title: null,
type: 'John',
base: 'John',
template: null,
@@ -23,6 +24,7 @@ describe('sortModelsByName', () => {
const jane: Model = {
export: 'interface',
name: 'Jane',
title: null,
type: 'Jane',
base: 'Jane',
template: null,
@@ -40,6 +42,7 @@ describe('sortModelsByName', () => {
const doe: Model = {
export: 'interface',
name: 'Doe',
title: null,
type: 'Doe',
base: 'Doe',
template: null,
1 change: 1 addition & 0 deletions src/utils/writeClientModels.spec.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ describe('writeClientModels', () => {
{
export: 'interface',
name: 'User',
title: null,
type: 'User',
base: 'User',
template: null,
1 change: 1 addition & 0 deletions src/utils/writeClientSchemas.spec.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ describe('writeClientSchemas', () => {
{
export: 'interface',
name: 'User',
title: null,
type: 'User',
base: 'User',
template: null,