Skip to content

Commit 04c04ef

Browse files
committed
feat: Add open api extensions for handler targets.
1 parent 362d217 commit 04c04ef

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

pkg/codeconfig/codeconfig.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func (c *codeConfig) Collect() error {
9797
return nil
9898
}
9999

100+
type apiHandler struct {
101+
worker *v1.ApiWorker
102+
target string
103+
}
104+
100105
// apiSpec produces an open api v3 spec for the requests API name
101106
func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
102107
doc := &openapi3.T{
@@ -111,11 +116,16 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
111116
doc.OpenAPI = "3.0.1"
112117

113118
// Compile an API specification from the functions in the stack for the given API name
114-
workers := make([]*v1.ApiWorker, 0)
119+
workers := make([]*apiHandler, 0)
115120

116121
// Collect all workers
117-
for _, f := range c.functions {
118-
workers = append(workers, f.apis[api].workers...)
122+
for handler, f := range c.functions {
123+
for _, w := range f.apis[api].workers {
124+
workers = append(workers, &apiHandler{
125+
target: containerNameFromHandler(handler),
126+
worker: w,
127+
})
128+
}
119129
}
120130

121131
// loop over workers to build new api specification
@@ -124,7 +134,7 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
124134
for _, w := range workers {
125135
params := make(openapi3.Parameters, 0)
126136
normalizedPath := ""
127-
for _, p := range strings.Split(w.Path, "/") {
137+
for _, p := range strings.Split(w.worker.Path, "/") {
128138
if strings.HasPrefix(p, ":") {
129139
paramName := strings.Replace(p, ":", "", -1)
130140
params = append(params, &openapi3.ParameterRef{
@@ -150,18 +160,25 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
150160
doc.Paths[normalizedPath] = pathItem
151161
}
152162

153-
for _, m := range w.Methods {
163+
for _, m := range w.worker.Methods {
154164
if pathItem.Operations() != nil && pathItem.Operations()[m] != nil {
155165
// If the operation already exists we should fail
156166
// NOTE: This should not happen as operations are stored in a map
157167
// in the api state for functions
158168
return nil, fmt.Errorf("found conflicting operations")
159169
}
160170

161-
// See if the path already exists
162171
doc.AddOperation(normalizedPath, m, &openapi3.Operation{
163172
OperationID: normalizedPath + m,
164173
Responses: openapi3.NewResponses(),
174+
ExtensionProps: openapi3.ExtensionProps{
175+
Extensions: map[string]interface{}{
176+
"x-nitric-target": map[string]interface{}{
177+
"type": "function",
178+
"name": containerNameFromHandler(w.target),
179+
},
180+
},
181+
},
165182
})
166183
}
167184
}

0 commit comments

Comments
 (0)