Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path param with pattern #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,23 @@ func fieldName(r *registry.Registry) func(name string) string {
}
}

var (
// match {field} or {field=pattern}, return param and pattern
pathParamRegexp = regexp.MustCompile(`{([^=}/]+)(?:=([^}]+))?}`)
)

func renderURL(r *registry.Registry) func(method data.Method) string {
fieldNameFn := fieldName(r)
return func(method data.Method) string {
methodURL := method.URL
reg := regexp.MustCompile("{([^}]+)}")
matches := reg.FindAllStringSubmatch(methodURL, -1)
matches := pathParamRegexp.FindAllStringSubmatch(methodURL, -1)
fieldsInPath := make([]string, 0, len(matches))
if len(matches) > 0 {
slog.Debug("url matches", slog.Any("matches", matches))
for _, m := range matches {
expToReplace := m[0]
fieldName := fieldNameFn(m[1])
fieldNameRaw := m[1]
fieldName := fieldNameFn(fieldNameRaw)
part := fmt.Sprintf(`${req.%s}`, fieldName)
methodURL = strings.ReplaceAll(methodURL, expToReplace, part)
fieldsInPath = append(fieldsInPath, fmt.Sprintf(`"%s"`, fieldName))
Expand Down
8 changes: 8 additions & 0 deletions test/integration/defaultConfig/defaultConfig_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ describe("test default configuration", () => {
});
});

it("http post body check request with path param pattern", async () => {
const result = await CounterService.HTTPPostWithPathParamPattern(
{ a: "first/10", req: { b: 15 }, c: "second/is/23" },
{ pathPrefix: "http://localhost:8081" }
);
expect(result.postResult).to.equal("first/10second/is/23");
});

it("http get request with optional fields", async () => {
const result = await CounterService.HTTPGetWithOptionalFields(
{},
Expand Down
8 changes: 8 additions & 0 deletions test/integration/noStaticClasses/noStaticClasses_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ describe("test with client class", () => {
});
});

it("http post body check request with path param pattern", async () => {
const result = await client.httpPostWithPathParamPattern(
{ a: "first/10", req: { b: 15 }, c: "second/is/23" },
{ pathPrefix: "http://localhost:8081" }
);
expect(result.postResult).to.equal("first/10second/is/23");
});

it("http get request with optional fields", async () => {
const result = await client.httpGetWithOptionalFields({});

Expand Down
16 changes: 16 additions & 0 deletions test/integration/protos/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ message HTTPGetWithZeroValueURLSearchParamsResponse {
ZeroValueMsg zero_value_msg = 3;
}

message HttpPostWithPathParamPatternRequest {
string a = 1;
PostRequest req = 2;
string c = 3;
}

message HttpPostWithPathParamPatternResponse {
string post_result = 1;
}

message OptionalFieldsRequest {}

message OptionalFieldsResponse {
Expand Down Expand Up @@ -237,6 +247,12 @@ service CounterService {
get: "/path/query"
};
}
rpc HTTPPostWithPathParamPattern(HttpPostWithPathParamPatternRequest) returns (HttpPostWithPathParamPatternResponse) {
option (google.api.http) = {
post: "/post/{a=first/*}/{c=**}"
body: "req"
};
}
rpc HTTPGetWithOptionalFields(OptionalFieldsRequest) returns (OptionalFieldsResponse) {
option (google.api.http) = {
get: "/optional"
Expand Down
6 changes: 6 additions & 0 deletions test/integration/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,9 @@ func (r *RealCounterService) HTTPGetWithOptionalFields(ctx context.Context, in *
func p[T any](v T) *T {
return &v
}

func (r *RealCounterService) HTTPPostWithPathParamPattern(ctx context.Context, in *HttpPostWithPathParamPatternRequest) (*HttpPostWithPathParamPatternResponse, error) {
return &HttpPostWithPathParamPatternResponse{
PostResult: in.GetA() + in.GetC(),
}, nil
}
8 changes: 8 additions & 0 deletions test/integration/useProtoNames/useProtoNames_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe("test with original proto names", () => {
});
});

it("http post body check request with path param pattern", async () => {
const result = await CounterService.HTTPPostWithPathParamPattern(
{ a: "first/10", req: { b: 15 }, c: "second/is/23" },
{ pathPrefix: "http://localhost:8081" }
);
expect(result.post_result).to.equal("first/10second/is/23");
});

it("http get request with optional fields", async () => {
const result = await CounterService.HTTPGetWithOptionalFields(
{},
Expand Down