Skip to content

Let a JSON API source quote several fields (#179, item 2) - #189

Open
caufieldjh wants to merge 1 commit into
mainfrom
caufieldjh/json-api-multiple-fields
Open

caufieldjh wants to merge 1 commit into
mainfrom
caufieldjh/json-api-multiple-fields

Conversation

@caufieldjh

Copy link
Copy Markdown
Contributor

Addresses item 2 of #179. Item 1 (XML via url:) landed in #188. The third ask in the issue comments, a non-JSON source built from a URL template, is not in this PR.

Problem

A configured JSON API source's content took one JSONPath and kept only its first match. Facts spread across a record, such as openFDA's device_class and regulation_number, never reached the cached text and could not be quoted. A JSON-derived record was also always labelled abstract_only, so a failed quote carried a misleading "only abstract available" note. This is the same mislabelling #164 fixed for Wikipedia.

Change

  • content may be a list or a labelled mapping, not just one JSONPath:
    sources:
      FDA_PRODUCT_CODE:
        url_template: "https://api.fda.gov/device/classification.json?search=product_code:{id}"
        fields:
          title: "$.results[0].device_name"
          content:
            Device name: "$.results[0].device_name"
            Device class: "$.results[0].device_class"
            Regulation number: "$.results[0].regulation_number"
    This is cached as one Label: value line per field, in order. A list gives bare values, one per line. Each path is read through extract_extra_fields, the same reader source_extra_fields uses: first match only, a list value's items joined with spaces, and an empty value left out, label included. A single-path content is unchanged.
  • content_type: a list or mapping defaults to full_text, since a record isn't an abstract. A single path stays abstract_only, as before. A new optional content_type key (abstract_only | full_text) overrides either default.
  • source_extra_fields is now honoured for a configured source's prefix. Its fields are appended under ### name headings and recorded in extra_fields_captured, as for DOI, PMID and ClinicalTrials.
  • Loader validation: only content takes a list or mapping. A source with any other malformed fields entry, or an unknown content_type, is skipped with a warning, so it fails at load rather than during a fetch.

Design choice to review

The full_text default for list and mapping content is a judgement call. The alternative is no default: list and mapping forms would stay abstract_only unless content_type: full_text is set, and the FDA example would then show the misleading abstract note unless configured.

Cache

No cache version bump. An entry already cached keeps its text. After changing a source's fields, cache reference PREFIX:ID --force refetches it. This was checked live: a single-path EBF entry was rewritten with the labelled lines as full_text. The docs say so.

Tests

  • tests/test_json_api_multiple_fields.py (new) uses the real openFDA EBF record (tests/fixtures/json_api/, fetched 2026-11-30, with the openfda lists cut to 3 entries). It covers:
    • loading from YAML, including sources: in the main config file
    • the malformed shapes that are rejected, and content_type validation
    • label lines, list form, dropping empty fields, list values, and the first match of a wildcard
    • the content_type defaults and override
    • source_extra_fields: appended, alone, and for another prefix
    • save → reload → find_text_in_reference for each field, served from the cache
    • no abstract note on a failed quote
  • The existing tests/test_json_api_source.py (30 tests) is unchanged and passes.
  • Locally, the new and related source and loader tests pass (92), as do just doctest (351), just mypy and just format.
  • test_wikipedia_source.py fails on and off on my machine with connect timeouts to its own 127.0.0.1 test server. wikipedia.py is untouched; I'm leaving that to CI.
  • Live check through the CLI against api.fda.gov: Device class: 2 and Regulation number: 872.3690 are valid. Device class: 3 is invalid, with no abstract note.

Docs

docs/how-to/add-reference-source.md has a new "Quoting several fields" section, and its config table lists content_type.

🤖 Generated with Claude Code

content took one JSONPath and kept its first match, so facts spread across
a record (openFDA's device_class and regulation_number) could not be
quoted. content may now also be a list of JSONPaths, one value per line,
or a mapping of labels to JSONPaths, one "Label: value" line each. Each
path is read as source_extra_fields reads one, and an empty value leaves
no label behind.

A list or mapping is a record, not an abstract, so it is cached as
full_text: a failed quote no longer says "only abstract available". A
single path stays abstract_only. A new content_type key overrides either.
source_extra_fields is now honoured for a configured source's prefix, and
a source with a malformed fields entry or content_type is skipped at load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@claude

claude Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 1s —— View job


I'll analyze this and get back to you.

3 similar comments
@claude

claude Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 1s —— View job


I'll analyze this and get back to you.

@claude

claude Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 1s —— View job


I'll analyze this and get back to you.

@claude

claude Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 1s —— View job


I'll analyze this and get back to you.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant