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

Could not resolve reference: JSON Pointer evaluation failed while evaluating token "components" against an unexpected Element #10191

Open
florianmartens opened this issue Nov 4, 2024 · 2 comments

Comments

@florianmartens
Copy link

florianmartens commented Nov 4, 2024

General:

  • MacOs
  • Browser: Chrome, Safari, Firefox
  • Method of installation: pnpm
  • Swagger-UI version: 5.17.14 (swagger-ui-react)
  • Swagger/OpenAPI version: 3.1.0

Content & configuration

Whenever I try to resolve a component schema:

Screenshot 2024-11-05 at 00 26 48

Describe the bug you're encountering

In my current app, swagger-ui it not able to resolve references whenever an escape sequence is present in my apps query parameters (not in the spec).

This is a dublicate of #9304 . The fix in the issue does not seem to work. Api-dom is locked on version 1.0.0-alpha.10 (after the supposed fix)

To reproduce...

  • Install react (with vite or similar)
  • Render swagger-ui-react with any valid spec
  • Add query params to your url e.g. ?activeElement=%7B"type"%3A"create_client"%7D
  • Refresh browser
  • You now see the errors

What I have tried

  • Swagger-ui instead of swagger-ui-react
@florianmartens
Copy link
Author

Update: This seems to be the same issue as: #9304 which was maked as fixed. I guess swagger-ui-react is still using the old version api-dom?

The issue: when the BROWSER url (not any url in the spec) contains an escape sequence, swagger-ui stopps being able to resolve the pointers.

@florianmartens
Copy link
Author

florianmartens commented Nov 5, 2024

Here's how I got around the issue. I'm now rendering swagger-ui (moved to the native js library) in an isolated <iframe /> with a Blob URL. I'm getting the scripts from the CDN. I guess this gives me lazy loading for free 🤷🏽‍♂️.

import { ContentHeightContainer } from "@/components/layout-right-drawer";
import { EditorInputProps } from "@/components/module-api-editor/types";
import { useEffect, useRef } from "react";

export function VisualPreviewOpenAPI(props: EditorInputProps) {
  const iframeRef = useRef<HTMLIFrameElement>(null);

  useEffect(() => {
    const iframe = iframeRef.current;
    if (!iframe) return;

    // Create the HTML content
    const html = `
      <!DOCTYPE html>
      <html>
        <head>
          <meta charset="UTF-8">
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.17.14/swagger-ui.css">
          <base href="${window.location.origin}" />
        </head>
        <body>
          <div id="swagger-ui"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.11.0/swagger-ui-bundle.js"></script>
          <script>
            window.onload = function() {
              const ui = SwaggerUIBundle({
                spec: ${JSON.stringify(props.value.data)},
                dom_id: '#swagger-ui',
                presets: [
                  SwaggerUIBundle.presets.apis,
                  SwaggerUIBundle.SwaggerUIStandalonePreset
                ],
                layout: "BaseLayout",
                deepLinking: false,
              });
            }
          </script>
        </body>
      </html>
    `;

    // Create a blob URL from the HTML content
    const blob = new Blob([html], { type: "text/html" });
    const blobUrl = URL.createObjectURL(blob);

    // Set the iframe src to the blob URL
    iframe.src = blobUrl;

    // Clean up the blob URL when the component unmounts
    return () => {
      URL.revokeObjectURL(blobUrl);
    };
  }, [props.value.data]);

  return (
    <ContentHeightContainer>
      <iframe
        ref={iframeRef}
        style={{
          width: "100%",
          height: "100%",
          border: "none",
          minHeight: "500px",
        }}
        sandbox="allow-scripts"
      />
    </ContentHeightContainer>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant