Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support additional properties #2981

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 0 additions & 7 deletions packages/typespec-ts/src/modular/emitModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,6 @@ function addExtendedDictInfo(
}
modelInterface.extends.push(`Record<string, any>`);
} else {
reportDiagnostic(context.program, {
code: "compatible-additional-properties",
format: {
modelName: modelInterface?.name ?? ""
},
target: NoTarget
});
modelInterface.properties?.push({
name: "additionalProperties",
docs: ["Additional properties"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FunctionDeclarationStructure, StructureKind } from "ts-morph";
import {
SdkArrayType,
SdkDictionaryType,
SdkModelPropertyType,
SdkModelType,
SdkType,
SdkUnionType,
Expand All @@ -10,7 +11,7 @@ import {
import { toCamelCase, toPascalCase } from "../../utils/casingUtils.js";

import { SdkContext } from "../../utils/interfaces.js";
import { getRequestModelMapping } from "../helpers/operationHelpers.js";
import { getAllAncestors, getAllProperties, getRequestModelMapping } from "../helpers/operationHelpers.js";
import { normalizeModelName } from "../emitModels.js";
import { NameType } from "@azure-tools/rlc-common";
import { isAzureCoreErrorType } from "../../utils/modelUtils.js";
Expand Down Expand Up @@ -331,11 +332,7 @@ function buildModelTypeSerializer(
statements: ["return item;"]
};

// This is only handling the compatibility mode, will need to update when we handle additionalProperties property.
const additionalPropertiesSpread = hasAdditionalProperties(type)
? "...item"
: "";

const additionalPropertiesSpread = getAdditionalPropertiesStatement(context, type);
const { directAssignment, propertiesStr } = getRequestModelMapping(
context,
type,
Expand Down Expand Up @@ -365,6 +362,19 @@ function buildModelTypeSerializer(
return serializerFunction;
}

function getAdditionalPropertiesStatement(context: SdkContext, type: SdkModelType): string | undefined {
return hasAdditionalProperties(type)
? (!context.rlcOptions?.compatibilityMode && hasAdditionalPropertiesProperty(type) ? "...item.additionalProperties" : "...item")
: undefined;
}

function hasAdditionalPropertiesProperty(type: SdkModelType) {
const allParents = getAllAncestors(type);
const properties: SdkModelPropertyType[] =
getAllProperties(type, allParents) ?? [];
return properties.some((p) => p.name === "additionalProperties");
}

function buildDictTypeSerializer(
context: SdkContext,
type: SdkDictionaryType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface D {
```

This is the tsp configuration.

```yaml
compatibilityMode: true
```
Expand All @@ -36,4 +37,189 @@ export interface SimpleModel extends Record<string, string> {
export function simpleModelSerializer(item: SimpleModel): any {
return { ...item, propA: item["propA"], propB: item["propB"] };
}
```
```

# Should generate serializer for additional properties with `additionalProperties` property for non-legacy code

## TypeSpec

This is tsp definition.

```tsp
model SimpleModel {
...Record<int32>;
propA: string;
propB: string;
}

@route("/serialize")
interface D {
op bar(@body body: SimpleModel): void;
}
```

This is the tsp configuration.

```yaml
compatibilityMode: false
```

## Provide generated models and its serializer

Generated Models.

```ts models
/** model interface SimpleModel */
export interface SimpleModel {
propA: string;
propB: string;
/** Additional properties */
additionalProperties?: Record<string, number>;
}

export function simpleModelSerializer(item: SimpleModel): any {
return {
...item.additionalProperties,
propA: item["propA"],
propB: item["propB"]
};
}
```

# Should treat a property named with `additionalProperties` as normal property

## TypeSpec

This is tsp definition.

```tsp
model SimpleModel {
additionalProperties: string;
propA: string;
propB: string;
}

@route("/serialize")
interface D {
op bar(@body body: SimpleModel): void;
}
```

This is the tsp configuration.

```yaml
compatibilityMode: true
```

## Provide generated models and its serializer

Generated Models.

```ts models
/** model interface SimpleModel */
export interface SimpleModel {
additionalProperties: string;
propA: string;
propB: string;
}

export function simpleModelSerializer(item: SimpleModel): any {
return {
additionalProperties: item["additionalProperties"],
propA: item["propA"],
propB: item["propB"]
};
}
```

# Should generate serializer for additional properties with `additionalProperties` property for non-legacy code

## TypeSpec

This is tsp definition.

```tsp
model SimpleModel {
...Record<int32>;
propA: string;
propB: string;
}

@route("/serialize")
interface D {
op bar(@body body: SimpleModel): void;
}
```

This is the tsp configuration.

```yaml
compatibilityMode: false
```

## Provide generated models and its serializer

Generated Models.

```ts models
/** model interface SimpleModel */
export interface SimpleModel {
propA: string;
propB: string;
/** Additional properties */
additionalProperties?: Record<string, number>;
}

export function simpleModelSerializer(item: SimpleModel): any {
return {
...item.additionalProperties,
propA: item["propA"],
propB: item["propB"]
};
}
```

# Should not generate `additionalProperties` property for non-legacy code if additional property is the same type

## TypeSpec

This is tsp definition.

```tsp
model SimpleModel {
...Record<string>;
propA: string;
propB: string;
}

@route("/serialize")
interface D {
op bar(@body body: SimpleModel): void;
}
```

This is the tsp configuration.

```yaml
compatibilityMode: false
```

## Provide generated models and its serializer

Generated Models.

```ts models
/** model interface SimpleModel */
export interface SimpleModel extends Record<string, string> {
propA: string;
propB: string;
}

export function simpleModelSerializer(item: SimpleModel): any {
return {
...item,
propA: item["propA"],
propB: item["propB"]
};
}
```
Loading