Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwaggerEditor@next: no specification loaded on page refresh #5075

Closed
glowcloud opened this issue May 24, 2024 · 5 comments
Closed

SwaggerEditor@next: no specification loaded on page refresh #5075

glowcloud opened this issue May 24, 2024 · 5 comments

Comments

@glowcloud
Copy link

Content & configuration

Running Swagger Editor locally with the default env definition (https://raw.githubusercontent.com/asyncapi/spec/v2.6.0/examples/streetlights-kafka.yml)

Describe the bug you're encountering

The specification loads fine but refreshing the page causes it to disappear from the editor. Any changes made will be lost. This also happens for the GitHub Pages deployment but not for https://editor-next.swagger.io/

To reproduce...

Steps to reproduce the behavior:

  1. Open https://ghpages-swagger-editor.swagger.io/ in incognito window
  2. See that the specification is loaded
  3. Refresh the page
  4. See an empty {} specification

Expected behavior

The editor should retain the specification on refresh like in https://editor-next.swagger.io/

@glowcloud
Copy link
Author

glowcloud commented May 24, 2024

It looks like the issue happens with Swagger UI v5.17.6, most probably due to swagger-api/swagger-ui#9918. With 5.17.5 the page refreshes as it should.

@char0n
Copy link
Member

char0n commented May 24, 2024

I made a mistake when porting following conditions:

if(this.props.spec && (this.props.spec !== prevStateSpec || this.props.spec !== prevProps.spec)) {
if(this.props.url !== prevStateUrl || this.props.url !== prevProps.url) {

They key is that we're missing prevProps.spec/url comparison in new function component. We must use useRef hook to access the prev value of spec/url prop and use that in the comparison.

@char0n
Copy link
Member

char0n commented May 27, 2024

The core issues was that after converting swagger-ui-react from class to function component, the default for the spec prop became the {} (empty object) instead of before used "" (empty string). The code needs to compensate for that. The spec updated related hook needs to look like this:

 useEffect(() => {
    if (system) {
      const prevStateSpec = system.specSelectors.specStr();
       if (
        spec &&
        spec !== SwaggerUIConstructor.config.defaults.spec &&
        (spec !== prevStateSpec || spec !== prevSpec)
      ) {
        const updatedSpec = typeof spec === 'object' ? JSON.stringify(spec) : spec;
        system.specActions.updateSpec(updatedSpec);
      }
    }
  }, [system, spec]);

@char0n
Copy link
Member

char0n commented May 27, 2024

SwaggerUI regression remediation released as https://github.com/swagger-api/swagger-ui/releases/tag/v5.17.13

@char0n
Copy link
Member

char0n commented May 27, 2024

Addressed in #5080

@char0n char0n closed this as completed May 27, 2024
swagger-bot pushed a commit that referenced this issue May 28, 2024
# [5.0.0-alpha.96](v5.0.0-alpha.95...v5.0.0-alpha.96) (2024-05-28)

### Bug Fixes

* fix re-render on page refresh ([#5080](#5080)) ([8366388](8366388)), closes [#5075](#5075)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants