-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make output formats more explicit (#1110)
* only call jsonSerialize() for HTTP output when it is required * always output JSON from task.run() Closes #979
- Loading branch information
Showing
8 changed files
with
102 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2024 ODK Central Developers | ||
// See the NOTICE file at the top-level directory of this distribution and at | ||
// https://github.com/getodk/central-backend/blob/master/NOTICE. | ||
// This file is part of ODK Central. It is subject to the license terms in | ||
// the LICENSE file found in the top-level directory of this distribution and at | ||
// https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central, | ||
// including this file, may be copied, modified, propagated, or distributed | ||
// except according to the terms contained in the LICENSE file. | ||
// | ||
// We wrap almost all our Express resource declarations in one of the endpoint | ||
// functions found below. These functions help deal with the possible return | ||
// values from the business logic, performing useful tasks like resolving | ||
// `Promise`s, managing output `Stream`s, and handling errors. | ||
// | ||
// There are specialized versions of endpoint for OpenRosa and OData-related | ||
// actions. | ||
|
||
// Standard simple serializer for object output. | ||
// Behaves a little differently at the root call vs nested; because we want eg | ||
// Array[User] to automatically serialize each User, we want to delve into the | ||
// list and rerun the serializer for each entry given an Array. But we don't want | ||
// to individually serialize plain objects to text or we end up with an array | ||
// of JSON strings. | ||
const serialize = (obj) => { | ||
if (obj === undefined) | ||
return null; | ||
else if (typeof obj?.forApi === 'function') | ||
return obj.forApi(); | ||
else if (Array.isArray(obj)) | ||
return obj.map(serialize); | ||
else | ||
return obj; | ||
}; | ||
|
||
module.exports = (obj) => JSON.stringify(serialize(obj)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const should = require('should'); | ||
const appRoot = require('app-root-path'); | ||
const jsonSerialize = require(appRoot + '/lib/util/json-serialize'); | ||
|
||
describe('util/json-serialize', () => { | ||
it('should convert nullish values to valid JSON', () => { | ||
should(jsonSerialize(null)).equal('null'); | ||
should(jsonSerialize(undefined)).equal('null'); | ||
}); | ||
|
||
it('should call forApi on the target if it exists', () => { | ||
jsonSerialize({ forApi: () => 42 }).should.eql('42'); | ||
}); | ||
|
||
it('should convert strings to valid JSON', () => { | ||
jsonSerialize('hello').should.equal('"hello"'); | ||
}); | ||
|
||
it('should jsonify any other values it finds', () => { | ||
jsonSerialize(42).should.equal('42'); | ||
jsonSerialize({ x: 1 }).should.equal('{"x":1}'); | ||
}); | ||
|
||
it('should subserialize each element if an array is found', () => { | ||
jsonSerialize([ | ||
'hello', | ||
{ forApi: () => 42 }, | ||
[ 'world', | ||
{ forApi: () => 23 } ] | ||
]).should.eql('["hello",42,["world",23]]'); | ||
}); | ||
|
||
it('should not subserialize plain objects within an array', () => { | ||
jsonSerialize([{ a: 1 }, { b: 2, c: 3 }]).should.eql('[{"a":1},{"b":2,"c":3}]'); | ||
}); | ||
}); |