Skip to content

adapter.json: Add opt-in deterministic JSON array order - #621

Closed
thammel wants to merge 1 commit into
eclipse-basyx:developfrom
rwth-iat:fix/573-deterministic-json-array-order
Closed

thammel wants to merge 1 commit into
eclipse-basyx:developfrom
rwth-iat:fix/573-deterministic-json-array-order

Conversation

@thammel

@thammel thammel commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Several JSON arrays originate from unordered Python sets: the top-level object lists of an AbstractObjectStore, and the set-valued attributes AssetAdministrationShell.submodel, ConceptDescription.is_case_of and Extension.refers_to. Their iteration order varies between runs because of hash randomization, so serializing the same data twice can yield different files. This makes the output unsuitable for diffing, caching and reproducible builds.

The new opt-in sort_arrays option sorts these arrays by a stable key, while the default behavior stays untouched. Identifiable objects are ordered by their id, references by their type, their key chain and their referred_semantic_id. Deriving the key from these structural attributes rather than from str() or repr() keeps the serialized order independent of the representation methods. The key covers every attribute that Reference.__eq__ considers, because two references differing only in referred_semantic_id are distinct set members whose order would otherwise be decided by the set again.

The option is also exposed as the encoder classes SortingAASToJsonEncoder and SortingStrippedAASToJsonEncoder, following the selection scheme of the existing stripped encoders. Like stripped, the parameter is ignored when a custom encoder class is given, so an encoder remains the single source of truth for how its arrays are ordered.

Not covered by this change

  • The XML adapter, which iterates the same unordered sets and has no equivalent option.
  • The AASX writer, which calls write_aas_json_file without sort_arrays.

Testing

Seven new test cases in JsonSerializationDeterministicOrderTest cover the top-level lists, submodel, is_case_of, refers_to, references differing only in referred_semantic_id, the stripped variant, and the documented "ignored if an encoder is given" contract. They assert the sorted result rather than a specific set order, so they do not depend on set iteration order themselves.

Fixes #573

Several JSON arrays originate from unordered Python sets: the
top-level object lists of an `AbstractObjectStore`, and the set-valued
attributes `AssetAdministrationShell.submodel`,
`ConceptDescription.is_case_of` and `Extension.refers_to`. Their
iteration order varies between runs because of hash randomization, so
serializing the same data twice can yield different files. This makes
the output unsuitable for diffing, caching and reproducible builds.

The new opt-in `sort_arrays` option sorts these arrays by a stable
key, while the default behavior stays untouched. Identifiable objects
are ordered by their `id`, references by their type, their key chain
and their `referred_semantic_id`. Deriving the key from these
structural attributes rather than from `str()` or `repr()` keeps the
serialized order independent of the representation methods. The key
covers every attribute that `Reference.__eq__` considers, because two
references differing only in `referred_semantic_id` are distinct set
members whose order would otherwise be decided by the set again.

The option is also exposed as the encoder classes
`SortingAASToJsonEncoder` and `SortingStrippedAASToJsonEncoder`,
following the selection scheme of the existing stripped encoders. Like
`stripped`, the parameter is ignored when a custom encoder class is
given, so an encoder remains the single source of truth for how its
arrays are ordered.

Fixes eclipse-basyx#573
@s-heppner

Copy link
Copy Markdown
Member

Will be done in #660

@s-heppner s-heppner closed this Nov 23, 2026
s-heppner pushed a commit that referenced this pull request Nov 23, 2026
Previously, the SDK lacked the JSON serialization formats "Metadata",
"ValueOnly" and "Path" of [IDTA-01001]. The `StrippedAASToJsonEncoder`
implemented a format that matched none of them (#651). Therefore, the
server responded with `501 Not Implemented` on `/$value` and `/$path`,
ignored `?extent` and misinterpreted `/$metadata` and `?level=core`
(#650, #651). Additionally, unordered JSON arrays had no defined order,
so equal objects could result in different JSON strings (#573).
Finally, elements of a `SubmodelElementList` never serialized their
`idShort`, even when it was set manually.

Changes in the SDK:

- The new subpackage `adapter.json.formats` offers an encoder for every
  serialization format: `MetadataJsonEncoder`, `ValueOnlyJsonEncoder`,
  `ReferenceJsonEncoder` and `PathJsonEncoder`. All of them subclass
  `AASToJsonEncoder`.
- `AASToJsonEncoder` gets the init argument `deep` for `level=core` and
  the class attribute `with_blob_value` for `extent=WithoutBLOBValue`
  of [IDTA-01002].
- Unordered collections are serialized in a canonical order, defined
  in `adapter._ordering`, and `modelType` is always the first key of a
  `Referable`.
- Elements of a `SubmodelElementList` serialize their `idShort` in JSON
  and XML, unless it was generated.
- In ValueOnly, `xs:decimal` values are rounded to the nearest IEEE
  double. This is tracked in #694.

Changes in the server:

- `app.adapter.modifiers` parses the modifiers centrally and selects
  the matching encoder. The Submodel Repository GET routes support all
  content modifiers, `level` and `extent`. Combinations that
  [IDTA-01002] does not allow are rejected with `400 Bad Request`.
- Operations and Capabilities have no ValueOnly or Metadata
  representation. They are omitted from listings. Requesting them, or
  a `SubmodelElementList` of them, returns `400 Bad Request`.
- In ValueOnly, elements without a value are omitted, except in a
  `SubmodelElementList`, where they are kept as `null` to preserve the
  indices.
- `GET .../submodel-elements/$value` returns one object with the
  idShorts as names. `/submodels/$value` follows the corrected version
  of the API specification ([admin-shell-io/aas-specs-api#713]).
- The idShortPaths of `$path` start with the idShort of the requested
  element. Therefore, an element of a `SubmodelElementList` without an
  `idShort` has no Path representation (`400 Bad Request`).

This changes existing behavior:

- Normal JSON responses of the Submodel Repository omit `Blob` values
  unless `?extent=WithBLOBValue` is given, since the specification
  defines `WithoutBLOBValue` as the default.
- XML only supports `$reference` and the Normal serialization with
  `level=deep` and `extent=WithBLOBValue`. Any other combination,
  including a request without `?extent=WithBLOBValue`, is rejected
  with `406 Not Acceptable`. This breaks existing clients.
- `?level=core` on `/shells` and `/concept-descriptions` is rejected
  with `400 Bad Request`.
- `?limit=0` is rejected with `400 Bad Request`.

#650 and #651 are fixed for GET requests only. PATCH requests are
still wrong.

This contribution was written with the help of Claude Opus 5.5.

Fixes #446
Fixes #573
Fixes #621
Fixes #623
Fixes #643
Fixes #644
Fixes #649

[IDTA-01001]: https://industrialdigitaltwin.io/aas-specifications/IDTA-01001/v3.1.2/mappings/mappings.html
[IDTA-01002]: https://industrialdigitaltwin.io/aas-specifications/IDTA-01002/v3.1.1/specification/interfaces-operation-parameters.html#SerializationModifier
[admin-shell-io/aas-specs-api#713]: admin-shell-io/aas-specs-api#713
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.

adapter.json: Non-deterministic array order in serialized JSON

2 participants