Skip to content

Commit 70c1b3f

Browse files
authored
Code to catch the missing mandatory parameter in query (Azure#15278)
1 parent 0ae2281 commit 70c1b3f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

sdk/core/core-client/src/urlHelpers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ function calculateQueryParameters(
133133
queryParameter,
134134
fallbackObject
135135
);
136-
if (queryParameterValue !== undefined && queryParameterValue !== null) {
136+
if (
137+
(queryParameterValue !== undefined && queryParameterValue !== null) ||
138+
queryParameter.mapper.required
139+
) {
137140
queryParameterValue = operationSpec.serializer.serialize(
138141
queryParameter.mapper,
139142
queryParameterValue,

sdk/core/core-client/test/serviceClient.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,90 @@ describe("ServiceClient", function() {
12761276
assert.include(error.message, "cannot be null or undefined");
12771277
}
12781278
});
1279+
1280+
it("should catch the mandatory parameter missing error in the query", async function() {
1281+
const operationSpec: OperationSpec = {
1282+
baseUrl: "http://localhost:3000",
1283+
path: "/reqopt/global/required/query",
1284+
httpMethod: "GET",
1285+
responses: {
1286+
200: {},
1287+
default: {
1288+
bodyMapper: {
1289+
type: {
1290+
name: "Composite",
1291+
className: "ErrorModel",
1292+
modelProperties: {
1293+
status: {
1294+
serializedName: "status",
1295+
type: {
1296+
name: "Number"
1297+
}
1298+
},
1299+
message: {
1300+
serializedName: "message",
1301+
type: {
1302+
name: "String"
1303+
}
1304+
}
1305+
}
1306+
}
1307+
}
1308+
}
1309+
},
1310+
headerParameters: [
1311+
{
1312+
parameterPath: "accept",
1313+
mapper: {
1314+
defaultValue: "application/json",
1315+
isConstant: true,
1316+
serializedName: "Accept",
1317+
type: {
1318+
name: "String"
1319+
}
1320+
}
1321+
}
1322+
],
1323+
queryParameters: [
1324+
{
1325+
parameterPath: "requiredGlobalQuery",
1326+
mapper: {
1327+
serializedName: "required-global-query",
1328+
required: true,
1329+
type: {
1330+
name: "String"
1331+
}
1332+
}
1333+
}
1334+
],
1335+
serializer: createSerializer()
1336+
};
1337+
1338+
let request: OperationRequest;
1339+
const pipeline = createEmptyPipeline();
1340+
pipeline.addPolicy(serializationPolicy(), { phase: "Serialize" });
1341+
const client = new ServiceClient({
1342+
httpClient: {
1343+
sendRequest: (req) => {
1344+
request = req;
1345+
return Promise.resolve({ request, status: 200, headers: createHttpHeaders() });
1346+
}
1347+
},
1348+
pipeline
1349+
});
1350+
1351+
try {
1352+
await client.sendOperationRequest(
1353+
{
1354+
options: undefined
1355+
},
1356+
operationSpec
1357+
);
1358+
assert.fail("Expected client to throw");
1359+
} catch (error) {
1360+
assert.include(error.message, "cannot be null or undefined");
1361+
}
1362+
});
12791363
});
12801364

12811365
async function testSendOperationRequest(

0 commit comments

Comments
 (0)