Skip to content

Commit

Permalink
fix(pathparameters): path parameters to use md class instead of direc…
Browse files Browse the repository at this point in the history
…t markup
  • Loading branch information
syroegkin committed Dec 24, 2022
1 parent 73b6182 commit a62a3b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/transformers/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const transformPath = (
md.line('');
// Set method as a subheader
md.line(md.string(method.toUpperCase()).h4());
const pathInfo = data[method];
const pathInfo: OpenAPIV2.OperationObject = data[method];

// Set summary
if ('summary' in pathInfo) {
Expand All @@ -58,7 +58,10 @@ export const transformPath = (

// Build parameters
if ('parameters' in pathInfo || pathParameters) {
const builtParameters = md.string(transformParameters(pathInfo.parameters, pathParameters));
const builtParameters = md.string(transformParameters(
pathInfo.parameters,
pathParameters,
));
if (builtParameters.length) {
md.line(builtParameters).line();
}
Expand Down
42 changes: 24 additions & 18 deletions src/transformers/pathParameters.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { OpenAPIV2 } from 'openapi-types';
import { dataTypeResolver } from './dataTypes';
import { Schema } from '../models/schema';
import { textEscape } from '../lib/textEscape';
import { Markdown } from '../lib/markdown';

export const transformParameters = (parameters, pathParameters?: any) => {
const res = [];
res.push('##### Parameters\n');
res.push('| Name | Located in | Description | Required | Schema |');
res.push('| ---- | ---------- | ----------- | -------- | ---- |');
[].concat(pathParameters, parameters).forEach((keys) => {
export const transformParameters = (
parameters: OpenAPIV2.Parameters,
pathParameters?: OpenAPIV2.Parameters,
) => {
const md = Markdown.md();

md.line(md.string('Parameters').h5()).line();
const table = md.table();
table.th('Name').th('Located in').th('Description').th('Required')
.th('Schema');

[].concat(pathParameters, parameters).forEach((keys: OpenAPIV2.Parameter) => {
if (keys) {
const line = [];
const tr = table.tr();
// Name first
line.push(keys.name || '');
tr.td(keys.name || '');
// Scope (in)
line.push(keys.in || '');
tr.td(keys.in || '');
// description
if ('description' in keys) {
line.push(textEscape(keys.description.replace(/[\r\n]/g, ' ')));
tr.td(md.string(keys.description.replace(/[\r\n]/g, ' ')).escape());
} else {
line.push('');
tr.td('');
}
line.push(keys.required ? 'Yes' : 'No');
tr.td(keys.required ? 'Yes' : 'No');

// Prepare schema to be transformed
let schema = null;
Expand All @@ -34,11 +41,10 @@ export const transformParameters = (parameters, pathParameters?: any) => {
schema.setItems('items' in keys ? keys.items : null);
}

line.push(dataTypeResolver(schema));
// Add spaces and glue using pipeline
const glued = line.map((el) => ` ${el} `).join('|');
res.push(`|${glued}|`);
tr.td(dataTypeResolver(schema));
}
});
return res.join('\n');

md.line(table);
return md.export();
};

0 comments on commit a62a3b7

Please sign in to comment.