Skip to content

Commit

Permalink
Merge pull request #552 from tdakkota/fix/clean-ref-correctly
Browse files Browse the repository at this point in the history
fix(gen): clean remote references correctly
  • Loading branch information
ernado authored Sep 5, 2022
2 parents 7c5619c + c159263 commit 8bc2103
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gen/gen_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (g *Generator) responseToIR(
return r, nil
}

n, err := pascal(strings.TrimPrefix(ref, "#/components/responses/"))
n, err := pascal(cleanRef(ref))
if err != nil {
return nil, errors.Wrapf(err, "response name: %q", ref)
}
Expand Down
27 changes: 27 additions & 0 deletions gen/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import (
"github.com/go-faster/errors"
)

func cleanRef(ref string) string {
_, result, ok := strings.Cut(ref, "#")
if !ok {
result = ref
}

for _, prefix := range []string{
"/components/schemas/",
"/components/responses/",
"/components/parameters/",
"/components/examples/",
"/components/requestBodies/",
"/components/headers/",
"/components/securitySchemes/",
"/components/links/",
"/components/callbacks/",
"/components/pathItems/",
} {
// Do not try to trim all prefixes, do it only once.
if strings.HasPrefix(result, prefix) {
result = strings.TrimPrefix(result, prefix)
break
}
}
return result
}

type nameGen struct {
parts []string
src []rune
Expand Down
2 changes: 1 addition & 1 deletion gen/schema_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func newSchemaGen(filename string, lookupRef func(ref string) (*ir.Type, bool))
localRefs: map[string]*ir.Type{},
lookupRef: lookupRef,
nameRef: func(ref string) (string, error) {
name, err := pascal(strings.TrimPrefix(ref, "#/components/schemas/"))
name, err := pascal(cleanRef(ref))
if err != nil {
return "", err
}
Expand Down

0 comments on commit 8bc2103

Please sign in to comment.