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
2 changes: 1 addition & 1 deletion .release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Now each connection gets 30 seconds to finish the TCP connect and the TLS handsh

Some optional fields fell back to their default when the response sent a value of the wrong type, the same as when the field was missing. A gist with `"comments_enabled": "false"` converted with `comments_enabled` set to `true`. A nested object that failed to convert, such as a gist's `owner`, became `None` without an error.

Now a present value of the wrong type, or a nested object that fails to convert, fails the conversion and the request fails with a `RequestError`, as it already did for required fields. A missing field or a `null` value still gives the same default as before. The affected fields are `Gist.owner` and `comments_enabled`; `GistFile.content`, `encoding`, and `truncated`; the `user` of `GistComment` and `GistCommit`; and `Repository.organization`, `network_count`, and `subscribers_count`.
Now a present value of the wrong type, or a nested object that fails to convert, fails the conversion and the request fails with a `RequestError`, as it already did for required fields. A missing field or a `null` value still gives the same default as before. The affected fields are `Gist.owner` and `comments_enabled`; `GistFile.content`, `encoding`, and `truncated`; the `user` of `GistComment` and `GistCommit`; and `Repository.organization`, `license`, `network_count`, and `subscribers_count`.

## Rename Issue.respository_url to repository_url

Expand Down
26 changes: 26 additions & 0 deletions .release-notes/nullable-model-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Accept null values in Release, User, and License fields

GitHub's API description allows six fields to be `null` that the library required to be strings: a release's `name`, `body`, and `published_at`, a user's `gravatar_id`, and a license's `url` and `spdx_id`. A response with `null` in any of them failed to convert, and the request failed with a `RequestError`. A `null` `gravatar_id` in a user made every model that requires that user fail to convert, such as a repository's `owner` or an issue's `user`.

These fields are now `(String | None)`, and `None` stands for `null`. A release's `body` is also `None` when the response leaves it out.

A repository whose license GitHub can't identify used to come back with `license` set to `None`, because GitHub's "Other" license has a `null` `url`. Now `license` holds that license, with `name` set to `"Other"` and `url` set to `None`.

Code that reads one of these fields has to handle `None`:

Before:

```pony
env.out.print(release.name)
```

After:

```pony
match release.name
| let name: String => env.out.print(name)
| None => env.out.print("(untitled)")
end
```

The constructors of `Release`, `User`, and `License` also accept `None` for these parameters. Existing calls that pass a `String` still compile.
3 changes: 3 additions & 0 deletions github_rest_api/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ actor \nodoc\ Main is TestList
test(_TestRepoJSONConverterMalformedOptionalFields)
test(_TestRepoJSONConverterNullOptionalFields)
test(_TestRepoJSONConverterOtherLicense)
test(_TestNullableFieldsRejectWrongTypes)
test(_TestReleaseJSONConverterAbsentBody)
test(_TestRepoJSONConverterOwnerNullGravatar)
test(_TestGistJSONConverterPreservesValues)
test(_TestGistJSONConverterMissingField)
test(_TestGistJSONConverterAbsentOptionalFields)
Expand Down
72 changes: 53 additions & 19 deletions github_rest_api/_test_json_converters.pony
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ primitive \nodoc\ _TestRequiredFields
end
end

primitive \nodoc\ _TestAssertOptionalString
"""
Fails the test unless `actual` is `expected`: the same string, or both
`None`.
"""
fun apply(
h: PropertyHelper,
expected: (String | None),
actual: (String | None),
what: String)
=>
match \exhaustive\ (expected, actual)
| (let e: String, let a: String) => h.assert_eq[String](e, a, what)
| (None, None) => None
| (let e: String, None) => h.fail(what + ": expected " + e + ", got None")
| (None, let a: String) => h.fail(what + ": expected None, got " + a)
end

class \nodoc\ _TestGitPersonJSONConverterPreservesValues is UnitTest
fun name(): String => "git-person-json-converter/preserves-values"

Expand Down Expand Up @@ -72,15 +90,17 @@ class \nodoc\ _TestLicenseJSONConverterPreservesValues is UnitTest

