From 792b453a1812c0ff80d7d83ddd4499e7b65632bb Mon Sep 17 00:00:00 2001
From: Oliwia Rogala <oliwia.rogala@smartbear.com>
Date: Fri, 10 May 2024 12:18:40 +0200
Subject: [PATCH 1/2] fix(components): fix rendering for empty examples in
 responses

Refs #9499
---
 src/core/components/response.jsx              |  4 +--
 .../response-empty-examples-object.cy.js      | 16 ++++++++++++
 .../response-empty-examples-object.yaml       | 25 +++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js
 create mode 100644 test/e2e-cypress/static/documents/features/response-empty-examples-object.yaml

diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx
index e5ab42edfc3..9e15be4da87 100644
--- a/src/core/components/response.jsx
+++ b/src/core/components/response.jsx
@@ -134,7 +134,7 @@ export default class Response extends React.Component {
     // Goal: find an example value for `sampleResponse`
     if(isOAS3) {
       sampleSchema = activeMediaType.get("schema")?.toJS()
-      if(examplesForMediaType) {
+      if(examplesForMediaType && examplesForMediaType.size > 0) {
         const targetExamplesKey = this.getTargetExamplesKey()
         const targetExample = examplesForMediaType
           .get(targetExamplesKey, Map({}))
@@ -208,7 +208,7 @@ export default class Response extends React.Component {
                   </small>
                 ) : null}
               </div>
-              {examplesForMediaType ? (
+              {examplesForMediaType && examplesForMediaType.size > 0 ? (
                 <div className="response-control-examples">
                   <small className="response-control-examples__title">
                     Examples
diff --git a/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js b/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js
new file mode 100644
index 00000000000..145f8d74a08
--- /dev/null
+++ b/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js
@@ -0,0 +1,16 @@
+/**
+ * @prettier
+ */
+
+describe("Response examples", () => {
+  it("should render a generated example when an empty examples object is provided", () => {
+    cy.visit("/?url=/documents/features/response-empty-examples-object.yaml")
+      .get("#operations-TEST-test")
+      .click()
+      .get(".example.microlight")
+      .contains("{}")
+      .should("exist")
+      .get(".examples-select-element")
+      .should("not.exist")
+  })
+})
diff --git a/test/e2e-cypress/static/documents/features/response-empty-examples-object.yaml b/test/e2e-cypress/static/documents/features/response-empty-examples-object.yaml
new file mode 100644
index 00000000000..daf01826834
--- /dev/null
+++ b/test/e2e-cypress/static/documents/features/response-empty-examples-object.yaml
@@ -0,0 +1,25 @@
+openapi: 3.1.0
+info:
+  title: TEST
+  description: TEST
+  version: '1.0'
+servers:
+- url: localhost:8080
+tags:
+- name: TEST
+paths:
+  /test-endpoint:
+    get:
+      summary: Test
+      operationId: test
+      responses:
+        '200':
+          description: OK
+          content:
+            application/json:
+              schema:
+                type: object
+              examples: {}
+      description: Test
+      tags:
+      - TEST

From 33fc924af6f25cf0924d1f8eb95c020606835e73 Mon Sep 17 00:00:00 2001
From: Oliwia Rogala <oliwia.rogala@smartbear.com>
Date: Fri, 10 May 2024 13:09:37 +0200
Subject: [PATCH 2/2] apply requested changes

---
 src/core/components/response.jsx                              | 4 ++--
 .../e2e/features/response-empty-examples-object.cy.js         | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx
index 9e15be4da87..f703a9194d5 100644
--- a/src/core/components/response.jsx
+++ b/src/core/components/response.jsx
@@ -134,7 +134,7 @@ export default class Response extends React.Component {
     // Goal: find an example value for `sampleResponse`
     if(isOAS3) {
       sampleSchema = activeMediaType.get("schema")?.toJS()
-      if(examplesForMediaType && examplesForMediaType.size > 0) {
+      if(Map.isMap(examplesForMediaType) && !examplesForMediaType.isEmpty()) {
         const targetExamplesKey = this.getTargetExamplesKey()
         const targetExample = examplesForMediaType
           .get(targetExamplesKey, Map({}))
@@ -208,7 +208,7 @@ export default class Response extends React.Component {
                   </small>
                 ) : null}
               </div>
-              {examplesForMediaType && examplesForMediaType.size > 0 ? (
+              {Map.isMap(examplesForMediaType) && !examplesForMediaType.isEmpty() ? (
                 <div className="response-control-examples">
                   <small className="response-control-examples__title">
                     Examples
diff --git a/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js b/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js
index 145f8d74a08..7c000a6e7a1 100644
--- a/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js
+++ b/test/e2e-cypress/e2e/features/response-empty-examples-object.cy.js
@@ -8,8 +8,8 @@ describe("Response examples", () => {
       .get("#operations-TEST-test")
       .click()
       .get(".example.microlight")
-      .contains("{}")
       .should("exist")
+      .contains("{}")
       .get(".examples-select-element")
       .should("not.exist")
   })