Giter VIP home page Giter VIP logo

Comments (7)

hanspagel avatar hanspagel commented on September 26, 2024 1

Great bug report! Let’s fix this!

from scalar.

gsmcdonald avatar gsmcdonald commented on September 26, 2024 1

@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.

gsmcdonald avatar gsmcdonald commented on September 26, 2024 1

@marclave I'll open a PR soon. Thank you!

from scalar.

gsmcdonald avatar gsmcdonald commented on September 26, 2024

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.

marclave avatar marclave commented on September 26, 2024

@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.

gsmcdonald avatar gsmcdonald commented on September 26, 2024

@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.

image

from scalar.

gsmcdonald avatar gsmcdonald commented on September 26, 2024

Fixed by #1934

from scalar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.