Skip to content

Commit 737c6d2

Browse files
committed
Reset to initial values
1 parent d4414f9 commit 737c6d2

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/components/api-request.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ export default class ApiRequest extends LitElement {
143143
continue;
144144
}
145145
const defaultVal = Array.isArray(paramSchema.default) ? paramSchema.default : `${paramSchema.default}`;
146+
let initialVal = '';
147+
if (paramSchema.required) {
148+
initialVal = defaultVal || (paramSchema.allowedValues && paramSchema.allowedValues[0]) || '';
149+
} else if (this.fillRequestWithDefault === 'true') {
150+
initialVal = defaultVal;
151+
}
146152
// Set the default style: https://spec.openapis.org/oas/v3.1.0.html#fixed-fields-9
147153
const paramStyle = param.style ?? {
148154
query: 'form',
@@ -199,20 +205,21 @@ export default class ApiRequest extends LitElement {
199205
data-ptype = "${paramLocation}"
200206
data-pname = "${paramName}"
201207
data-default = "${defaultVal}"
208+
data-initial = "${initialVal}"
202209
data-param-serialize-style = "${paramStyle}"
203210
data-param-serialize-explode = "${paramExplode}"
204211
spellcheck = "false"
205212
placeholder="${generatedParamSchema.example || defaultVal || ''}"
206213
style = "width:100%; margin-top: 1rem; margin-bottom: 1rem;"
207-
.value="${this.fillRequestWithDefault === 'true' ? defaultVal : ''}"></textarea>`
214+
.value="${initialVal}"></textarea>`
208215
|| generatedParamSchema.allowedValues && html`
209216
<select aria-label="mime type" style="width:100%; margin-top: 1rem; margin-bottom: 1rem;"
210217
data-ptype="${paramLocation}"
211218
data-pname="${paramName}"
212-
.value="${this.fillRequestWithDefault === 'true' ? defaultVal : ''}"
219+
data-initial="${initialVal}"
213220
@change="${(e) => { this.storedParamValues[paramName] = e; this.computeCurlSyntax(); }}">
214221
${generatedParamSchema.allowedValues.map((allowedValue) => html`
215-
<option value="${allowedValue}" ?selected = '${allowedValue === this.storedParamValues[paramName]}'>
222+
<option value="${allowedValue}" ?selected = '${allowedValue === this.storedParamValues[paramName] || allowedValue === initialVal}'>
216223
${allowedValue === null ? '-' : allowedValue}
217224
</option>`
218225
)}
@@ -229,9 +236,10 @@ export default class ApiRequest extends LitElement {
229236
data-ptype="${paramLocation}"
230237
data-pname="${paramName}"
231238
data-default="${Array.isArray(defaultVal) ? defaultVal.join('~|~') : defaultVal}"
239+
data-initial="${initialVal}"
232240
data-array="false"
233241
@keyup="${this.requestParamFunction}"
234-
.value="${this.fillRequestWithDefault === 'true' ? defaultVal : ''}"
242+
.value="${initialVal}"
235243
/>`
236244
: ''}
237245
@@ -693,7 +701,7 @@ export default class ApiRequest extends LitElement {
693701
onClearRequestData(e) {
694702
const requestPanelEl = e.target.closest('.request-panel');
695703
const requestPanelInputEls = [...requestPanelEl.querySelectorAll('input, select, tag-input, textarea:not(.is-hidden)')];
696-
requestPanelInputEls.forEach((el) => { el.value = ''; });
704+
requestPanelInputEls.forEach((el) => { el.value = el.dataset.initial || ''; });
697705

698706
const event = { bubbles: true, composed: true, detail: { explorerLocation: this.elementId, operation: { method: this.method, path: this.path }, type: 'RequestCleared' } };
699707
this.dispatchEvent(new CustomEvent('event', event));

src/utils/schema-utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function getTypeInfo(parameter, options = { includeNulls: false, enableEx
4747
deprecated: !!schema.deprecated,
4848
example: examples || '',
4949
default: schema.default ?? '',
50+
required: !!schema.required,
5051
title: schema.title || '',
5152
description: schema.description || '',
5253
constraints: [],

0 commit comments

Comments
 (0)