Skip to content

Commit b824bad

Browse files
joherediv-jiaodi
andauthored
Fix URI template variable name decoding for better service compatibility (#3583)
* Decode URI characters before returning the expanded URI * remove only and regen smoke test --------- Co-authored-by: Jiao Di (MSFT) <[email protected]>
1 parent 3e1d66d commit b824bad

File tree

19 files changed

+359
-18
lines changed

19 files changed

+359
-18
lines changed

packages/typespec-test/test/NetworkAnalytics.Management/generated/typespec-ts/sdk/test/arm-test/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/ai/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/anomalyDetector/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/batch_modular/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/contentsafety_modular/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/eventgrid_modular/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/healthInsights_radiologyinsights/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/loadtesting_modular/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/nestedClient/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

packages/typespec-test/test/openai_generic/generated/typespec-ts/src/static-helpers/urlTemplate.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function expandUrlTemplate(
180180
context: Record<string, any>,
181181
option?: UrlTemplateOptions,
182182
): string {
183-
return template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
183+
const result = template.replace(/\{([^{}]+)\}|([^{}]+)/g, (_, expr, text) => {
184184
if (!expr) {
185185
return encodeReservedComponent(text);
186186
}
@@ -210,4 +210,21 @@ export function expandUrlTemplate(
210210
}
211211
return result.join("");
212212
});
213+
214+
return normalizeUnreserved(result);
215+
}
216+
217+
/**
218+
* Normalize an expanded URI by decoding percent-encoded unreserved characters.
219+
* RFC 3986 unreserved: "-" / "." / "~"
220+
*/
221+
function normalizeUnreserved(uri: string): string {
222+
return uri.replace(/%([0-9A-Fa-f]{2})/g, (match, hex) => {
223+
const char = String.fromCharCode(parseInt(hex, 16));
224+
// Decode only if it's unreserved
225+
if (/[\-.~]/.test(char)) {
226+
return char;
227+
}
228+
return match; // leave other encodings intact
229+
});
213230
}

0 commit comments

Comments
 (0)