From c15926347cc598fa1cfb2dc24867a942db020fbc Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 5 Sep 2022 09:58:29 +0300 Subject: [PATCH] fix(gen): clean remote references correctly --- gen/gen_responses.go | 2 +- gen/names.go | 27 +++++++++++++++++++++++++++ gen/schema_gen.go | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/gen/gen_responses.go b/gen/gen_responses.go index 8f0fadd5f..aad5565cd 100644 --- a/gen/gen_responses.go +++ b/gen/gen_responses.go @@ -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) } diff --git a/gen/names.go b/gen/names.go index d56dc74cb..2c9e50e8a 100644 --- a/gen/names.go +++ b/gen/names.go @@ -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 diff --git a/gen/schema_gen.go b/gen/schema_gen.go index 3e4bce45f..39e369b14 100644 --- a/gen/schema_gen.go +++ b/gen/schema_gen.go @@ -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 }