From d0199e29005c371d79cb044b212a5b0b4a94ab8d Mon Sep 17 00:00:00 2001 From: Kristian Samuelsson Date: Mon, 19 Feb 2024 19:37:54 +0000 Subject: [PATCH 1/3] feat: add option to disable rendering of large payloads --- docs/usage/configuration.md | 9 ++++++ src/core/components/highlight-code.jsx | 38 ++++++++++++++++++------- test/unit/components/highlight-code.jsx | 24 +++++++++++++--- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 1722c25c573..214f1f83c6a 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -206,6 +206,15 @@ Parameter name | Docker variable | Description Swagger UI has finished rendering a newly provided definition. + + + renderSizeThreshold + + Unavailable + The maximum size (in bytes) of a payload to render. + Any payload above this will not be rendered, but may be available for copy or download. + + syntaxHighlight diff --git a/src/core/components/highlight-code.jsx b/src/core/components/highlight-code.jsx index 29f075e374f..8487f8079f0 100644 --- a/src/core/components/highlight-code.jsx +++ b/src/core/components/highlight-code.jsx @@ -10,6 +10,7 @@ import { CopyToClipboard } from "react-copy-to-clipboard" const HighlightCode = ({value, fileName = "response.txt", className, downloadable, getConfigs, canCopy, language}) => { const config = isFunction(getConfigs) ? getConfigs() : null const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) + const renderSizeThreshold = get(config, "renderSizeThreshold") const rootRef = useRef(null) useEffect(() => { @@ -43,8 +44,24 @@ const HighlightCode = ({value, fileName = "response.txt", className, downloadabl } } + const getRenderValues = () => { + if (renderSizeThreshold) { + const valueSizeInBytes = (new TextEncoder().encode(value)).byteLength + const shouldRenderValue = valueSizeInBytes < renderSizeThreshold + return [shouldRenderValue, valueSizeInBytes] + } + return [true, undefined] + } + const [shouldRenderValue, valueSizeInBytes] = getRenderValues() + return (
+ {!shouldRenderValue && ( +
+ Value is too large ({valueSizeInBytes} bytes), rendering disabled. +
+ )} + {canCopy && (
} - {canSyntaxHighlight - ? - {value} - - :
{value}
- } + {shouldRenderValue && ( + canSyntaxHighlight + ? + {value} + + :
{value}
+ )}
) diff --git a/test/unit/components/highlight-code.jsx b/test/unit/components/highlight-code.jsx index 1df14e788d5..86f9f32a969 100644 --- a/test/unit/components/highlight-code.jsx +++ b/test/unit/components/highlight-code.jsx @@ -3,28 +3,44 @@ import expect from "expect" import { shallow, mount } from "enzyme" import HighlightCode from "core/components/highlight-code" -const fakeGetConfigs = () => ({syntaxHighlight: {activated: true, theme: "agate"}}) +const fakeGetConfigs = (renderSizeThreshold = undefined) => ( + { + renderSizeThreshold: renderSizeThreshold, + syntaxHighlight: { + activated: true, + theme: "agate" + } + }) describe("", () => { it("should render a Download button if downloadable", () => { - const props = {downloadable: true, getConfigs: fakeGetConfigs } + const props = { downloadable: true, getConfigs: fakeGetConfigs } const wrapper = shallow() expect(wrapper.find(".download-contents").length).toEqual(1) }) it("should render a Copy To Clipboard button if copyable", () => { - const props = {canCopy: true, getConfigs: fakeGetConfigs } + const props = { canCopy: true, getConfigs: fakeGetConfigs } const wrapper = shallow() expect(wrapper.find("CopyToClipboard").length).toEqual(1) }) it("should render values in a preformatted element", () => { const value = "test text" - const props = {value: value, getConfigs: fakeGetConfigs} + const props = { value: value, getConfigs: fakeGetConfigs } const wrapper = mount() const preTag = wrapper.find("pre") expect(preTag.length).toEqual(1) expect(preTag.text()).toEqual(value) }) + + it("should not render values larger than threshold", () => { + const value = "aaaaaaaa" //8 bytes + const props = { value: value, getConfigs: () => fakeGetConfigs(7) } + const wrapper = mount() + const infoTag = wrapper.find("div.microlight") + + expect(infoTag.text()).toEqual("Value is too large (8 bytes), rendering disabled.") + }) }) From 9b2e3f25999c4f6f1a1e1a9a92ffad0ba97adb96 Mon Sep 17 00:00:00 2001 From: Kristian Samuelsson Date: Mon, 19 Feb 2024 20:47:40 +0000 Subject: [PATCH 2/3] feat: add option to disable syntax highlighting of large payloads --- docs/usage/configuration.md | 10 ++++- src/core/components/highlight-code.jsx | 16 ++++---- test/unit/components/highlight-code.jsx | 49 +++++++++++++++++++++---- 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 214f1f83c6a..2032274695b 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -226,13 +226,21 @@ Parameter name | Docker variable | Description - syntaxHighlight.activate + syntaxHighlight.activated Unavailable Boolean=true. Whether syntax highlighting should be activated or not. + + syntaxHighlight.sizeThreshold + + Unavailable + The maximum size (in bytes) of a payload to syntax highlight. + Any payload above this will not be syntax highlighted. Use this to avoid long delays caused by larged payloads. + + syntaxHighlight.theme diff --git a/src/core/components/highlight-code.jsx b/src/core/components/highlight-code.jsx index 8487f8079f0..9549fe48a53 100644 --- a/src/core/components/highlight-code.jsx +++ b/src/core/components/highlight-code.jsx @@ -9,8 +9,9 @@ import { CopyToClipboard } from "react-copy-to-clipboard" const HighlightCode = ({value, fileName = "response.txt", className, downloadable, getConfigs, canCopy, language}) => { const config = isFunction(getConfigs) ? getConfigs() : null - const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) const renderSizeThreshold = get(config, "renderSizeThreshold") + const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) + const syntaxHighlightSizeThreshold = canSyntaxHighlight ? get(config, "syntaxHighlight.sizeThreshold", undefined) : undefined const rootRef = useRef(null) useEffect(() => { @@ -45,14 +46,15 @@ const HighlightCode = ({value, fileName = "response.txt", className, downloadabl } const getRenderValues = () => { - if (renderSizeThreshold) { + if (renderSizeThreshold || syntaxHighlightSizeThreshold) { const valueSizeInBytes = (new TextEncoder().encode(value)).byteLength - const shouldRenderValue = valueSizeInBytes < renderSizeThreshold - return [shouldRenderValue, valueSizeInBytes] + const shouldRenderValue = renderSizeThreshold ? valueSizeInBytes < renderSizeThreshold : true + const shouldSyntaxHighlight = syntaxHighlightSizeThreshold ? valueSizeInBytes < syntaxHighlightSizeThreshold : true + return [shouldRenderValue, shouldSyntaxHighlight, valueSizeInBytes] } - return [true, undefined] + return [true, true, undefined] } - const [shouldRenderValue, valueSizeInBytes] = getRenderValues() + const [shouldRenderValue, shouldSyntaxHighlight, valueSizeInBytes] = getRenderValues() return (
@@ -75,7 +77,7 @@ const HighlightCode = ({value, fileName = "response.txt", className, downloadabl } {shouldRenderValue && ( - canSyntaxHighlight + canSyntaxHighlight && shouldSyntaxHighlight ? ( +const defaultSyntaxHighlightConfig = { + activated: true, + theme: "agate" +} + +const fakeGetConfigs = ( + renderSizeThreshold = undefined, + syntaxHighlight = defaultSyntaxHighlightConfig) => ( { renderSizeThreshold: renderSizeThreshold, - syntaxHighlight: { - activated: true, - theme: "agate" - } + syntaxHighlight: syntaxHighlight }) describe("", () => { @@ -27,14 +31,29 @@ describe("", () => { it("should render values in a preformatted element", () => { const value = "test text" - const props = { value: value, getConfigs: fakeGetConfigs } + const syntaxHighlightConfig = { + activated: false + } + const props = { value: value, getConfigs: () => fakeGetConfigs(undefined, syntaxHighlightConfig) } const wrapper = mount() - const preTag = wrapper.find("pre") + const highlighterTag = wrapper.find("SyntaxHighlighter") + expect(highlighterTag.length).toEqual(0) + const preTag = wrapper.find("pre") expect(preTag.length).toEqual(1) expect(preTag.text()).toEqual(value) }) + it("should render values in a syntax highlighted element", () => { + const value = "test text" + const props = { value: value, getConfigs: fakeGetConfigs } + const wrapper = mount() + const syntaxHighlighterTag = wrapper.find("SyntaxHighlighter") + + expect(syntaxHighlighterTag.length).toEqual(1) + expect(syntaxHighlighterTag.text()).toEqual(value) + }) + it("should not render values larger than threshold", () => { const value = "aaaaaaaa" //8 bytes const props = { value: value, getConfigs: () => fakeGetConfigs(7) } @@ -43,4 +62,20 @@ describe("", () => { expect(infoTag.text()).toEqual("Value is too large (8 bytes), rendering disabled.") }) + + it("should not highlight values larger larger than syntax highlight threshold", () => { + const value = "aaaaaaaa" //8 bytes + const syntaxHighlightConfig = { + defaultSyntaxHighlightConfig, + sizeThreshold: 8 + } + const props = { value: value, getConfigs: () => fakeGetConfigs(undefined, syntaxHighlightConfig) } + const wrapper = mount() + const highlighterTag = wrapper.find("SyntaxHighlighter") + expect(highlighterTag.length).toEqual(0) + + const preTag = wrapper.find("pre") + expect(preTag.length).toEqual(1) + expect(preTag.text()).toEqual(value) + }) }) From 72e70325f5d925f1394f7c3b87f5e41e8c7e3b89 Mon Sep 17 00:00:00 2001 From: Kristian Samuelsson Date: Wed, 6 Mar 2024 21:13:25 +0000 Subject: [PATCH 3/3] refactor: change name of render size threshold config --- docs/usage/configuration.md | 2 +- src/core/components/highlight-code.jsx | 2 +- test/unit/components/highlight-code.jsx | 10 +++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 2032274695b..b481e8ebd3a 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -208,7 +208,7 @@ Parameter name | Docker variable | Description - renderSizeThreshold + payload.render.sizeThreshold Unavailable The maximum size (in bytes) of a payload to render. diff --git a/src/core/components/highlight-code.jsx b/src/core/components/highlight-code.jsx index 9549fe48a53..3702c748608 100644 --- a/src/core/components/highlight-code.jsx +++ b/src/core/components/highlight-code.jsx @@ -9,7 +9,7 @@ import { CopyToClipboard } from "react-copy-to-clipboard" const HighlightCode = ({value, fileName = "response.txt", className, downloadable, getConfigs, canCopy, language}) => { const config = isFunction(getConfigs) ? getConfigs() : null - const renderSizeThreshold = get(config, "renderSizeThreshold") + const renderSizeThreshold = get(config, "payload.render.sizeThreshold") const canSyntaxHighlight = get(config, "syntaxHighlight") !== false && get(config, "syntaxHighlight.activated", true) const syntaxHighlightSizeThreshold = canSyntaxHighlight ? get(config, "syntaxHighlight.sizeThreshold", undefined) : undefined const rootRef = useRef(null) diff --git a/test/unit/components/highlight-code.jsx b/test/unit/components/highlight-code.jsx index 84794bc3a04..f0cbaf35a32 100644 --- a/test/unit/components/highlight-code.jsx +++ b/test/unit/components/highlight-code.jsx @@ -9,10 +9,14 @@ const defaultSyntaxHighlightConfig = { } const fakeGetConfigs = ( - renderSizeThreshold = undefined, - syntaxHighlight = defaultSyntaxHighlightConfig) => ( + renderSizeThreshold = undefined, + syntaxHighlight = defaultSyntaxHighlightConfig) => ( { - renderSizeThreshold: renderSizeThreshold, + payload: { + render: { + sizeThreshold: renderSizeThreshold, + } + }, syntaxHighlight: syntaxHighlight })