Skip to content

Add the endpoint definition layer and request bodies (T13, T13d) - #12

Merged
kirre-bylund merged 3 commits into
devfrom
t13-definition-layer
Nov 25, 2026
Merged

kirre-bylund merged 3 commits into
devfrom
t13-definition-layer

Conversation

@kirre-bylund

@kirre-bylund kirre-bylund commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Implements T13 and T13d from specs-library/specs/discipline/sdk/cpp-sdk/tasks.md.

What this adds

  • src/core/definition.h — RawResponse, the HasNormalizeResponse and HasToJson traits, BuildUrl, BuildDefaultHeaders, BuildBody and Send<Definition>().
  • src/core/query_parameters.{h,cpp} — typed, insertion-ordered, percent-encoded query parameters.
  • include/lootlocker/json_builder.h — the write handle for request bodies.
  • tests/test_definition.cpp — 18 test functions, including the worked ListBroadcastsDef and a POST definition with a nested reused type.

Verification

The task's stated check: the worked ListBroadcastsDef compiles and its typed wrapper returns the same RequestId the executor assigned. TestTypedWrapperReturnsExecutorId asserts exactly that, and also that nothing has been sent yet — the request is queued, not dispatched, until Update().

14/14 CTest targets pass with curl, 13/13 in the -DLOOTLOCKER_MODULE_ONLY=core configuration. Include boundary and module boundary both clean.

Request bodies

You chose the ADL to_json on the request type over a ConstructBody hook. The reason holds up in the code: the nested optionals object that guest, Steam and PSN sessions all send needs a to_json whichever way the outer body is built, so a definition hook would have meant two mechanisms for one job.

JsonBuilder is a separate type from JsonValue, not a mutable one. JsonValue is a path into a shared immutable document, which is what makes it cheap and safe to hold across a callback; making it writable would mean copying on every write or giving up that sharing.

Send<Definition>() detects to_json with a trait, so a request type without one sends no body and no Content-Type — every existing GET definition is unchanged. When there is a body, Content-Type is set by the executor rather than by a definition, because a body without it is a request the server rejects and no definition should have to remember.

Three bugs found while testing, all verified by sabotage

Set(key, "literal") picked the bool overload. const char* to bool is a standard conversion and to std::string is a user-defined one, so every string literal silently became true. Exactly the same trap QueryParameters::Add had. Fixed with an explicit const char* overload.

ToJson() aborted on invalid UTF-8. nlohmann's default error handler throws, and a throw under JSON_NOEXCEPTION is an abort — so a player name with a stray byte would have terminated the process. Now uses error_handler_t::replace. Sabotaging it back to the default exits -1.

NormalizeResponse is optional. The sketch shows it on every definition, but a definition with no endpoint-specific convenience should not have to write an empty one. A SFINAE trait detects it. ListBroadcastsDef omits it, which is what proves the trait works.

Spec changes carried in lootlocker/index#1935

from_json returns Result<Empty> rather than void — a void decoder cannot report a missing field, which is the silent-default failure D23 exists to prevent. The api-sketch's "Request bodies are not yet specified" section is replaced by the decision, and the sketched to_json(JsonValue&, ...) is corrected to JsonBuilder&.

A definition carries everything the generic executor needs for one
endpoint: the wire contract, the request and response types, and the
three hooks D27 specifies. Send<Definition>() is the only entry point,
and it returns the id the executor assigned.

NormalizeResponse is optional. A definition with no endpoint-specific
convenience should not have to write an empty one, so a SFINAE trait
detects it and dispatches. The worked ListBroadcastsDef omits it,
which is what proves the trait works.

rom_json now returns Result<Empty> rather than �oid. A void
decoder cannot report a missing field, which is the silent-default
failure D23 exists to prevent. The spec is revised to match.

LL-Version is pinned to 2021-03-01 rather than omitted: the backend
defaults a missing header to that same value, so omitting it would
silently opt every adopter into the oldest API version.

Found while testing: Add(name, "literal") picked the �ool overload,
because const char* to �ool is a standard conversion and to
std::string is a user-defined one. Every string literal in a query
string became 	rue. Fixed with an explicit const char* overload and
verified by sabotage.

There is no request body, and the spec has no hook for one. Every MVP
endpoint is a GET, so this task is complete without it, but the gap is
now recorded in the api-sketch and is T13c's first job.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings November 25, 2026 10:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved correctness issues remain in response handling, error metadata preservation, locale-independent query formatting, uint64_t parameter support, and decoder validation.

