Skip to content

Commit f6760ce

Browse files
committed
Add default option to always show required properties first
1 parent dbd39af commit f6760ce

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

packages/compiler/src/content/chunks/operation.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,21 @@ export function renderParameters(
167167
return;
168168
}
169169
debug(`Rendering parameters`);
170-
const { showDebugPlaceholders } = getSettings().display;
170+
const { showDebugPlaceholders, sortRequiredProperties } =
171+
getSettings().display;
172+
if (sortRequiredProperties) {
173+
debug("Sorting required parameters to top.");
174+
parameters.sort((a, b) => {
175+
if (a.required && !b.required) {
176+
return -1;
177+
}
178+
if (!a.required && b.required) {
179+
return 1;
180+
}
181+
return a.name.localeCompare(b.name);
182+
});
183+
}
184+
171185
renderer.createParametersSection(() => {
172186
for (const parameter of parameters) {
173187
debug(`Rendering parameter: name=${parameter.name}`);

packages/compiler/src/content/chunks/schema.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
PropertyAnnotations,
55
} from "@speakeasy-api/docs-md-shared";
66

7+
import { debug } from "../../logging.ts";
78
import type { Renderer } from "../../renderers/base.ts";
89
import { getSettings } from "../../settings.ts";
910
import { assertNever } from "../../util/assertNever.ts";
@@ -518,6 +519,21 @@ export function renderObjectProperties({
518519
};
519520
}
520521
);
522+
523+
const { sortRequiredProperties } = getSettings().display;
524+
if (sortRequiredProperties) {
525+
debug("Sorting required parameters to top.");
526+
properties.sort((a, b) => {
527+
if (a.isRequired && !b.isRequired) {
528+
return -1;
529+
}
530+
if (!a.isRequired && b.isRequired) {
531+
return 1;
532+
}
533+
return a.name.localeCompare(b.name);
534+
});
535+
}
536+
521537
for (const property of properties) {
522538
createExpandableProperty({
523539
renderer,

packages/compiler/src/settings.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,20 @@ export const settingsSchema = z.strictObject({
375375
* When this setting is enabled, you must also set `output.embedOutDir`
376376
*/
377377
maxNestingLevel: z.number().optional(),
378+
379+
/***
380+
* Whether or not to sort required properties and parameters first.
381+
*
382+
* When enabled, properties will still be sorted alphabetically but required
383+
* properties will be shown first.
384+
*/
385+
sortRequiredProperties: z.boolean().default(true),
378386
})
379387
.default({
380388
visibleResponses: "explicit",
381389
showDebugPlaceholders: false,
382390
expandTopLevelPropertiesOnPageLoad: true,
391+
sortRequiredProperties: true,
383392
}),
384393

385394
/**

0 commit comments

Comments
 (0)