Comments (7)
Great bug report! Let’s fix this!
from scalar.
@hanspagel What do you think about something like this? I haven't tested it because of some build issues, but I think the logic adds up.
/**
* Gets the first example response if there are multiple example responses
* or the only example if there is only one example response.
*/
const getFirstExampleResponse = () => {
if (!hasMultipleExamples.value)
return currentJsonResponse.value.example;
else if (Array.isArray(currentJsonResponse.value.examples))
return currentJsonResponse.value.examples[0]
else {
const firstProperty = Object.keys(currentJsonResponse.value.examples)[0];
return currentJsonResponse.value.examples[firstProperty];
}
}
const currentResponseWithExample = computed(() => ({
...currentJsonResponse.value,
example:
hasMultipleExamples.value && selectedExampleKey.value
? currentJsonResponse.value.examples[selectedExampleKey.value].value ??
currentJsonResponse.value.examples[selectedExampleKey.value]
: getFirstExampleResponse(),
}))
const changeTab = (index: number) => {
selectedResponseIndex.value = index
selectedExampleKey.value = null;
}
from scalar.
@marclave I'll open a PR soon. Thank you!
from scalar.
I think the issue is that the selectedExampleKey
value isn't reset when switching tabs, which causes an error when attempting to find that value in the examples object here.
I'm not sure if the expected behavior would be to clear the value so that the first response is selected when switching tabs or potentially just checking if the key exists in the examples
object (in case you wanted to maintain state between response codes with the same responses).
from scalar.
@hanspagel What do you think about something like this? I haven't tested it because of some build issues, but I think the logic adds up.
/** * Gets the first example response if there are multiple example responses * or the only example if there is only one example response. */ const getFirstExampleResponse = () => { if (!hasMultipleExamples.value) return currentJsonResponse.value.example; else if (Array.isArray(currentJsonResponse.value.examples)) return currentJsonResponse.value.examples[0] else { const firstProperty = Object.keys(currentJsonResponse.value.examples)[0]; return currentJsonResponse.value.examples[firstProperty]; } } const currentResponseWithExample = computed(() => ({ ...currentJsonResponse.value, example: hasMultipleExamples.value && selectedExampleKey.value ? currentJsonResponse.value.examples[selectedExampleKey.value].value ?? currentJsonResponse.value.examples[selectedExampleKey.value] : getFirstExampleResponse(), })) const changeTab = (index: number) => { selectedResponseIndex.value = index selectedExampleKey.value = null; }
this looks good to me! want to open a PR @gsmcdonald ? we can also tackle it ✨
from scalar.
@marclave I'm struggling to get it to build. Have you seen this error before? As much as I hate to ask you to make another change, if you do have time to implement this, it might be faster. I'll post this in the Discord as well to make conversation easier.
from scalar.
Fixed by #1934
from scalar.
Related Issues (20)
- Reuse the parsed OpenAPI document in the Client
- render request payload for webhooks
- API Client lacks support for formData params (Swagger 2.0) HOT 1
- Data migration bugs
- Incorrect request url
- Fix generate-types script HOT 1
- Using a big spec in the config causes a quota error HOT 2
- Issues with Automatic Headers and Cookies in Scalar after Recent Updates (standalone.js) HOT 1
- form parameters are not added to the request HOT 1
- new workspaces don’t use the configured proxy
- API client breaks when pasting long text in the address bar
- OpenAPI HTTP1.1 example code is invalid
- Multiple auth methods bug HOT 3
- Renaming workspace doesn’t work HOT 3
- Remove `@scalar/themes` from the dependencies of `@scalar/types` HOT 2
- Body not filling with example values HOT 1
- Hitting 'Enter' in any input field resets the url path HOT 1
- Missing styles HOT 4
- Main content order doesn't follow x-tagGroups, causing misalignment with sidebar
- Get request adds content-type json header by default when using scalar from CDN.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scalar.