Skip to content
Closed
Show file tree
Hide file tree
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
20 changes: 14 additions & 6 deletions src/components/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ export default class ApiRequest extends LitElement {
continue;
}
const defaultVal = Array.isArray(paramSchema.default) ? paramSchema.default : `${paramSchema.default}`;
let initialVal = '';
if (paramSchema.required) {
initialVal = defaultVal || (paramSchema.allowedValues && paramSchema.allowedValues[0]) || '';
} else if (this.fillRequestWithDefault === 'true') {
initialVal = defaultVal;
}
// Set the default style: https://spec.openapis.org/oas/v3.1.0.html#fixed-fields-9
const paramStyle = param.style ?? {
query: 'form',
Expand Down Expand Up @@ -199,20 +205,21 @@ export default class ApiRequest extends LitElement {
data-ptype = "${paramLocation}"
data-pname = "${paramName}"
data-default = "${defaultVal}"
data-initial = "${initialVal}"
data-param-serialize-style = "${paramStyle}"
data-param-serialize-explode = "${paramExplode}"
spellcheck = "false"
placeholder="${generatedParamSchema.example || defaultVal || ''}"
style = "width:100%; margin-top: 1rem; margin-bottom: 1rem;"
.value="${this.fillRequestWithDefault === 'true' ? defaultVal : ''}"></textarea>`
.value="${initialVal}"></textarea>`
|| generatedParamSchema.allowedValues && html`
<select aria-label="mime type" style="width:100%; margin-top: 1rem; margin-bottom: 1rem;"
data-ptype="${paramLocation}"
data-pname="${paramName}"
.value="${this.fillRequestWithDefault === 'true' ? defaultVal : ''}"
data-initial="${initialVal}"
@change="${(e) => { this.storedParamValues[paramName] = e; this.computeCurlSyntax(); }}">
${generatedParamSchema.allowedValues.map((allowedValue) => html`
<option value="${allowedValue}" ?selected = '${allowedValue === this.storedParamValues[paramName]}'>
<option value="${allowedValue}" ?selected = '${allowedValue === this.storedParamValues[paramName] || allowedValue === initialVal}'>
${allowedValue === null ? '-' : allowedValue}
</option>`
)}
Expand All @@ -229,9 +236,10 @@ export default class ApiRequest extends LitElement {
data-ptype="${paramLocation}"
data-pname="${paramName}"
data-default="${Array.isArray(defaultVal) ? defaultVal.join('~|~') : defaultVal}"
data-initial="${initialVal}"
data-array="false"
@keyup="${this.requestParamFunction}"
.value="${this.fillRequestWithDefault === 'true' ? defaultVal : ''}"
.value="${initialVal}"
/>`
: ''}

Expand Down Expand Up @@ -692,8 +700,8 @@ export default class ApiRequest extends LitElement {

onClearRequestData(e) {
const requestPanelEl = e.target.closest('.request-panel');
const requestPanelInputEls = [...requestPanelEl.querySelectorAll('input, tag-input, textarea:not(.is-hidden)')];
requestPanelInputEls.forEach((el) => { el.value = ''; });
const requestPanelInputEls = [...requestPanelEl.querySelectorAll('input, select, tag-input, textarea:not(.is-hidden)')];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think select needs special handling, for instance what if select doesn't have a ' ' option, for optional enums for instance, the values include null not ' '. And for required enums ' ' isn't an option. Realistically we would need to select the default value in cases where the parameter is required and the nullish version that matches the property for types were it is required. I don't know if the properties already exist to figure that out from the html attributes....

Copy link
Contributor Author

@woefe woefe Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure how required parameters should behave when hide-defaults is enabled. What should be the initial value and what should "reset" reset them to? My new revision ignores the hide-defaults for required parameters and uses the defaultValue if available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things also come to mind here:

  • How is "initial" different from "default" here. I think default is what these should be reset to.
  • We can never use || because that breaks nullable and falsy default values.
    I think this is going to take some more serious attention.

Rather than this latest commit, I would suggest going back to what you had before, but adding in special case handling for picking an allowed value (or null or false or '') for enums for the reset option. It might be just something like defaultValue !== undefined ? defaultValue : allowed values[0] but I haven't thought through that all the way.

requestPanelInputEls.forEach((el) => { el.value = el.dataset.initial || ''; });

const event = { bubbles: true, composed: true, detail: { explorerLocation: this.elementId, operation: { method: this.method, path: this.path }, type: 'RequestCleared' } };
this.dispatchEvent(new CustomEvent('event', event));
Expand Down
1 change: 1 addition & 0 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function getTypeInfo(parameter, options = { includeNulls: false, enableEx
deprecated: !!schema.deprecated,
example: examples || '',
default: schema.default ?? '',
required: !!schema.required,
title: schema.title || '',
description: schema.description || '',
constraints: [],
Expand Down