fun ref apply(h: TestHelper) ? =>
let creds = _TestDummyCredentials(h)
h.for_all[String](
recover val Generators.ascii_printable(1, 20) end)(
{(base, h)(creds) =>
h.for_all2[String, Bool](
recover val Generators.ascii_printable(1, 20) end,
recover val Generators.bool() end)(
{(base, nulls, h)(creds) =>
let b: String val = base.clone()
let node_id_val: String val = "node_id_" + b
let name_val: String val = "name_" + b
let key_val: String val = "key_" + b
let spdx_id_val: String val = "spdx_id_" + b
let url_val: String val = "url_" + b
let spdx_id_val: (String | None) =
if nulls then None else "spdx_id_" + b end
let url_val: (String | None) = if nulls then None else "url_" + b end
let obj = JSONObject
.update("node_id", node_id_val)
.update("name", name_val)
Expand All @@ -93,8 +113,8 @@ class \nodoc\ _TestLicenseJSONConverterPreservesValues is UnitTest
h.assert_eq[String](node_id_val, license.node_id)
h.assert_eq[String](name_val, license.name)
h.assert_eq[String](key_val, license.key)
h.assert_eq[String](spdx_id_val, license.spdx_id)
h.assert_eq[String](url_val, license.url)
_TestAssertOptionalString(h, spdx_id_val, license.spdx_id, "spdx_id")
_TestAssertOptionalString(h, url_val, license.url, "url")
else
h.fail("converter raised an error")
end
Expand Down Expand Up @@ -1898,9 +1918,13 @@ class \nodoc\ _TestUserJSONConverterPreservesValues is UnitTest
recover val Generators.bool() end)(
{(base, site_admin, h)(creds) =>
let b: String val = base.clone()
// The flag also switches gravatar_id between a string and null.
let gravatar: (String | None) =
if site_admin then None else "gravatar_" + b end
let obj =
_TestUserJSON(b)
.update("id", I64(77))
.update("gravatar_id", gravatar)
.update("type", "type_" + b)
.update("site_admin", site_admin)
try
Expand All @@ -1909,7 +1933,8 @@ class \nodoc\ _TestUserJSONConverterPreservesValues is UnitTest
h.assert_eq[I64](77, user.id)
h.assert_eq[String]("unode_" + b, user.node_id)
h.assert_eq[String]("avatar_" + b, user.avatar_url)
h.assert_eq[String]("gravatar_" + b, user.gravatar_id)
_TestAssertOptionalString(
h, gravatar, user.gravatar_id, "gravatar_id")
h.assert_eq[String]("uurl_" + b, user.url)
h.assert_eq[String]("uhurl_" + b, user.html_url)
h.assert_eq[String]("furl_" + b, user.followers_url)
Expand Down Expand Up @@ -1944,24 +1969,34 @@ class \nodoc\ _TestReleaseJSONConverterPreservesValues is UnitTest

fun ref apply(h: TestHelper) ? =>
let creds = _TestDummyCredentials(h)
h.for_all[String](
recover val Generators.ascii_printable(1, 20) end)(
{(base, h)(creds) =>
h.for_all2[String, Bool](
recover val Generators.ascii_printable(1, 20) end,
recover val Generators.bool() end)(
{(base, nulls, h)(creds) =>
let b: String val = base.clone()
let name: (String | None) = if nulls then None else "rname_" + b end
let body: (String | None) = if nulls then None else "rbody_" + b end
let published: (String | None) =
if nulls then None else "rpa_" + b end
let obj =
_TestReleaseJSON(b)
.update("name", name)
.update("body", body)
.update("published_at", published)
try
let release =
ReleaseJSONConverter(JSONNav(_TestReleaseJSON(b)), creds)?
let release = ReleaseJSONConverter(JSONNav(obj), creds)?
h.assert_eq[I64](5, release.id)
h.assert_eq[String]("rnid_" + b, release.node_id)
h.assert_eq[String]("login_" + b, release.author.login)
h.assert_eq[String]("rtag_" + b, release.tag_name)
h.assert_eq[String]("rtarget_" + b, release.target_commitish)
h.assert_eq[String]("rname_" + b, release.name)
h.assert_eq[String]("rbody_" + b, release.body)
_TestAssertOptionalString(h, name, release.name, "name")
_TestAssertOptionalString(h, body, release.body, "body")
h.assert_eq[Bool](true, release.draft)
h.assert_eq[Bool](false, release.prerelease)
h.assert_eq[String]("rca_" + b, release.created_at)
h.assert_eq[String]("rpa_" + b, release.published_at)
_TestAssertOptionalString(
h, published, release.published_at, "published_at")
h.assert_eq[USize](1, release.assets.size())
h.assert_eq[String]("aname_" + b, release.assets(0)?.name)
h.assert_eq[String]("rurl_" + b, release.url)
Expand All @@ -1981,9 +2016,8 @@ class \nodoc\ _TestReleaseJSONConverterMissingField is UnitTest
fun ref apply(h: TestHelper) =>
let required: Array[String] val =
[ "id"; "node_id"; "author"; "tag_name"; "target_commitish"; "name"
"body"; "draft"; "prerelease"; "created_at"; "published_at"; "assets"
"url"; "assets_url"; "upload_url"; "html_url"; "tarball_url"
"zipball_url" ]
"draft"; "prerelease"; "created_at"; "published_at"; "assets"; "url"
"assets_url"; "upload_url"; "html_url"; "tarball_url"; "zipball_url" ]
_TestRequiredFields(
h, ReleaseJSONConverter, _TestReleaseJSON("test"), required)

Expand Down
80 changes: 75 additions & 5 deletions github_rest_api/_test_optional_fields.pony
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class \nodoc\ _TestRepoJSONConverterMalformedOptionalFields is UnitTest
h,
RepositoryJSONConverter,
_TestRepositoryJSON("test"),
[ ("organization", JSONObject); ("organization", "ponylang")
[ ("license", JSONObject); ("license", "MIT"); ("license", I64(1))
("organization", JSONObject); ("organization", "ponylang")
("network_count", "1"); ("network_count", true)
("network_count", JSONObject)
("subscribers_count", "1"); ("subscribers_count", true)
Expand Down Expand Up @@ -226,9 +227,8 @@ class \nodoc\ _TestGistCommitJSONConverterOptionalUser is UnitTest

class \nodoc\ _TestRepoJSONConverterOtherLicense is UnitTest
"""
GitHub reports an unidentified license as "Other" with a null `url`.
`License.url` cannot hold null yet (#233), so the repository must still
convert, with `license` set to `None`.
GitHub reports an unidentified license as "Other" with a null `url`. The
repository converts and keeps the license, with `url` set to `None`.
"""
fun name(): String => "repo-json-converter/other-license"

Expand All @@ -244,7 +244,77 @@ class \nodoc\ _TestRepoJSONConverterOtherLicense is UnitTest
try
let repo =
RepositoryJSONConverter(JSONNav(json), _TestDummyCredentials(h))?
h.assert_true(repo.license is None, "license should be None")
match \exhaustive\ repo.license
| let l: License =>
h.assert_eq[String]("Other", l.name)
h.assert_true(l.url is None, "url should be None")
match \exhaustive\ l.spdx_id
| let id: String => h.assert_eq[String]("NOASSERTION", id)
| None => h.fail("spdx_id should be NOASSERTION")
end
| None => h.fail("the Other license should be kept")
end
else
h.fail("a repository with the Other license should convert")
end

class \nodoc\ _TestNullableFieldsRejectWrongTypes is UnitTest
"""
The fields that accept null still reject a value of the wrong type.
"""
fun name(): String => "nullable-fields/reject-wrong-types"

fun ref apply(h: TestHelper) =>
_TestRejectsMalformed(
h,
UserJSONConverter,
_TestUserJSON("test"),
[("gravatar_id", I64(1)); ("gravatar_id", true)])
_TestRejectsMalformed(
h,
LicenseJSONConverter,
_TestLicenseJSON("test"),
[ ("url", I64(1)); ("url", true); ("spdx_id", I64(1))
("spdx_id", JSONObject) ])
_TestRejectsMalformed(
h,
ReleaseJSONConverter,
_TestReleaseJSON("test"),
[ ("name", I64(1)); ("name", true); ("body", I64(1)); ("body", false)
("published_at", I64(1)); ("published_at", JSONObject) ])

class \nodoc\ _TestReleaseJSONConverterAbsentBody is UnitTest
"""
GitHub's schema does not require a release's `body`, so a release without
one converts with `body` set to `None`.
"""
fun name(): String => "release-json-converter/absent-body"

fun ref apply(h: TestHelper) =>
let json = _TestReleaseJSON("test").remove("body")
try
let release =
ReleaseJSONConverter(JSONNav(json), _TestDummyCredentials(h))?
h.assert_true(release.body is None, "body should be None")
else
h.fail("a release without a body should convert")
end

class \nodoc\ _TestRepoJSONConverterOwnerNullGravatar is UnitTest
"""
A repository whose owner has a null `gravatar_id` converts, because the
nested user accepts null there.
"""
fun name(): String => "repo-json-converter/owner-null-gravatar"

fun ref apply(h: TestHelper) =>
let owner = _TestUserJSON("test").update("gravatar_id", None)
let json = _TestRepositoryJSON("test").update("owner", owner)
try
let repo =
RepositoryJSONConverter(JSONNav(json), _TestDummyCredentials(h))?
h.assert_true(
repo.owner.gravatar_id is None, "gravatar_id should be None")
else
h.fail("a repository whose owner has a null gravatar_id should convert")
end
16 changes: 9 additions & 7 deletions github_rest_api/license.pony
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ use req = "request"
class val License
"""
A repository license as returned by the GitHub REST API. Contains the
license's SPDX identifier, display name, and key.
license's SPDX identifier, display name, and key. `spdx_id` and `url` are
`None` when their value is null. GitHub's "Other" license, for a license it
can't identify, has a null `url`.
"""
let _creds: req.Credentials
let node_id: String
let name: String
let key: String
let spdx_id: String
let url: String
let spdx_id: (String | None)
let url: (String | None)

new val create(creds: req.Credentials,
node_id': String,
name': String,
key': String,
spdx_id': String,
url': String)
spdx_id': (String | None),
url': (String | None))
=>
_creds = creds
node_id = node_id'
Expand All @@ -38,8 +40,8 @@ primitive LicenseJSONConverter is req.JSONConverter[License]
let node_id = json("node_id").as_string()?
let name = json("name").as_string()?
let key = json("key").as_string()?
let spdx_id = json("spdx_id").as_string()?
let url = json("url").as_string()?
let spdx_id = JSONNavUtil.string_or_none(json("spdx_id"))?
let url = JSONNavUtil.string_or_none(json("url"))?

License(
creds,
Expand Down
27 changes: 18 additions & 9 deletions github_rest_api/release.pony
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ class val Release
"""
A GitHub release, containing its tag, target commit, release
notes body, draft/prerelease status, and associated assets.

`name`, `body`, and `published_at` are `None` when their value is null.
`body` is also `None` when the response omits it.
"""
let _creds: req.Credentials
let id: I64
let node_id: String
let author: User
let tag_name: String
let target_commitish: String
let name: String
let body: String
let name: (String | None)
let body: (String | None)
let draft: Bool
let prerelease: Bool
let created_at: String
let published_at: String
let published_at: (String | None)
let assets: Array[Asset] val
let url: String
let assets_url: String
Expand All @@ -37,12 +40,12 @@ class val Release
author': User,
tag_name': String,
target_commitish': String,
name': String,
body': String,
name': (String | None),
body': (String | None),
draft': Bool,
prerelease': Bool,
created_at': String,
published_at': String,
published_at': (String | None),
assets': Array[Asset] val,
url': String,
assets_url': String,
Expand Down Expand Up @@ -194,13 +197,19 @@ primitive ReleaseJSONConverter
let tag_name = json("tag_name").as_string()?
let target_commitish =
json("target_commitish").as_string()?
let name = json("name").as_string()?
let body = json("body").as_string()?
let name = JSONNavUtil.string_or_none(json("name"))?
let body_json = json("body")
let body =
if _JSONAbsentOrNull(body_json) then
None
else
body_json.as_string()?
end
let draft = json("draft").as_bool()?
let prerelease = json("prerelease").as_bool()?
let created_at = json("created_at").as_string()?
let published_at =
json("published_at").as_string()?
JSONNavUtil.string_or_none(json("published_at"))?

let assets = recover trn Array[Asset] end
for i in json("assets").as_array()?.values() do
Expand Down
Loading
Loading