Merge pull request #4 from amarzavery/fixStringBody

string body needs to be handled differently.
This commit is contained in:
Amar Zavery 2017-10-10 17:24:21 -07:00 коммит произвёл GitHub
Родитель 9e28513965 1924927467
Коммит 3c204ad315
13 изменённых файлов: 30 добавлений и 69 удалений

Просмотреть файл

@ -5,3 +5,4 @@ test/
.travis.yml
.gitignore
gulpfile.js
.git

Просмотреть файл

@ -1,3 +1,6 @@
### 0.1.1 - 2017-10-10
- Reverting the change made in #2.
### 0.1.0 - 2017-09-16
- Initial version of ms-rest-js
- Provides support for basic credentials

22
dist/lib/util/utils.js поставляемый
Просмотреть файл

@ -316,24 +316,10 @@ function dispatchRequest(options) {
}
}
catch (err) {
const textResponse = operationResponse.bodyAsText;
// As per, http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.25 and
// https://spacetelescope.github.io/understanding-json-schema/reference/type.html JSON has
// ("null", "boolean", "object", "array", "number", or "string"), or "integer" as basic types.
// For JSON.parse() to a parse a string, the input should have double quotes in it like "\"Some Text\"".
// Since this is not the case, we want to make sure that the response body was actually a string
// and not a malformed JSON object or JSON array. If the response body was null like "null" or
// a number like "10" or a boolean like "true" or "false", then JSON.parse() handles those
// scenarios correctly. If the input does not start with "{" or "[" then it is most probably a string.
if (!(textResponse.startsWith("{") || textResponse.startsWith("["))) {
operationResponse.bodyAsJson = textResponse;
}
else {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${textResponse}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new restError_1.RestError(msg, errCode, res.status, options, res, textResponse);
return Promise.reject(e);
}
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${operationResponse.bodyAsText}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new restError_1.RestError(msg, errCode, res.status, options, res, operationResponse.bodyAsText);
return Promise.reject(e);
}
}
return Promise.resolve(operationResponse);

2
dist/lib/util/utils.js.map поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

4
dist/test/serializationTests.js поставляемый
Просмотреть файл

@ -196,7 +196,9 @@ describe("msrest", function () {
let mapper = { type: { name: "TimeSpan" }, required: false, serializedName: "TimeSpan" };
let duration = moment.duration({ days: 123, hours: 22, minutes: 14, seconds: 12, milliseconds: 11 });
let serializedDateString = Serializer.serialize(mapper, duration, "dateTimeObj");
serializedDateString.should.equal("P123DT22H14M12.010999999998603S");
if (serializedDateString !== "P123DT22H14M12.011S" && serializedDateString !== "P123DT22H14M12.010999999998603S") {
done(new Error(`serializedDateString: ${serializedDateString} from moment is invalid.`));
}
done();
});
it("should correctly serialize an array of primitives", function (done) {

2
dist/test/serializationTests.js.map поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -313,23 +313,10 @@ export async function dispatchRequest(options: WebResource): Promise<HttpOperati
operationResponse.bodyAsJson = JSON.parse(operationResponse.bodyAsText);
}
} catch (err) {
const textResponse = operationResponse.bodyAsText;
// As per, http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.25 and
// https://spacetelescope.github.io/understanding-json-schema/reference/type.html JSON has
// ("null", "boolean", "object", "array", "number", or "string"), or "integer" as basic types.
// For JSON.parse() to a parse a string, the input should have double quotes in it like "\"Some Text\"".
// Since this is not the case, we want to make sure that the response body was actually a string
// and not a malformed JSON object or JSON array. If the response body was null like "null" or
// a number like "10" or a boolean like "true" or "false", then JSON.parse() handles those
// scenarios correctly. If the input does not start with "{" or "[" then it is most probably a string.
if (!(textResponse.startsWith("{") || textResponse.startsWith("["))) {
operationResponse.bodyAsJson = textResponse;
} else {
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${textResponse}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new RestError(msg, errCode, res.status, options, res, textResponse);
return Promise.reject(e);
}
const msg = `Error "${err}" occured while executing JSON.parse on the response body - ${operationResponse.bodyAsText}.`;
const errCode = err.code || "JSON_PARSE_ERROR";
const e = new RestError(msg, errCode, res.status, options, res, operationResponse.bodyAsText);
return Promise.reject(e);
}
}
return Promise.resolve(operationResponse);

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

2
msRestBundle.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -5,7 +5,7 @@
"email": "azsdkteam@microsoft.com",
"url": "https://github.com/Azure/ms-rest-js"
},
"version": "0.1.0",
"version": "0.2.0",
"description": "Isomorphic client Runtime for Typescript/node.js/browser javascript client libraries generated using AutoRest",
"tags": [
"isomorphic",

Просмотреть файл

@ -204,7 +204,9 @@ describe("msrest", function () {
let mapper: msRest.Mapper = { type: { name: "TimeSpan" }, required: false, serializedName: "TimeSpan" };
let duration = moment.duration({ days: 123, hours: 22, minutes: 14, seconds: 12, milliseconds: 11 });
let serializedDateString = Serializer.serialize(mapper, duration, "dateTimeObj");
serializedDateString.should.equal("P123DT22H14M12.010999999998603S");
if (serializedDateString !== "P123DT22H14M12.011S" && serializedDateString !== "P123DT22H14M12.010999999998603S") {
done(new Error(`serializedDateString: ${serializedDateString} from moment is invalid.`));
}
done();
});