Skip to content

Commit

Permalink
module.string: fix issue with nested controller API (#3351)
Browse files Browse the repository at this point in the history
This commit fixes a bug where API endpoints for nested controllers did
not propagate correctly. The controller handler assumed that the full
HTTP path was being used rather than the truncated path given to
component handlers.

This also fixes an issue in the UI where the URL for deeply nested
modules was calculated incorrectly.

Fixes #3294
  • Loading branch information
rfratto authored Mar 23, 2023
1 parent 831e8f2 commit e008d64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion component/module/string/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package string
import (
"context"
"net/http"
"path"

"github.com/go-kit/log"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -114,6 +115,13 @@ func (c *Component) Handler() http.Handler {
fa := api.NewFlowAPI(c.ctrl, r)
fa.RegisterRoutes("/", r)

r.PathPrefix("/{id}/").Handler(c.ctrl.ComponentHandler())
r.PathPrefix("/{id}/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Re-add the full path to ensure that nested controllers propagate
// requests properly.
r.URL.Path = path.Join(c.opts.HTTPPath, r.URL.Path)

c.ctrl.ComponentHandler().ServeHTTP(w, r)
})

return r
}
10 changes: 9 additions & 1 deletion web/ui/src/features/component/ComponentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ComponentViewProps {

export const ComponentView: FC<ComponentViewProps> = (props) => {
// TODO(rfratto): expand/collapse icon for sections (treat it like Row in grafana dashboard)
console.log(props.component);

const referencedBy = props.component.referencedBy.filter((id) => props.info[id] !== undefined).map((id) => props.info[id]);
const referencesTo = props.component.referencesTo.filter((id) => props.info[id] !== undefined).map((id) => props.info[id]);
Expand Down Expand Up @@ -138,7 +139,10 @@ export const ComponentView: FC<ComponentViewProps> = (props) => {
<section id="module">
<h2>Module components</h2>
<div className={styles.sectionContent}>
<ComponentList components={props.component.moduleInfo} parent={props.component.id} />
<ComponentList
components={props.component.moduleInfo}
parent={pathJoin([props.component.parent, props.component.id])}
/>
</div>
</section>
)}
Expand All @@ -147,6 +151,10 @@ export const ComponentView: FC<ComponentViewProps> = (props) => {
);
};

function pathJoin(paths: (string | undefined)[]): string {
return paths.filter((p) => p && p !== '').join('/');
}

/**
* partitionBody groups a body by attributes and inner blocks, assigning unique
* keys for each.
Expand Down

0 comments on commit e008d64

Please sign in to comment.