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

How to explicitly define response for a route #246

Closed
amsal opened this issue Feb 15, 2024 · 2 comments · Fixed by #247
Closed

How to explicitly define response for a route #246

amsal opened this issue Feb 15, 2024 · 2 comments · Fixed by #247

Comments

@amsal
Copy link

amsal commented Feb 15, 2024

I want to return a binary image response from one of my routes.

https://go.dev/play/p/YlsBFq25iaK

But the generated openapi schema has few definitions I do not want.

"paths": {
    "/image": {
      "post": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "contentEncoding": "base64",
                  "type": "string"
                }
              },
              "image/png": {
                "schema": {
                  "format": "binary",
                  "type": "string"
                }
              }
            },
            "description": "Image response",
            "headers": {
              "Content-Type": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "default": {
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorModel"
                }
              }
            },
            "description": "Error"
          }
        }
      }
    }
  }

Is it possible to change the success response to this? (remove the json response and content-type header)

  "200": {
    "content": {
      "image/png": {
        "schema": {
          "format": "binary",
          "type": "string"
        }
      }
    },
    "description": "Image response"
  }
@danielgtaylor
Copy link
Owner

@amsal the fact that application/json is present appears to be a bug, thanks for reporting! I'll fix this ASAP.

As for the Content-Type header, Huma will document every header in your response and that is by design. I recommend keeping it, but if you absolutely want to remove it you can do so:

delete(api.OpenAPI().Paths["/image"].Post.Responses["200"].Headers, "Content-Type")

https://go.dev/play/p/8OR1Ft4BYAY

@danielgtaylor
Copy link
Owner

@amsal the extra application/json response should be fixed in #247, and I'll publish a new release with the fix soon.

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

Successfully merging a pull request may close this issue.

2 participants