Skip to content

Commit

Permalink
[v6] Extensible enum (#716)
Browse files Browse the repository at this point in the history
* Handle extensible enum

* update smoke tests
  • Loading branch information
joheredi authored Aug 13, 2020
1 parent 5a4bbaa commit 968347a
Show file tree
Hide file tree
Showing 60 changed files with 1,692 additions and 1,361 deletions.
62 changes: 41 additions & 21 deletions src/generators/modelsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import {
OperationGroupDetails
} from "../models/operationDetails";
import { ParameterDetails } from "../models/parameterDetails";
import { ImplementationLocation, Parameter } from "@azure-tools/codemodel";
import {
ImplementationLocation,
Parameter,
SchemaType
} from "@azure-tools/codemodel";
import { KnownMediaType } from "@azure-tools/codegen";
import { getStringForValue } from "../utils/valueHelpers";
import { getLanguageMetadata } from "../utils/languageHelpers";
Expand Down Expand Up @@ -201,7 +205,8 @@ interface GeneratedResponseDetails {
* Extracts the necessary data from the response body to generate a response type
*/
function getBodyProperties({
types: { bodyType }, mediaType
types: { bodyType },
mediaType
}: OperationResponseDetails): GeneratedResponseDetails | undefined {
if (!bodyType && mediaType !== KnownMediaType.Binary) {
return;
Expand All @@ -213,7 +218,9 @@ function getBodyProperties({
// Used when the bodyType is not a primitive, or for binary media types with no defined bodyType.
const mainProperties: OptionalKind<PropertySignatureStructure>[] = [];
// These are the additional default properties to add under the _response property in the response type
const internalResponseProperties: OptionalKind<PropertySignatureStructure>[] = [];
const internalResponseProperties: OptionalKind<
PropertySignatureStructure
>[] = [];

if (bodyType) {
if (hasBodyProperty) {
Expand All @@ -235,21 +242,27 @@ function getBodyProperties({
docs: ["The response body as parsed JSON or XML"],
type: bodyType.typeName,
leadingTrivia: writer => writer.blankLine()
});
}
);
} else if (mediaType === KnownMediaType.Binary) {
mainProperties.push(
{
name: "blobBody",
type: "Promise<Blob>",
docs: ["BROWSER ONLY\n\nThe response body as a browser Blob.\nAlways `undefined` in node.js."],
docs: [
"BROWSER ONLY\n\nThe response body as a browser Blob.\nAlways `undefined` in node.js."
],
hasQuestionToken: true
},
{
name: "readableStreamBody",
type: "NodeJS.ReadableStream",
docs: ["NODEJS ONLY\n\nThe response body as a node.js Readable stream.\nAlways `undefined` in the browser."],
docs: [
"NODEJS ONLY\n\nThe response body as a node.js Readable stream.\nAlways `undefined` in the browser."
],
hasQuestionToken: true
});
}
);
}

return {
Expand Down Expand Up @@ -315,12 +328,14 @@ function buildResponseType(
{
name: "_response",
docs: ["The underlying HTTP response."],
type: innerResponseProperties.length ? Writers.intersectionType(
"coreHttp.HttpResponse",
Writers.objectType({
properties: innerResponseProperties
})
) : "coreHttp.HttpResponse",
type: innerResponseProperties.length
? Writers.intersectionType(
"coreHttp.HttpResponse",
Writers.objectType({
properties: innerResponseProperties
})
)
: "coreHttp.HttpResponse",
leadingTrivia: writer => writer.blankLine()
}
]
Expand Down Expand Up @@ -355,13 +370,13 @@ function buildResponseType(
*/
return intersectionTypes.length > 1
? // Using apply instead of calling the method directly to be able to conditionally pass
// parameters, this way we don't have to have a nested if/else tree to decide which parameters
// to pass, we will pass any intersectionTypes availabe plus the innerType. When there are no intersection types
// we just return innerType
Writers.intersectionType.apply(
Writers,
intersectionTypes as IntersectionTypeParameters
)
// parameters, this way we don't have to have a nested if/else tree to decide which parameters
// to pass, we will pass any intersectionTypes availabe plus the innerType. When there are no intersection types
// we just return innerType
Writers.intersectionType.apply(
Writers,
intersectionTypes as IntersectionTypeParameters
)
: innerTypeWriter;
}

Expand All @@ -370,11 +385,16 @@ const writeChoices = (
modelsIndexFile: SourceFile
) =>
clientDetails.unions.forEach(choice => {
const values = [...choice.values];
if (choice.schemaType === SchemaType.Choice) {
values.push("string");
}

modelsIndexFile.addTypeAlias({
name: choice.name,
docs: [choice.description],
isExported: true,
type: choice.values.join(" | "),
type: values.join(" | "),
trailingTrivia: writer => writer.newLine()
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/models/unionDetails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { SchemaType } from "@azure-tools/codemodel";

/**
* Details of a typed value union, transformed from ChoiceSchema.
*/
Expand All @@ -9,4 +11,5 @@ export interface UnionDetails {
description: string;
serializedName: string;
values: string[];
schemaType: SchemaType;
}
2 changes: 2 additions & 0 deletions src/transforms/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ export function transformChoice(
): UnionDetails {
const metadata = getLanguageMetadata(choice.language);
let name = normalizeName(metadata.name, NameType.Interface);
let schemaType = choice.type;

return {
name,
schemaType,
description: `Defines values for ${metadata.name}.`,
serializedName: metadata.name,
values: choice.choices.map(c =>
Expand Down
4 changes: 2 additions & 2 deletions test/integration/generated/bodyArray/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export interface Product {
/**
* Defines values for Enum0.
*/
export type Enum0 = "foo1" | "foo2" | "foo3";
export type Enum0 = "foo1" | "foo2" | "foo3" | string;
/**
* Defines values for Enum1.
*/
export type Enum1 = "foo1" | "foo2" | "foo3";
export type Enum1 = "foo1" | "foo2" | "foo3" | string;
/**
* Defines values for FooEnum.
*/
Expand Down
12 changes: 9 additions & 3 deletions test/integration/generated/bodyComplex/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,21 @@ export type Cookiecuttershark = Shark & {};
/**
* Defines values for CMYKColors.
*/
export type CMYKColors = "cyan" | "Magenta" | "YELLOW" | "blacK";
export type CMYKColors = "cyan" | "Magenta" | "YELLOW" | "blacK" | string;
/**
* Defines values for MyKind.
*/
export type MyKind = "Kind1";
export type MyKind = "Kind1" | string;
/**
* Defines values for GoblinSharkColor.
*/
export type GoblinSharkColor = "pink" | "gray" | "brown" | "RED" | "red";
export type GoblinSharkColor =
| "pink"
| "gray"
| "brown"
| "RED"
| "red"
| string;

/**
* Contains response data for the getValid operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as coreHttp from "@azure/core-http";
/**
* Defines values for Enum0.
*/
export type Enum0 = "one" | "two";
export type Enum0 = "one" | "two" | string;

/**
* Contains response data for the apiV1ValueGet operation.
Expand Down
9 changes: 6 additions & 3 deletions test/integration/generated/lro/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,8 @@ export type ProductPropertiesProvisioningStateValues =
| "Updated"
| "Deleting"
| "Deleted"
| "OK";
| "OK"
| string;
/**
* Defines values for SubProductPropertiesProvisioningStateValues.
*/
Expand All @@ -941,7 +942,8 @@ export type SubProductPropertiesProvisioningStateValues =
| "Updated"
| "Deleting"
| "Deleted"
| "OK";
| "OK"
| string;
/**
* Defines values for OperationResultStatus.
*/
Expand All @@ -956,7 +958,8 @@ export type OperationResultStatus =
| "Updated"
| "Deleting"
| "Deleted"
| "OK";
| "OK"
| string;

/**
* Optional parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export type FlattenedProductPropertiesProvisioningStateValues =
| "Updated"
| "Deleting"
| "Deleted"
| "OK";
| "OK"
| string;

/**
* Optional parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as coreHttp from "@azure/core-http";
/**
* Defines values for Enum0.
*/
export type Enum0 = "one" | "two";
export type Enum0 = "one" | "two" | string;

/**
* Contains response data for the apiV1ValueGet operation.
Expand Down
2 changes: 1 addition & 1 deletion test/integration/generated/noMappers/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as coreHttp from "@azure/core-http";
/**
* Defines values for Enum0.
*/
export type Enum0 = "one" | "two";
export type Enum0 = "one" | "two" | string;

/**
* Contains response data for the apiV1ValueGet operation.
Expand Down
3 changes: 2 additions & 1 deletion test/integration/generated/paging/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export type OperationResultStatus =
| "Updated"
| "Deleting"
| "Deleted"
| "OK";
| "OK"
| string;

/**
* Contains response data for the getNoItemNamePages operation.
Expand Down
8 changes: 5 additions & 3 deletions test/integration/generated/xmlservice/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export interface XmlGetHeadersHeaders {
/**
* Defines values for PublicAccessType.
*/
export type PublicAccessType = "container" | "blob";
export type PublicAccessType = "container" | "blob" | string;
/**
* Defines values for AccessTier.
*/
Expand All @@ -398,13 +398,15 @@ export type AccessTier =
| "P50"
| "Hot"
| "Cool"
| "Archive";
| "Archive"
| string;
/**
* Defines values for ArchiveStatus.
*/
export type ArchiveStatus =
| "rehydrate-pending-to-hot"
| "rehydrate-pending-to-cool";
| "rehydrate-pending-to-cool"
| string;
/**
* Defines values for LeaseStatusType.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ export interface AzureResourceBase {
}

// @public
export type CleanupOptions = "Always" | "OnSuccess" | "OnExpiration";
export type CleanupOptions = "Always" | "OnSuccess" | "OnExpiration" | string;

// @public
export interface ContainerConfiguration {
containerGroupName?: string;
}

// @public
export type CreatedByType = "User" | "Application" | "ManagedIdentity" | "Key";
export type CreatedByType = "User" | "Application" | "ManagedIdentity" | "Key" | string;

// @public
export type DeploymentScript = AzureResourceBase & {
Expand Down Expand Up @@ -266,7 +266,7 @@ export interface ManagedServiceIdentity {
}

// @public
export type ManagedServiceIdentityType = "UserAssigned";
export type ManagedServiceIdentityType = "UserAssigned" | string;

// @public
export interface ScriptConfigurationBase {
Expand All @@ -291,7 +291,7 @@ export interface ScriptLogsList {
}

// @public
export type ScriptProvisioningState = "Creating" | "ProvisioningResources" | "Running" | "Succeeded" | "Failed" | "Canceled";
export type ScriptProvisioningState = "Creating" | "ProvisioningResources" | "Running" | "Succeeded" | "Failed" | "Canceled" | string;

// @public
export interface ScriptStatus {
Expand All @@ -304,7 +304,7 @@ export interface ScriptStatus {
}

// @public
export type ScriptType = "AzurePowerShell" | "AzureCLI";
export type ScriptType = "AzurePowerShell" | "AzureCLI" | string;

// @public
export interface StorageAccountConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,24 @@ export type AzureCliScript = DeploymentScript & {
/**
* Defines values for ManagedServiceIdentityType.
*/
export type ManagedServiceIdentityType = "UserAssigned";
export type ManagedServiceIdentityType = "UserAssigned" | string;
/**
* Defines values for ScriptType.
*/
export type ScriptType = "AzurePowerShell" | "AzureCLI";
export type ScriptType = "AzurePowerShell" | "AzureCLI" | string;
/**
* Defines values for CreatedByType.
*/
export type CreatedByType = "User" | "Application" | "ManagedIdentity" | "Key";
export type CreatedByType =
| "User"
| "Application"
| "ManagedIdentity"
| "Key"
| string;
/**
* Defines values for CleanupOptions.
*/
export type CleanupOptions = "Always" | "OnSuccess" | "OnExpiration";
export type CleanupOptions = "Always" | "OnSuccess" | "OnExpiration" | string;
/**
* Defines values for ScriptProvisioningState.
*/
Expand All @@ -527,7 +532,8 @@ export type ScriptProvisioningState =
| "Running"
| "Succeeded"
| "Failed"
| "Canceled";
| "Canceled"
| string;

/**
* Contains response data for the create operation.
Expand Down
Loading

0 comments on commit 968347a

Please sign in to comment.