diff --git a/huma_test.go b/huma_test.go index e00b7abb..cd21f79f 100644 --- a/huma_test.go +++ b/huma_test.go @@ -2144,6 +2144,45 @@ Content-Type: text/plain assert.Equal(t, 256, resp.Code) }, }, + { + Name: "response-external-schema", + Register: func(t *testing.T, api huma.API) { + type Resp struct { + Body struct { + Greeting string `json:"greeting"` + } + } + + huma.Register(api, huma.Operation{ + Method: http.MethodGet, + Path: "/response", + Responses: map[string]*huma.Response{ + "200": { + Description: "Success", + Content: map[string]*huma.MediaType{ + "application/json": { + Schema: &huma.Schema{ + // Using an external schema should not break. + // https://github.com/danielgtaylor/huma/issues/703 + Ref: "http://example.com/schemas/foo.json", + }, + }, + }, + }, + }, + }, func(ctx context.Context, input *struct{}) (*Resp, error) { + resp := &Resp{} + resp.Body.Greeting = "Hello, world!" + return resp, nil + }) + }, + Method: http.MethodGet, + URL: "/response", + Assert: func(t *testing.T, resp *httptest.ResponseRecorder) { + assert.Equal(t, http.StatusOK, resp.Code) + assert.JSONEq(t, `{"greeting":"Hello, world!"}`, resp.Body.String()) + }, + }, { // Simulate a request with a body that came from another call, which // includes the `$schema` field. It should be allowed to be passed diff --git a/transforms.go b/transforms.go index 2dd0e037..292cdf27 100644 --- a/transforms.go +++ b/transforms.go @@ -6,6 +6,7 @@ import ( "os" "path" "reflect" + "strings" ) type schemaField struct { @@ -49,7 +50,7 @@ func NewSchemaLinkTransformer(prefix, schemasPath string) *SchemaLinkTransformer } func (t *SchemaLinkTransformer) addSchemaField(oapi *OpenAPI, content *MediaType) bool { - if content == nil || content.Schema == nil || content.Schema.Ref == "" { + if content == nil || content.Schema == nil || content.Schema.Ref == "" || !strings.HasPrefix(content.Schema.Ref, "#/") { return true }