Skip to content

Commit

Permalink
allow empty path and method
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Jun 18, 2024
1 parent 7082415 commit 6d8dcfe
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 69 deletions.
3 changes: 1 addition & 2 deletions internal/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"strings"

"github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen"
"github.com/pb33f/libopenapi"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/telkomindonesia/oapik/internal/util"
Expand Down Expand Up @@ -74,7 +73,7 @@ func (p Proxy) GetName() string {
if name == "" {
name, _ = strings.CutSuffix(path.Base(p.Spec), path.Ext(p.Spec))
}
return codegen.UppercaseFirstCharacter(name)
return name
}

type ProxyOperation struct {
Expand Down
14 changes: 10 additions & 4 deletions internal/proxy/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (pe *ProxyExtension) loadProxy(ctx context.Context) (err error) {

pe.proxied = map[*v3.Operation]*ProxyOperation{}
for m := range orderedmap.Iterate(ctx, pe.docv3.Model.Paths.PathItems) {
for _, op := range util.GetOperationsMap(m.Value()) {
for method, op := range util.GetOperationsMap(m.Value()) {
if op.Extensions == nil {
continue
}
Expand All @@ -105,6 +105,12 @@ func (pe *ProxyExtension) loadProxy(ctx context.Context) (err error) {
} else {
pop.Spec = path.Join(pe.specDir, pop.Spec)
}
if pop.Path == "" {
pop.Path = m.Key()
}
if pop.Method == "" {
pop.Method = method
}

_, err = pop.GetOpenAPIDoc()
if err != nil {
Expand Down Expand Up @@ -141,13 +147,13 @@ func (pe *ProxyExtension) pruneAndPrefixUpstream(ctx context.Context) (err error

for doc, uopPopMap := range upstreams {
docv3, _ := doc.BuildV3Model()
prefix := util.MapFirstEntry(util.MapFirstEntry(uopPopMap).Value).Key.GetName()
prefix := util.MapFirstEntry(util.MapFirstEntry(uopPopMap).Value).Key.GetName() + "-"

// add prefix to operation id
opmap := map[*v3.Operation]struct{}{}
for uop, popmap := range uopPopMap {
for uop := range uopPopMap {
opmap[uop] = struct{}{}
uop.OperationId = util.MapFirstEntry(popmap).Key.GetName() + uop.OperationId
uop.OperationId = prefix + uop.OperationId
}

// delete unused operations and path items
Expand Down
12 changes: 6 additions & 6 deletions internal/proxy/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"golang.org/x/tools/imports"
)

const prefixUpstream = "Upstream"
const prefixUpstream = "upstream-"

func addTemplateFunc(pe ProxyExtension) {
codegen.TemplateFunctions["upstreamOperationID"] = func(opid string) string {
Expand All @@ -27,22 +27,22 @@ func addTemplateFunc(pe ProxyExtension) {
}

uop, _ := v.GetUpstreamOperation()
return prefixUpstream + uop.OperationId
return codegen.ToCamelCase(prefixUpstream + uop.OperationId)
}
return ""
}
codegen.TemplateFunctions["upstream"] = func(opid string) string {
for k, v := range pe.Proxied() {
if opid != k.OperationId {
if opid != codegen.ToCamelCase(k.OperationId) {
continue
}
return v.GetName()
return codegen.ToCamelCase(v.GetName())
}
return ""
}
codegen.TemplateFunctions["upstreams"] = func() (a []string) {
for _, p := range pe.Upstream() {
a = append(a, p.GetName())
a = append(a, codegen.ToCamelCase(p.GetName()))
}
return
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func Generate(ctx context.Context, specPath string, opts GenerateOptions) (bytes

bytes, err = imports.Process("oapi.go", bytes, nil)
if err != nil {
return nil, fmt.Errorf("error formatting Go code %s: %w", bytes, err)
return nil, fmt.Errorf("error formatting Go code: %w", err)
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/templates/proxy/strict/strict-interface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type StrictServerInterface interface {
{{range .}}{{.SummaryAsComment }}
// ({{.Method}} {{.Path}})
{{$opid := .OperationId -}}
{{$opid}}(ctx context.Context, request {{$opid | ucFirst}}RequestObject) ({{ (or (upstreamOperationID $opid) $opid) | ucFirst}}RequestObject, error)
{{$opid}}(ctx context.Context, request {{$opid | ucFirst}}RequestObject) ({{ (or (upstreamOperationID $opid | camelCase) $opid) | ucFirst}}RequestObject, error)
{{end}}{{/* range . */ -}}
}

type StrictUpstreamInterface interface {
{{range upstreams}}
{{.}}() http.HandlerFunc
{{. | camelCase}}() http.HandlerFunc
{{end}}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/proxy/testdata/spec-proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ servers:
security:
- {}
paths:
"/tenants/{tenant-id}/profiles/{profile-id}":
get:
# populate the rest of parameters from an operation defined inside `spec`
x-proxy:
name: profile
"/profiles/{profile-id}":
get:
operationId: GetProfile
Expand Down
119 changes: 104 additions & 15 deletions internal/proxy/testoutput/oapi-proxy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6d8dcfe

Please sign in to comment.