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

Unable to render example from schema when using anyOf or other multiple responses #946

Open
tjperry07 opened this issue Sep 5, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@tjperry07
Copy link
Contributor

Describe the bug

I noticed that it can't render the example from the schema when using anyof. It does know that there are two possible responses, but not what the schema is. I do know each one is correct because they are being used elsewhere and that renders.

      "EnvironmentsResponse": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "description": "A collection of environment information.",
            "anyOf": [
              {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Environment"
                }
              },
              {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/EnvironmentSummary"
                }
              }
            ]
          }
        }
      },

Expected behavior

It should render each response.

Current behavior

Doesn't render each response. Instead returns

{
  "data": [
    null
  ]
}

Possible solution

Steps to reproduce

https://preview-docs.coalesce.io/docs/api/coalesce/get-environments (no schema example)
https://preview-docs.coalesce.io/docs/api/coalesce/get-environment (Schema example working)

They both share the Environment Component.

Every endpoint that has multiple responses doesn't render an example.
List Nodes
Get Node
List Workspace Nodes

It does work in Swagger Editor.

Screenshot 2024-09-05 at 3 54 06 PM

Context

Not able to provide response examples when there are multiple options.

Your Environment

    "@docusaurus/core": "^3.5.2",
    "@docusaurus/preset-classic": "^3.5.2",
    "docusaurus-plugin-openapi-docs": "^4.0.1",
    "docusaurus-theme-openapi-docs": "^4.0.1",
@tjperry07 tjperry07 added the bug Something isn't working label Sep 5, 2024
@tjperry07
Copy link
Contributor Author

I can pop over the file, it's big so I didn't attach it

@tjperry07
Copy link
Contributor Author

coalesce-api.json

🤞🏾 this downloads

@tjperry07
Copy link
Contributor Author

Related to #617

@tjperry07
Copy link
Contributor Author

I wrote a python script to remove the empty responses from the mdx file output. I am using a JSON spec to generate my API files. But someone might find it helpful. It ignores other response examples.

import os
import re

def remove_tab_item(content):
    pattern = re.compile(r'(<TabItem\s+label={"Example \(from schema\)"}\s+value={"Example \(from schema\)"}.*?</TabItem>)', re.DOTALL)
    matches = pattern.findall(content)
    new_content = pattern.sub('', content)
    return new_content, matches

def process_file(file_path, log_file):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    
    new_content, matches = remove_tab_item(content)
    
    if matches:
        with open(log_file, 'a', encoding='utf-8') as log:
            log.write(f"File: {file_path}\n")
            for match in matches:
                log.write("Found TabItem:\n")
                log.write(match)
                log.write("\n\n")
        
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(new_content)
        print(f"Updated and logged: {file_path}")
    else:
        print(f"No changes: {file_path}")

def process_directory(directory, log_file):
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            process_file(file_path, log_file)

# Specify the directory to start from and the log file
start_directory = "docs/api"
log_file_path = "tab_item_log.txt"

# Clear the log file if it exists
open(log_file_path, 'w').close()

# Run the script
process_directory(start_directory, log_file_path)

print(f"Log file created: {log_file_path}")

@robbieaverill
Copy link
Contributor

Does #944 help to fix this issue?

@tjperry07
Copy link
Contributor Author

@robbieaverill looks like it should

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants