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
17 changes: 17 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 PropertyBinding from "sap/ui/model/PropertyBinding";

/**
* 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 Down Expand Up @@ -30,6 +31,22 @@ declare module "sap/ui/model/json/TypedJSONModel" {
oContext: TypedJSONContext<Data, Root>,
): PropertyByRelativeBindingPath<Data, Root, Path>;

// Overload for absolute paths
bindProperty<Path extends AbsoluteBindingPath<Data>>(
sPath: Path,
oContext?: undefined,
mParameters?: object,
): PropertyBinding;
// Overload for relative paths
bindProperty<
Path extends RelativeBindingPath<Data, Root>,
Root extends AbsoluteBindingPath<Data>,
>(
sPath: Path,
oContext: TypedJSONContext<Data, Root>,
mParameters?: object,
): PropertyBinding;

setData(oData: Data, bMerge?: boolean): void;

// setProperty with AbsoluteBindingPath (context === undefined),
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
20 changes: 20 additions & 0 deletions test-packages/typed-json-model/webapp/model/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Context from "sap/ui/model/Context";
import PropertyBinding from "sap/ui/model/PropertyBinding";
import JSONModel from "sap/ui/model/json/JSONModel";
import {
AbsoluteBindingPath,
Expand Down Expand Up @@ -57,6 +58,25 @@ export class TypedJSONModel<Data extends object> extends JSONModel {
| PropertyByRelativeBindingPath<Data, Root, Path>;
}

// Overload for absolute paths
bindProperty<Path extends AbsoluteBindingPath<Data>>(
sPath: Path,
oContext?: undefined,
mParameters?: object,
): PropertyBinding;
// Overload for relative paths
bindProperty<Path extends RelativeBindingPath<Data, Root>, Root extends AbsoluteBindingPath<Data>>(
sPath: Path,
oContext: TypedJSONContext<Data, Root>,
mParameters?: object,
): PropertyBinding;
bindProperty<
Path extends AbsoluteBindingPath<Data> | RelativeBindingPath<Data, Root>,
Root extends AbsoluteBindingPath<Data>,
>(sPath: Path, oContext?: TypedJSONContext<Data, Root>, mParameters?: object): PropertyBinding {
return super.bindProperty(sPath, oContext, mParameters);
}

setData(oData: Data, bMerge?: boolean): void {
super.setData(oData, bMerge);
}
Expand Down
61 changes: 61 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,61 @@
/**
* @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 PropertyBinding from "sap/ui/model/PropertyBinding";

interface Person {
name: string;
age: number;
address: {
city: string;
zip: string;
};
orders: Array<{
id: number;
product: string;
}>;
}

const data: Person = {
name: "John",
age: 30,
address: { city: "Walldorf", zip: "69190" },
orders: [{ id: 1, product: "UI5" }],
};

/***********************************************************************************************************************
* bindProperty - Absolute cases
**********************************************************************************************************************/

const model0 = new TypedJSONModel(data);

/** @expect ok */ let propertyBindingAbsolute: PropertyBinding = model0.bindProperty("/name");
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/age");
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/address/city");
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/address/zip");
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/orders/0/id");
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/orders/0/product");

/** @expect ts2345 */ propertyBindingAbsolute = model0.bindProperty("/phone");
/** @expect ts2345 */ propertyBindingAbsolute = model0.bindProperty("/address/country");
/** @expect ts2345 */ propertyBindingAbsolute = model0.bindProperty("/orders/0/price");
/** @expect ts2345 */ propertyBindingAbsolute = model0.bindProperty("/doesNotExist");
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/name", undefined);
/** @expect ok */ propertyBindingAbsolute = model0.bindProperty("/age", undefined);

/***********************************************************************************************************************
* bindProperty - Relative cases
**********************************************************************************************************************/

const context = model0.createBindingContext("/address");

/** @expect ok */ let propertyBindingRelative: PropertyBinding = model0.bindProperty("city", context);
/** @expect ok */ propertyBindingRelative = model0.bindProperty("zip", context);

/** @expect ts2769 */ propertyBindingRelative = model0.bindProperty("country", context);
/** @expect ts2769 */ propertyBindingRelative = model0.bindProperty("phone", context);

/** @expect ts2769 */ propertyBindingAbsolute = model0.bindProperty("/name", "not-a-context");
Loading