Repository: swagger-api/swagger-ui (JavaScript)

Issue:
Swagger UI is generating incorrect Example Value for a nullable property in openapi 3.1.1
### Q&A (please complete the following information)
 - OS: Windows 10
 - Browser: Firefox
 - Version: 136.0.4
 - Method of installation: https://editor-next.swagger.io/
 - Swagger-UI version: https://editor-next.swagger.io/
 - Swagger/OpenAPI version: latest/ OpenAPI 3.1.1

### Content & configuration
<!--
  Provide us with a way to see what you're seeing,
  so that we can fix your issue.
-->

Example Swagger/OpenAPI definition:
```json
{
  "openapi": "3.1.1",
  "info": {
    "title": "Playground | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:5150"
    }
  ],
  "paths": {
    "/todos": {
      "post": {
        "tags": [
          "TodoEndpoints"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TodoRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "NullableOfPriority": {
        "enum": [
          "Low",
          "Medium",
          "High",
          "Critical",
          null
        ]
      },
      "TodoRequest": {
        "required": [
          "description",
          "priority"
        ],
        "type": "object",
        "properties": {
          "description": {
            "type": [
              "null",
              "string"
            ]
          },
          "dueDate": {
            "type": [
              "null",
              "string"
            ],
            "format": "date-time"
          },
          "priority": {
            "$ref": "#/components/schemas/NullableOfPriority"
          },
          "tags": {
            "type": [
              "null",
              "array"
            ],
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "TodoEndpoints"
    }
  ]
}
```

### Describe the bug you're encountering
<!-- A clear and concise description of what the bug is. -->
Swagger UI produces `null`s for `Description` and `DueDate` properties in the Example Value section:

![Image](https://github.com/user-attachments/assets/39ebd165-543d-41b6-9a02-17479f703192)

### To reproduce...

Steps to reproduce the behavior:
1. Go to https://editor-next.swagger.io/
2. Import the OpenAPI specification above
3. Check the Example Value

### Expected behavior
<!-- A clear and concise description of what you expected to happen. -->
The following for the example value:

![Image](https://github.com/user-attachments/assets/f85d57a5-7c22-42dd-8dad-2cee393eb3d3)

### Additional context or thoughts
<!-- Add any other context about the problem here. -->
In the specification above, if we change the order of members in the type array from:
```json
"description": {
            "type": [
              "null",
              "string"
            ]
          },
```
To:
```json
"description": {
            "type": [
              "string",
              "null"
            ]
          },
```
Swagger UI will produce the correct example value.


Discussion:
Addressed in https://github.com/swagger-api/swagger-ui/pull/10390.
Released as https://github.com/swagger-api/swagger-ui/releases/tag/v5.20.4



Patch:
diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js b/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js
index 7f71364ae4a..84315b3ddb5 100644
--- a/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js
+++ b/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js
@@ -67,7 +67,10 @@ export const foldType = (type) => {
     } else if (type.includes("object")) {
       return "object"
     } else {
-      const pickedType = randomPick(type)
+      const notNullTypes = type.filter((t) => t !== "null")
+      const pickedType = randomPick(
+        notNullTypes.length > 0 ? notNullTypes : type
+      )
       if (ALL_TYPES.includes(pickedType)) {
         return pickedType
       }


Test patch:
diff --git a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js
index a5ff7fc1470..e2a3e88d16b 100644
--- a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js
+++ b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js
@@ -373,6 +373,20 @@ describe("sampleFromSchema", () => {
     expect(sampleFromSchema(definition)).toEqual(expected)
   })
 
+  it("should handle nullable primitive types defined as list of types", function () {
+    const sample = (schema) => sampleFromSchema(fromJS(schema))
+
+    expect(sample({ type: ["string", "null"] })).toStrictEqual("string")
+    expect(sample({ type: ["null", "string"] })).toStrictEqual("string")
+    expect(sample({ type: ["number", "null"] })).toStrictEqual(0)
+    expect(sample({ type: ["null", "number"] })).toStrictEqual(0)
+    expect(sample({ type: ["integer", "null"] })).toStrictEqual(0)
+    expect(sample({ type: ["null", "integer"] })).toStrictEqual(0)
+    expect(sample({ type: ["boolean", "null"] })).toStrictEqual(true)
+    expect(sample({ type: ["null", "boolean"] })).toStrictEqual(true)
+    expect(sample({ type: ["null"] })).toStrictEqual(null)
+  })
+
   it("should return const value", function () {
     const definition = fromJS({ const: 3 })
     const expected = 3