Skip to content
Merged
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
52 changes: 52 additions & 0 deletions packages/angular/src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1963,4 +1963,56 @@ describe('angular HttpClient generator', () => {
expect(getHttpClientReturnTypes(['getPetById'])).toBe('');
});
});

// ── urlEncodeParameters ─────────────────────────────────────────────

describe('urlEncodeParameters', () => {
const createOptionsWithUrlEncode = (urlEncodeParameters: boolean) => {
const output = createOutput({ urlEncodeParameters });
return createGeneratorOptions({
route: '/api/pets/${petId}',
context: createContextSpec(output),
});
};

it('encodes path parameters when urlEncodeParameters is true', () => {
const verbOption = createVerbOption();
const options = createOptionsWithUrlEncode(true);

const impl = generateHttpClientImplementation(verbOption, options);

expect(impl).toContain(
'`/api/pets/${encodeURIComponent(String(petId))}`',
);
expect(impl).not.toContain('`/api/pets/${petId}`');
});

it('leaves the route unchanged when urlEncodeParameters is false', () => {
const verbOption = createVerbOption();
const options = createOptionsWithUrlEncode(false);

const impl = generateHttpClientImplementation(verbOption, options);

expect(impl).toContain('`/api/pets/${petId}`');
expect(impl).not.toContain('encodeURIComponent');
});

it('encodes the route passed to the mutator config', () => {
const verbOption = createVerbOption({
mutator: {
name: 'customInstance',
path: './mutator',
default: true,
hasThirdArg: false,
hasSecondArg: false,
} as GeneratorVerbOptions['mutator'],
});
const options = createOptionsWithUrlEncode(true);

const impl = generateHttpClientImplementation(verbOption, options);

expect(impl).toContain('/api/pets/${encodeURIComponent(String(petId))}');
expect(impl).not.toContain('url: `/api/pets/${petId}`');
});
});
});
13 changes: 12 additions & 1 deletion packages/angular/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
type GetterProp,
GetterPropType,
isBoolean,
makeRouteSafe,
pascal,
toObjectString,
} from '@orval/core';
Expand Down Expand Up @@ -316,8 +317,18 @@ export const generateHttpClientImplementation = (
paramsSerializer,
paramsFilter,
}: GeneratorVerbOptions,
{ route, context }: HttpClientGeneratorContext,
{ route: _route, context }: HttpClientGeneratorContext,
) => {
// Opt-in URL-encoding of path parameters (`urlEncodeParameters`), applied once
// at the single point the route enters this builder so the mutator config and
// every inline interpolation below stay consistent. `makeRouteSafe` is not
// idempotent — applying it more than once double-encodes — so it must run here
// exactly once and nowhere downstream.
let route = _route;
if (context.output.urlEncodeParameters) {
route = makeRouteSafe(route);
}

const isRequestOptions = override.requestOptions !== false;
const isFormData = !override.formData.disabled;
const isFormUrlEncoded = override.formUrlEncoded !== false;
Expand Down
37 changes: 37 additions & 0 deletions packages/angular/src/http-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2523,4 +2523,41 @@ describe('angular httpResource generator', () => {
expect(healthFile?.content).not.toContain('getPetByIdResource');
});
});

// ── urlEncodeParameters ─────────────────────────────────────────────

describe('urlEncodeParameters', () => {
const generateRetrievalHeader = (urlEncodeParameters: boolean): string => {
const verbOption = createVerbOption();
routeRegistry.set('getPetById', '/api/pets/${petId}');

return generateHttpResourceHeader({
title: 'PetService',
isRequestOptions: true,
isMutator: false,
isGlobalMutator: false,
provideIn: 'root',
hasAwaitedType: false,
output: createOutput({ urlEncodeParameters }),
verbOptions: { getPetById: verbOption },
clientImplementation: '',
} as never);
};

it('encodes the signal path parameter when urlEncodeParameters is true', () => {
const header = generateRetrievalHeader(true);

expect(header).toContain(
'`/api/pets/${encodeURIComponent(String(petId()))}`',
);
expect(header).not.toContain('`/api/pets/${petId()}`');
});

it('leaves the route unchanged when urlEncodeParameters is false', () => {
const header = generateRetrievalHeader(false);

expect(header).toContain('`/api/pets/${petId()}`');
expect(header).not.toContain('encodeURIComponent');
});
});
});
11 changes: 10 additions & 1 deletion packages/angular/src/http-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isSyntheticDefaultImportsAllow,
jsDoc,
kebab,
makeRouteSafe,
type NormalizedOutputOptions,
type OpenApiInfoObject,
OutputMode,
Expand Down Expand Up @@ -812,13 +813,21 @@ const buildHttpResourceFunction = (
(prop) => prop.type === GetterPropType.NAMED_PATH_PARAMS,
);
const signalRoute = applySignalRoute(route, params, hasNamedParams);
// Opt-in URL-encoding of path parameters (`urlEncodeParameters`). Must run
// AFTER `applySignalRoute`: that step matches the literal `${param}` template
// to rewrite it to its signal form (e.g. `${param()}`), so encoding first
// would stop the substitution from matching. Wrapping the already-rewritten
// form yields `${encodeURIComponent(String(param()))}`, which is correct.
const encodedRoute = output.urlEncodeParameters
? makeRouteSafe(signalRoute)
: signalRoute;

const signalProps = buildSignalProps(props, params);
const args = toObjectString(signalProps, 'implementation');

const { bodyForm, request, isUrlOnly } = buildResourceRequest(
verbOption,
signalRoute,
encodedRoute,
);

if (uniqueContentTypes.length > 1) {
Expand Down
7 changes: 6 additions & 1 deletion packages/query/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ export const generateAngularHttpRequestFunction = (
formUrlEncoded,
override,
}: GeneratorVerbOptions,
{ route, context }: GeneratorOptions,
{ route: _route, context }: GeneratorOptions,
) => {
let route = _route;
if (context.output.urlEncodeParameters) {
route = makeRouteSafe(route);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this belongs here as makeRouteSafe is used in /fetch/index.ts and /query/client.ts so moving this to here won't it double encode the routes now in Fetch and Tanstack Query?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only affects the angular client (were in generateAngularHttpRequestFunction function).

Other clients are unaffected, and the generated code for them is identical with or without this change.


const isRequestOptions = override.requestOptions !== false;
const isFormData = !override.formData.disabled;
const isFormUrlEncoded = override.formUrlEncoded !== false;
Expand Down
Loading
Loading