Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/dts-generator/src/resources/typed-json-model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare module "sap/ui/model/json/TypedJSONModel" {
import JSONModel from "sap/ui/model/json/JSONModel";
import TypedJSONContext from "sap/ui/model/json/TypedJSONContext";
import Context from "sap/ui/model/Context";
import ClientContextBinding from "sap/ui/model/ClientContextBinding";

/**
* TypedJSONModel is a subclass of JSONModel that provides type-safe access to the model data. It is only available when using UI5 with TypeScript.
Expand All @@ -18,6 +19,19 @@ declare module "sap/ui/model/json/TypedJSONModel" {
fnCallBack?: Function,
bReload?: boolean,
): TypedJSONContext<Data, Path>;
bindContext<Path extends AbsoluteObjectBindingPath<Data>>(
sPath: Path,
oContext?: undefined,
mParameters?: object,
): ClientContextBinding;
bindContext<
Path extends RelativeObjectBindingPath<Data, Root>,
Root extends AbsoluteObjectBindingPath<Data>,
>(
sPath: Path,
oContext?: TypedJSONContext<Data, Root>,
mParameters?: object,
): ClientContextBinding;
getData(): Data;
getProperty<Path extends AbsoluteBindingPath<Data>>(
sPath: Path,
Expand Down Expand Up @@ -82,6 +96,24 @@ declare module "sap/ui/model/json/TypedJSONModel" {
: // if T is not of type object:
never;

/**
* Valid absolute binding path for underlying object types (excludes arrays and primitives).
*
* @example
* type Order = { customer: { address: { city: string } }, items: string[], total: number };
* type ObjectPaths = AbsoluteObjectBindingPath<Order>; // "/customer" | "/customer/address"
*/
export type AbsoluteObjectBindingPath<Type> = {
[Path in AbsoluteBindingPath<Type>]: PropertyByAbsoluteBindingPath<
Type,
Path
> extends Array<unknown>
? never
: PropertyByAbsoluteBindingPath<Type, Path> extends object
? Path
: never;
}[AbsoluteBindingPath<Type>];

/**
* Valid relative binding path in a JSONModel.
* The root of the path is defined by the given root string.
Expand All @@ -98,6 +130,32 @@ declare module "sap/ui/model/json/TypedJSONModel" {
? Rest
: never;

/**
* Valid relative binding path for underlying object types (excludes arrays and primitives).
* The root of the path is defined by the given root string.
*
* @example
* type SalesOrder = { buyer: { id: string, name: string }, items: string[] };
* type PathRelativeToSalesOrder = RelativeObjectBindingPath<SalesOrder, "/buyer">; // never (no nested objects)
*
* type Order = { customer: { address: { city: string } }, total: number };
* type PathInOrder = RelativeObjectBindingPath<Order, "/">; // "customer" | "customer/address"
*/
export type RelativeObjectBindingPath<
Type,
Root extends AbsoluteBindingPath<Type>,
> = {
[Path in RelativeBindingPath<Type, Root>]: PropertyByRelativeBindingPath<
Type,
Root,
Path
> extends Array<unknown>
? never
: PropertyByRelativeBindingPath<Type, Root, Path> extends object
? Path
: never;
}[RelativeBindingPath<Type, Root>];

/**
* The type of a property in a JSONModel identified by the given path.
* Counterpart to {@link AbsoluteBindingPath}.
Expand Down
2 changes: 1 addition & 1 deletion test-packages/typed-json-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ci": "npm run lint && npm run ui5lint && npm run ts-typecheck && npm run test"
},
"devDependencies": {
"@types/openui5": "1.136.0",
"@openui5/types": "^1.146.0",
"@ui5/cli": "^4.0.30",
"@ui5/linter": "^1.20.2",
"eslint": "^9.37.0",
Expand Down
3 changes: 2 additions & 1 deletion test-packages/typed-json-model/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"baseUrl": "./",
"paths": {},
"composite": true,
"outDir": "./dist"
"outDir": "./dist",
"types": ["@openui5/types"]
},
"include": ["./webapp/**/*"],
"exclude": ["./**/*.mjs", "./webapp/**/test/**"]
Expand Down
23 changes: 23 additions & 0 deletions test-packages/typed-json-model/webapp/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import Context from "sap/ui/model/Context";
import JSONModel from "sap/ui/model/json/JSONModel";
import {
AbsoluteBindingPath,
AbsoluteObjectBindingPath,
PropertyByAbsoluteBindingPath,
PropertyByRelativeBindingPath,
RelativeBindingPath,
RelativeObjectBindingPath,
} from "./typing";
import ClientContextBinding from "sap/ui/model/ClientContextBinding";

export class TypedJSONContext<Data extends object, Root extends AbsoluteBindingPath<Data>> extends Context {
constructor(oModel: TypedJSONModel<Data>, sPath: Root) {
Expand Down Expand Up @@ -39,6 +42,26 @@ export class TypedJSONModel<Data extends object> extends JSONModel {
return super.createBindingContext(sPath, oContext, mParameters, fnCallBack, bReload) as TypedJSONContext<Data, Path>;
}

// Overload for absolute paths
bindContext<Path extends AbsoluteObjectBindingPath<Data>>(
sPath: Path,
oContext?: undefined,
mParameters?: object,
): ClientContextBinding;
// Overload for relative paths
bindContext<Path extends RelativeObjectBindingPath<Data, Root>, Root extends AbsoluteObjectBindingPath<Data>>(
sPath: Path,
oContext?: TypedJSONContext<Data, Root>,
mParameters?: object,
): ClientContextBinding;
// Implementation
bindContext<
Path extends AbsoluteObjectBindingPath<Data> | RelativeObjectBindingPath<Data, Root>,
Root extends AbsoluteObjectBindingPath<Data>,
>(sPath: Path, oContext?: TypedJSONContext<Data, Root>, mParameters?: object): ClientContextBinding {
return super.bindContext(sPath, oContext, mParameters);
}

getData(): Data {
return super.getData() as Data;
}
Expand Down
56 changes: 56 additions & 0 deletions test-packages/typed-json-model/webapp/model/test/cases/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @file Various general test cases to test the TypedJSONModel for APIs which always return the same type,
* regardless of the provided path (e.g. getObject, getPath, etc.)
*/

import { TypedJSONModel } from "../../model";
import ClientContextBinding from "sap/ui/model/ClientContextBinding";
import { Placeholder } from "../input";

/***********************************************************************************************************************
* bindContext - Absolute cases
**********************************************************************************************************************/

const data = {
root: {
aString: "string",
anObject: { a: "foo" },
anArray: [],
anArrayOfObjects: [{ aNumber: 1 }],
aPlaceholder: new Placeholder(),
anArrayOfPlaceholders: [new Placeholder()],
aTuple: ["string", 1],
},
};

const model1 = new TypedJSONModel(data);

/** @expect ok */ let clientContextBindingAbsolute: ClientContextBinding = model1.bindContext("/root/anObject");
/** @expect ok */ model1.bindContext("/root/anArrayOfObjects/0");
/** @expect ok */ model1.bindContext("/root/aPlaceholder");
/** @expect ok */ model1.bindContext("/root/anArrayOfPlaceholders/0");

/** @expect ts2769 */ model1.bindContext("/root/anArray");
/** @expect ts2769 */ model1.bindContext("/root/aTuple");
/** @expect ts2769 */ model1.bindContext("/root/aTuple/0");
/** @expect ts2769 */ model1.bindContext("/root/aJsonSafeArray/0");
/** @expect ts2769 */ model1.bindContext("/root/anArrayOfObjects/0/aNumber");
/** @expect ts2769 */ model1.bindContext("/root/anArray/0/doesNotExist");

/***********************************************************************************************************************
* bindContext - Relative cases
**********************************************************************************************************************/

const context = model1.createBindingContext("/root");

/** @expect ok */ let clientContextBindingRelative: ClientContextBinding = model1.bindContext("anObject", context);
/** @expect ok */ model1.bindContext("anArrayOfObjects/0", context);
/** @expect ok */ model1.bindContext("aPlaceholder", context);
/** @expect ok */ model1.bindContext("anArrayOfPlaceholders/0", context);

/** @expect ts2769 */ model1.bindContext("anArray", context);
/** @expect ts2769 */ model1.bindContext("aTuple", context);
/** @expect ts2769 */ model1.bindContext("aTuple/0", context);
/** @expect ts2769 */ model1.bindContext("aJsonSafeArray/0", context);
/** @expect ts2769 */ model1.bindContext("anArrayOfObjects/0/aNumber", context);
/** @expect ts2769 */ model1.bindContext("anArray/0/doesNotExist", context);
34 changes: 34 additions & 0 deletions test-packages/typed-json-model/webapp/model/typing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ export type AbsoluteBindingPath<Type> =
: // if T is not of type object:
never;

/**
* Valid absolute binding path for underlying object types (excludes arrays and primitives).
*
* @example
* type Order = { customer: { address: { city: string } }, items: string[], total: number };
* type ObjectPaths = AbsoluteObjectBindingPath<Order>; // "/customer" | "/customer/address"
*/
export type AbsoluteObjectBindingPath<Type> = {
[Path in AbsoluteBindingPath<Type>]: PropertyByAbsoluteBindingPath<Type, Path> extends Array<unknown>
? never
: PropertyByAbsoluteBindingPath<Type, Path> extends object
? Path
: never;
}[AbsoluteBindingPath<Type>];

/**
* Valid relative binding path in a JSONModel.
* The root of the path is defined by the given root string.
Expand Down Expand Up @@ -87,6 +102,25 @@ export type PropertyByRelativeBindingPath<
RelativePath extends string,
> = PropertyByAbsoluteBindingPath<Type, `${Root}/${RelativePath}`>;

/**
* Valid relative binding path for underlying object types (excludes arrays and primitives).
* The root of the path is defined by the given root string.
*
* @example
* type SalesOrder = { buyer: { id: string, name: string }, items: string[] };
* type PathRelativeToSalesOrder = RelativeObjectBindingPath<SalesOrder, "/buyer">; // never (no nested objects)
*
* type Order = { customer: { address: { city: string } }, total: number };
* type PathInOrder = RelativeObjectBindingPath<Order, "/">; // "customer" | "customer/address"
*/
export type RelativeObjectBindingPath<Type, Root extends AbsoluteBindingPath<Type>> = {
[Path in RelativeBindingPath<Type, Root>]: PropertyByRelativeBindingPath<Type, Root, Path> extends Array<unknown>
? never
: PropertyByRelativeBindingPath<Type, Root, Path> extends object
? Path
: never;
}[RelativeBindingPath<Type, Root>];

/***********************************************************************************************************************
* Helper types to split the types above into separate parts
* to make it easier to read and understand.
Expand Down
Loading
Loading