Skip to content

New --alphabetize switch: Sorts output file consistently #942

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/transform/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function transformHeaderObjMap(
): string {
let output = "";

for (const k of Object.keys(headerMap)) {
const v = headerMap[k];
const sortedEntries = Object.entries(headerMap).sort(([a], [b]) => a.localeCompare(b, "en"));
for (const [k, v] of sortedEntries) {
if (!v.schema) continue;

if (v.description) output += comment(v.description);
Expand Down
16 changes: 11 additions & 5 deletions src/transform/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ export function transformParametersArray(

// sort into map
let mappedParams: Record<string, Record<string, ParameterObject>> = {};
for (const paramObj of parameters as any[]) {
if (paramObj.$ref && globalParameters) {
const paramName = paramObj.$ref.split('["').pop().replace(PARAM_END_RE, ""); // take last segment
for (const paramObj of parameters) {
if ("$ref" in paramObj && paramObj.$ref && globalParameters) {
// take last segment
let paramName = paramObj.$ref.split('["').pop();
paramName = String(paramName).replace(PARAM_END_RE, "");

if (globalParameters[paramName]) {
const reference = globalParameters[paramName] as any;
const reference = globalParameters[paramName];
if (!reference.in) continue;

if (!mappedParams[reference.in]) mappedParams[reference.in] = {};
switch (ctx.version) {
case 3: {
Expand All @@ -36,7 +41,7 @@ export function transformParametersArray(
case 2: {
mappedParams[reference.in][reference.name || paramName] = {
...reference,
$ref: paramObj.$ref,
...("$ref" in paramObj ? { $ref: paramObj.$ref } : null),
};
break;
}
Expand All @@ -45,6 +50,7 @@ export function transformParametersArray(
continue;
}

if (!("in" in paramObj)) continue;
if (!paramObj.in || !paramObj.name) continue;
if (!mappedParams[paramObj.in]) mappedParams[paramObj.in] = {};
mappedParams[paramObj.in][paramObj.name] = paramObj;
Expand Down
3 changes: 2 additions & 1 deletion src/transform/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export function transformPathsObj(paths: Record<string, PathItemObject>, options

let output = "";

for (const [url, pathItem] of Object.entries(paths)) {
const sortedEntries = Object.entries(paths).sort(([a], [b]) => a.localeCompare(b, "en"));
for (const [url, pathItem] of sortedEntries) {
if (pathItem.description) output += comment(pathItem.description); // add comment

if (pathItem.$ref) {
Expand Down
3 changes: 2 additions & 1 deletion src/transform/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { transformSchemaObj } from "./schema.js";
export function transformRequestBodies(requestBodies: Record<string, RequestBody>, ctx: GlobalContext) {
let output = "";

for (const [name, requestBody] of Object.entries(requestBodies)) {
const sortedEntries = Object.entries(requestBodies).sort(([a], [b]) => a.localeCompare(b, "en"));
for (const [name, requestBody] of sortedEntries) {
if (requestBody && requestBody.description) output += ` ${comment(requestBody.description)}`;
output += ` "${name}": {\n ${transformRequestBodyObj(requestBody, ctx)}\n }\n`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/transform/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export function transformResponsesObj(responsesObj: Record<string, any>, ctx: Gl

let output = "";

for (const httpStatusCode of Object.keys(responsesObj)) {
const sortedEntries = Object.entries(responsesObj).sort(([a], [b]) => a.localeCompare(b, "en"));
for (const [httpStatusCode, response] of sortedEntries) {
const statusCode = Number(httpStatusCode) || `"${httpStatusCode}"`; // don’t surround w/ quotes if numeric status code
const response = responsesObj[httpStatusCode];
if (response.description) output += comment(response.description);

if (response.$ref) {
Expand Down
6 changes: 2 additions & 4 deletions src/transform/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
nodeType,
tsArrayOf,
tsIntersectionOf,
tsPartial,
tsReadonly,
tsTupleOf,
tsUnionOf,
Expand All @@ -28,9 +27,8 @@ function hasDefaultValue(node: any): boolean {
export function transformSchemaObjMap(obj: Record<string, any>, options: TransformSchemaObjOptions): string {
let output = "";

for (const k of Object.keys(obj)) {
const v = obj[k];

const sortedEntries = Object.entries(obj).sort(([a], [b]) => a.localeCompare(b, "en"));
for (const [k, v] of sortedEntries) {
// 1. Add comment in jsdoc notation
const comment = prepareComment(v);
if (comment) output += comment;
Expand Down