UI5 / UI5/typescript

Recursive types don't work for sap.ui.model.json.TypedJSONModel

Open
#532 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
213
Forks
35
Avg merge
37m
Merged PRs (30d)
1

Description

Hello there.

Describe the bug
There is an issue with sap.ui.model.json.TypedJSONModel if recursive types are used.

Let's say we have something like this:

import TypedJSONModel from "sap/ui/model/json/TypedJSONModel";

type OrderItems = {
	OrderId: string;
	Id: string;
	to_Order?: Order;
};
type Order = {
	Id: string;
	to_OrderItems?: { results: OrderItems[] };
};

/**
 * @namespace xxx
 */
export default class TestJSONModel extends TypedJSONModel<typeof TestJSONModel._mInitialData> {
	private static readonly _mInitialData = {
		Orders: [] as Order[]
	};

	test() {
		// errors here
		const value = this.getProperty("/Orders/0/to_OrderItems/results");
	}
}

We get errors:
Image
Which does make sense, of course, but it makes this model unusable for types generated for OData Entities, because they can be indeed recursive.
The example above demonstrates that. So, we have an Order, which can have OrderItem[], and OrderItem may have Order. It goes into recursive and kills the types.

Expected behavior
There should be a possibility to use types, even if they are recursive.
I actually had developed typed JSONModel by myself couple of years ago, and I solved this issue by adding Depth, which sets the max number of inner properties to be accessible.

Let's say we have /Orders/0/to_OrderItems/results as in example above. The depth here would be 4.
/ -> Depth 0
/Orders -> Depth 1
/Orders/0 -> Depth 2
etc.
So I would propose to have that in the TypedJSONModel.
If there are better ideas - would be nice hear them out.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the TypeScript definition and implementation entry point for sap.ui.model.json.TypedJSONModel, then reproduce the recursive Order and OrderItems example from the issue. Investigate how getProperty derives nested paths and determine an agreed limit or other handling for recursive types; done means the example type-checks without making recursive models unusable.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.