From de72dfe551ef3bda3fd70f3ecb0487d6db157f74 Mon Sep 17 00:00:00 2001 From: "Sean T. Allen" Date: Sat, 5 Dec 2026 13:34:20 -0500 Subject: [PATCH] Accept null values in Release, User, and License fields GitHub's API description allows `null` for a release's `name`, `body`, and `published_at`, a user's `gravatar_id`, and a license's `url` and `spdx_id`. The converters read them as strings, so a response with null in any of them failed to convert, including any model that requires a user whose gravatar is null. These fields are now `(String | None)`; a release's `body` is also `None` when it is absent, since the schema doesn't require it. With `License.url` nullable, `Repository.license` no longer needs to turn a license that fails to convert into `None`. GitHub's "Other" license now comes through, and a malformed license fails the repository conversion, as the other optional nested objects already do. This covers the fields chosen in #250. That issue stays open for the other fields GitHub's schema marks nullable. Closes #233 Closes #235 --- .release-notes/next-release.md | 2 +- .release-notes/nullable-model-fields.md | 26 +++++++ github_rest_api/_test.pony | 3 + github_rest_api/_test_json_converters.pony | 72 ++++++++++++++----- github_rest_api/_test_optional_fields.pony | 80 ++++++++++++++++++++-- github_rest_api/license.pony | 16 +++-- github_rest_api/release.pony | 27 +++++--- github_rest_api/repository.pony | 14 ++-- github_rest_api/user.pony | 7 +- 9 files changed, 195 insertions(+), 52 deletions(-) create mode 100644 .release-notes/nullable-model-fields.md diff --git a/.release-notes/next-release.md b/.release-notes/next-release.md index a046999..d4052cf 100644 --- a/.release-notes/next-release.md +++ b/.release-notes/next-release.md @@ -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 diff --git a/.release-notes/nullable-model-fields.md b/.release-notes/nullable-model-fields.md new file mode 100644 index 0000000..dc047dd --- /dev/null +++ b/.release-notes/nullable-model-fields.md @@ -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. diff --git a/github_rest_api/_test.pony b/github_rest_api/_test.pony index 4c15783..605dcf0 100644 --- a/github_rest_api/_test.pony +++ b/github_rest_api/_test.pony @@ -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) diff --git a/github_rest_api/_test_json_converters.pony b/github_rest_api/_test_json_converters.pony index aa54247..4854fb2 100644 --- a/github_rest_api/_test_json_converters.pony +++ b/github_rest_api/_test_json_converters.pony @@ -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" @@ -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) @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/github_rest_api/_test_optional_fields.pony b/github_rest_api/_test_optional_fields.pony index 6f04be8..fb11f18 100644 --- a/github_rest_api/_test_optional_fields.pony +++ b/github_rest_api/_test_optional_fields.pony @@ -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) @@ -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" @@ -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 diff --git a/github_rest_api/license.pony b/github_rest_api/license.pony index a121e0b..1cc7a56 100644 --- a/github_rest_api/license.pony +++ b/github_rest_api/license.pony @@ -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' @@ -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, diff --git a/github_rest_api/release.pony b/github_rest_api/release.pony index 1971aaf..7ee045a 100644 --- a/github_rest_api/release.pony +++ b/github_rest_api/release.pony @@ -9,6 +9,9 @@ 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 @@ -16,12 +19,12 @@ class val Release 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 @@ -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, @@ -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 diff --git a/github_rest_api/repository.pony b/github_rest_api/repository.pony index ac961cb..3119206 100644 --- a/github_rest_api/repository.pony +++ b/github_rest_api/repository.pony @@ -10,9 +10,8 @@ class val Repository and releases, deleting labels, and fetching commits, issues, and pull requests. - `organization`, `network_count`, and `subscribers_count` are `None` when - the response omits the field or its value is null. `license` is `None` when - the response omits the field, its value is null, or it does not convert. + `organization`, `network_count`, `subscribers_count`, and `license` are + `None` when the response omits the field or its value is null. """ let _creds: req.Credentials let id: I64 @@ -637,13 +636,12 @@ primitive RepositoryJSONConverter json("watchers_count").as_i64()? let language = JSONNavUtil.string_or_none(json("language"))? - // TODO(#233): GitHub sends `"url": null` for the "Other" license, which - // License cannot hold, so a license that fails to convert becomes None. + let license_json = json("license") let license = - try - LicenseJSONConverter(json("license"), creds)? - else + if _JSONAbsentOrNull(license_json) then None + else + LicenseJSONConverter(license_json, creds)? end let archived = json("archived").as_bool()? diff --git a/github_rest_api/user.pony b/github_rest_api/user.pony index 2dc6baa..b3c7e3a 100644 --- a/github_rest_api/user.pony +++ b/github_rest_api/user.pony @@ -6,13 +6,14 @@ class val User A GitHub user account. Contains the user's login, profile URLs, and account metadata. The `user_type` field corresponds to the `"type"` key in the GitHub JSON response (renamed because `type` is a Pony keyword). + `gravatar_id` is `None` when its value is null. """ let _creds: req.Credentials let login: String let id: I64 let node_id: String let avatar_url: String - let gravatar_id: String + let gravatar_id: (String | None) let url: String let html_url: String let followers_url: String @@ -32,7 +33,7 @@ class val User id': I64, node_id': String, avatar_url': String, - gravatar_id': String, + gravatar_id': (String | None), url': String, html_url': String, followers_url': String, @@ -79,7 +80,7 @@ primitive UserJSONConverter is req.JSONConverter[User] let id = json("id").as_i64()? let node_id = json("node_id").as_string()? let avatar_url = json("avatar_url").as_string()? - let gravatar_id = json("gravatar_id").as_string()? + let gravatar_id = JSONNavUtil.string_or_none(json("gravatar_id"))? let url = json("url").as_string()? let html_url = json("html_url").as_string()? let followers_url = json("followers_url").as_string()?