6 open issues
What changed in this PR

Adds the internal endpoint-definition layer for the SDK, connecting typed endpoint definitions and query construction to the existing executor while updating response decoding to report malformed data explicitly.

Changes:

  • Added generic request construction, dispatch, URL and header helpers, response normalization, and ADL-based decoding.
  • Added typed, insertion-ordered, percent-encoded query parameters with numeric formatting.
  • Updated tests, build integration, and documentation for the Result-based decoder contract.
File Description
tests/​test_definition.cpp Adds tests for endpoint dispatch, request construction, response decoding, errors, headers, and query parameter serialization.
tests/​CMakeLists.txt Registers the endpoint-definition test target.
src/​core/​query_parameters.h Declares typed, insertion-ordered query parameter storage and serialization.
src/​core/​query_parameters.cpp Implements query parameter encoding and numeric formatting.
src/​core/​json.h Adds the ADL-based Result JSON decoding contract.
src/​core/​definition.h Defines endpoint metadata, request construction, response decoding, normalization, and executor dispatch.
README.md Documents the updated Result-based custom decoder pattern.
CMakeLists.txt Adds the query parameter implementation to the core library.

Review effort: Lite

Comment thread src/core/definition.h Resolved
Comment thread src/core/definition.h Resolved
Comment thread src/core/query_parameters.cpp Resolved
Comment thread src/core/query_parameters.h Resolved
Comment thread README.md Resolved
Comment thread tests/test_definition.cpp Resolved
The definition layer had no way to send a body, which blocks guest auth
and white-label login - both POST. The owner chose the hook shape: an
ADL 	o_json on the request type, the same mechanism rom_json uses
for responses, rather than a fourth hook on the definition.

The reason is the nested optionals object that guest, Steam and PSN
sessions all send. That type needs a 	o_json whichever way the outer
body is built, so a ConstructBody hook would mean two mechanisms for
one job.

JsonBuilder is a new public type, deliberately separate from
JsonValue. JsonValue is a path into a shared immutable document,
which is what makes it cheap and safe to hold across a callback; making
it writable would mean copying on every write or giving up that sharing.

Send<Definition>() detects 	o_json with a trait. A request type
without one sends no body and no Content-Type, so every existing GET
definition is unchanged. When there is a body, Content-Type is set by
the executor rather than by a definition.

Two traps found while testing, both fixed and both verified by sabotage:

- Set(key, "literal") picked the �ool overload, exactly as
  QueryParameters::Add did. Every string literal became 	rue.
- ToJson() with nlohmann's default error handler aborts on invalid
  UTF-8, because a throw under JSON_NOEXCEPTION is an abort. A player
  name with a stray byte would have terminated the process. Now uses
  error_handler_t::replace.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kirre-bylund kirre-bylund changed the title Add the endpoint definition layer (T13) Add the endpoint definition layer and request bodies (T13, T13d) Nov 25, 2026
Comment thread src/core/definition.h Resolved
Comment thread src/core/definition.h Resolved
Owner findings:

- is-development is not a default header. It is read only on the
  white-label login path, so it belongs in that definition's
  ConstructHeaders rather than on every request.
- LL-SDK-Version and LL-Instance-Identifier were missing. The other
  SDKs send both and the backend's tracing middleware reads both, so
  without them every request is attributed to "unknown". The instance
  identifier is generated once per Client, so two clients in one
  process are distinguishable.

Copilot findings:

- A 204 was parsed as JSON, so EndSession - the one legitimate empty
  success - reported MalformedResponse. Handled before the parser, and
  NormalizeResponse still runs.
- A decode failure replaced the whole error, losing the executor's
  request_id and status_code. Only the code and message are replaced
  now, so a malformed body still reports which request it was.
- %g is governed by the process-wide C locale, so in a comma-decimal
  locale 1.5 became 1,5 and then 1%2C5 on the wire. The separator is
  normalised to '.' rather than calling setlocale, which would change
  global process state.
- QueryParameters had no uint64 overload, so the call was ambiguous
  between int64_t and bool and a definition could not add an unsigned
  wire id at all.
- The README's from_json example and the test's decoder both assigned
  AsString() without validating, so a missing field became an empty
  string - contradicting the contract the same PR documents.

Verified by sabotage: removing the 204 branch fails two assertions, and
dropping the locale normalisation produces ratio=1%2C5.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@kirre-bylund
kirre-bylund merged commit 5c3c7fd into dev Nov 25, 2026
6 checks passed
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.

2 participants