Skip to content

Commit

Permalink
fix: Add missing attributes and "boolean" type (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed Aug 8, 2019
1 parent 6867544 commit 4c78260
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/generators/MethodGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('MethodGenerator', () => {
expect(methodDefinition.normalizedUrl).toBe('/identity-providers');
expect(methodDefinition.parameterMethod).toBe('postById');
expect(methodDefinition.pathParameters[0]).toEqual({name: 'id', type: 'any'});
expect(methodDefinition.bodyParameters[0]).toEqual({name: 'body', required: false, type: '{ user: string }'});
expect(methodDefinition.bodyParameters[0]).toEqual({name: 'body', required: false, type: '{ user?: string }'});
});
});
});
13 changes: 10 additions & 3 deletions src/generators/MethodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as StringUtil from '../util/StringUtil';

export enum SwaggerType {
ARRAY = 'array',
BOOLEAN = 'boolean',
INTEGER = 'integer',
NUMBER = 'number',
OBJECT = 'object',
Expand All @@ -14,6 +15,7 @@ export enum SwaggerType {
export enum TypeScriptType {
ANY = 'any',
ARRAY = 'Array',
BOOLEAN = 'boolean',
EMPTY_OBJECT = '{}',
NUMBER = 'number',
STRING = 'string',
Expand Down Expand Up @@ -240,9 +242,11 @@ export class MethodGenerator {

const schema: Record<string, string> = {};

for (const property of Object.keys(properties)) {
const propertyName = requiredProperties && !requiredProperties.includes(property) ? `${property}?` : property;
schema[propertyName] = this.buildTypeFromSchema(properties[property], `${schemaName}/${property}`);
for (const [property, propertyOptons] of Object.entries(properties)) {
const isRequired = requiredProperties && requiredProperties.includes(property);
const isReadOnly = propertyOptons.readOnly;
const propertyName = `${isReadOnly ? 'readonly ' : ''}${property}${isRequired ? '' : '?'}`;
schema[propertyName] = this.buildTypeFromSchema(propertyOptons, `${schemaName}/${property}`);
}

return inspect(schema, {breakLength: Infinity, depth: Infinity})
Expand Down Expand Up @@ -277,6 +281,9 @@ export class MethodGenerator {
case SwaggerType.STRING: {
return TypeScriptType.STRING;
}
case SwaggerType.BOOLEAN: {
return TypeScriptType.BOOLEAN;
}
case SwaggerType.NUMBER:
case SwaggerType.INTEGER: {
return TypeScriptType.NUMBER;
Expand Down
8 changes: 4 additions & 4 deletions src/test/snapshots/1-query-param-description.ts.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class ArchiveService {
*/
archiveConversation = async (
instanceId: string,
body: {archive: {}; conversationId: string}
): Promise<{instanceId: string; name: string}> => {
body: {archive?: boolean; conversationId?: string}
): Promise<{instanceId?: string; name?: string}> => {
const config: AxiosRequestConfig = {
data: {
...body,
Expand All @@ -29,8 +29,8 @@ export class ArchiveService {
url: `/instance/${instanceId}/archive`,
};
const response = await this.apiClient.request<{
instanceId: string;
name: string;
instanceId?: string;
name?: string;
}>(config);
return response.data;
};
Expand Down

0 comments on commit 4c78260

Please sign in to comment.