Skip to content

Commit

Permalink
Merge pull request #80 from mt35-rs/issue-79
Browse files Browse the repository at this point in the history
fix: this uses proper schema path for errors
  • Loading branch information
danielgtaylor committed Jan 5, 2023
2 parents bae3143 + 9ba4221 commit a312f61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
32 changes: 28 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,30 @@ func AddAllowedHeaders(name ...string) {
// ContextFromRequest returns a Huma context for a request, useful for
// accessing high-level convenience functions from e.g. middleware.
func ContextFromRequest(w http.ResponseWriter, r *http.Request) Context {
schemasPath := ""
docsPath := ""
specPath := ""
urlPrefix := ""
disableSchemaProperty := false

router := GetRouter(r.Context())
if router != nil {
docsPath = router.DocsPath()
schemasPath = router.SchemasPath()
specPath = router.OpenAPIPath()
urlPrefix = router.urlPrefix
disableSchemaProperty = router.disableSchemaProperty
}

return &hcontext{
Context: r.Context(),
ResponseWriter: w,
r: r,
Context: r.Context(),
ResponseWriter: w,
r: r,
docsPath: docsPath,
schemasPath: schemasPath,
specPath: specPath,
urlPrefix: urlPrefix,
disableSchemaProperty: disableSchemaProperty,
}
}

Expand Down Expand Up @@ -106,6 +126,10 @@ func (c *hcontext) WithValue(key, value interface{}) Context {
errors: append([]error{}, c.errors...),
op: c.op,
closed: c.closed,
docsPath: c.docsPath,
schemasPath: c.schemasPath,
specPath: c.specPath,
urlPrefix: c.urlPrefix,
disableSchemaProperty: c.disableSchemaProperty,
}
}
Expand Down Expand Up @@ -313,7 +337,7 @@ func (c *hcontext) writeModel(ct string, status int, model interface{}) {
// Some automatic responses won't be registered but will have an error model
// returned. We should support these as well.
if modelType == reflect.TypeOf(&ErrorModel{}) {
modelRef = "/" + modelType.Elem().Name()
modelRef = c.schemasPath + "/" + modelType.Elem().Name()
}
}

Expand Down
24 changes: 14 additions & 10 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,27 +600,31 @@ func New(docs, version string) *Router {

r.docsHandler = RapiDocHandler(r)

updateReq := func(req *http.Request) *http.Request {
reqContext := req.Context()
// Inject the operation info before other middleware so that the later
// middleware will have access to it.
withOpID := context.WithValue(reqContext, opIDContextKey, &OperationInfo{})
// Add the router so we can query the router for information in
// request handlers
withRouter := context.WithValue(withOpID, routerContextKey, r)
return req.WithContext(withRouter)
}

// Error handlers
r.mux.NotFound(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := ContextFromRequest(w, r)
ctx := ContextFromRequest(w, updateReq(r))
ctx.WriteError(http.StatusNotFound, fmt.Sprintf("Cannot find %s", r.URL.String()))
}))

r.mux.MethodNotAllowed(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := ContextFromRequest(w, r)
ctx := ContextFromRequest(w, updateReq(r))
ctx.WriteError(http.StatusMethodNotAllowed, fmt.Sprintf("No handler for method %s", r.Method))
}))

r.Middleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Inject the operation info before other middleware so that the later
// middleware will have access to it.
reqContext := req.Context()
withOpID := context.WithValue(reqContext, opIDContextKey, &OperationInfo{})
withRouter := context.WithValue(withOpID, routerContextKey, r)
req = req.WithContext(withRouter)

next.ServeHTTP(w, req)
next.ServeHTTP(w, updateReq(req))

// Automatically add links to OpenAPI and docs.
if req.URL.Path == "/" {
Expand Down

0 comments on commit a312f61

Please sign in to comment.