Skip to content

Commit

Permalink
Fixed an enum related issue and add some RPs in the test (#669)
Browse files Browse the repository at this point in the history
* Add databricks, fix several issues

* Fixed a completer related issue and add support for credential

* Add two m4 configuration and fix some issues

* Add support for enum with one value

* Fixed an enum related issue and add some RPs in the test
  • Loading branch information
dolauli authored Aug 19, 2020
1 parent ce1b3af commit c02f5df
Show file tree
Hide file tree
Showing 45 changed files with 11,855 additions and 124 deletions.
4 changes: 2 additions & 2 deletions powershell/cmdlets/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,7 @@ export class NewCmdletClass extends Class {
addDefaultInfo(cmdletParameter, vParam);
}

const isEnum = propertyType.schema.language.csharp?.enum !== undefined;
const isEnum = propertyType instanceof NewEnumImplementation;;
const hasEnum = propertyType instanceof NewArrayOf && propertyType.elementType instanceof NewEnumImplementation;
if (isEnum || hasEnum) {
cmdletParameter.add(new Attribute(ArgumentCompleterAttribute, { parameters: [`typeof(${hasEnum ? (<NewArrayOf>propertyType).elementType.declaration : propertyType.declaration})`] }));
Expand Down Expand Up @@ -2613,7 +2613,7 @@ export class NewCmdletClass extends Class {
// regularCmdletParameter.add(new Attribute(ArgumentCompleterAttribute, { parameters: [`typeof(${this.declaration})`] }));
}

const isEnum = propertyType.schema.language.csharp?.enum !== undefined;
const isEnum = propertyType instanceof NewEnumImplementation;
const hasEnum = propertyType instanceof NewArrayOf && propertyType.elementType instanceof NewEnumImplementation;
if (isEnum || hasEnum) {
regularCmdletParameter.add(new Attribute(ArgumentCompleterAttribute, { parameters: [`typeof(${hasEnum ? (<NewArrayOf>propertyType).elementType.declaration : propertyType.declaration})`] }));
Expand Down
4 changes: 4 additions & 0 deletions powershell/llcsharp/schema/schema-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { EnhancedTypeDeclaration, NewEnhancedTypeDeclaration } from './extended-
import { PwshModel } from '../../utils/PwshModel';
import { NewModelState } from '../../utils/model-state';
import { Channel, Host, Session, startSession } from '@azure-tools/autorest-extension-base';
import { schemaHasEnum } from '../validations';

export class SchemaDefinitionResolver {
private readonly cache = new Map<string, EnhancedTypeDeclaration>();
Expand Down Expand Up @@ -244,6 +245,9 @@ export class NewSchemaDefinitionResolver {
return new NewString(schema, required);
}
case SchemaType.SealedChoice:
if (schema.language.default.skip === true) {
return new NewString(schema, required);
}
return new NewEnumImplementation(schema, required);
case undefined:
if (schema.extensions && schema.extensions['x-ms-enum']) {
Expand Down
2 changes: 1 addition & 1 deletion powershell/llcsharp/schema/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ ${this.validateEnum(eventListener, property)}
return `await ${eventListener}.AssertRegEx(${nameof(property.value)},${property},@"${pattern}");`;
}
private validateEnum(eventListener: Variable, property: Variable): string {
if (this.schema.type !== SchemaType.SealedChoice) {
if (this.schema.type !== SchemaType.SealedChoice && this.schema.type != SchemaType.Choice) {
return '';
}
const choiceValues = (<SealedChoiceSchema>this.schema).choices.map((c) => c.value);
Expand Down
5 changes: 0 additions & 5 deletions powershell/plugins/plugin-create-inline-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,6 @@ function createVirtualProperties(schema: ObjectSchema, stack: Array<string>, thr
alias: [],
required: property.required || property.language.default.required
});
// dolauli, set constant value and make it readonly, if it is constant
if (property.schema.type === SchemaType.Constant) {
property.language.default.readOnly = true;
property.language.default.constantValue = (<ConstantSchema>property.schema).value.value;
}
}

// resolve name collisions.
Expand Down
32 changes: 31 additions & 1 deletion powershell/plugins/plugin-tweak-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Property, codeModelSchema, CodeModel, StringSchema, ObjectSchema, GroupSchema, isObjectSchema, SchemaType, GroupProperty, ParameterLocation, Operation, Parameter, VirtualParameter, getAllProperties, ImplementationLocation, OperationGroup, Request, SchemaContext, ChoiceSchema, Scheme, Schema, ConstantSchema } from '@azure-tools/codemodel';
import { Property, SealedChoiceSchema, codeModelSchema, CodeModel, StringSchema, ObjectSchema, GroupSchema, isObjectSchema, SchemaType, GroupProperty, ParameterLocation, Operation, Parameter, VirtualParameter, getAllProperties, ImplementationLocation, OperationGroup, Request, SchemaContext, ChoiceSchema, Scheme, Schema, ConstantSchema } from '@azure-tools/codemodel';
//import { ModelState } from '@azure-tools/codemodel-v3';
//import { KnownMediaType, knownMediaType, ParameterLocation, getPolymorphicBases, isSchemaObject, JsonType, Property, Schema, processCodeModel, StringFormat, codemodel, ModelState } from '@azure-tools/codemodel-v3';
import { pascalCase, deconstruct, fixLeadingNumber, serialize, KnownMediaType } from '@azure-tools/codegen';
Expand Down Expand Up @@ -286,6 +286,21 @@ async function tweakModelV2(state: State): Promise<PwshModel> {
} else if (parameter.schema.type === SchemaType.Constant) {
const constantSchema = parameter.schema as ConstantSchema;
parameter.language.default.constantValue = constantSchema.value.value;
} else if (parameter.schema.type === SchemaType.SealedChoice) {
const sealedChoiceSchema = parameter.schema as SealedChoiceSchema;
if (sealedChoiceSchema.choices.length === 1) {
parameter.language.default.constantValue = sealedChoiceSchema.choices[0].value;
if (sealedChoiceSchema.language.default.skip !== false) {
sealedChoiceSchema.language.default.skip = true;
}
}
}
} else {
if (parameter.schema.type === SchemaType.SealedChoice) {
const sealedChoiceSchema = parameter.schema as SealedChoiceSchema;
if (sealedChoiceSchema.choices.length === 1) {
sealedChoiceSchema.language.default.skip = false;
}
}
}
}
Expand All @@ -306,6 +321,21 @@ async function tweakModelV2(state: State): Promise<PwshModel> {
} else if (property.schema.type === SchemaType.Constant) {
const constantSchema = property.schema as ConstantSchema;
property.language.default.constantValue = constantSchema.value.value;
} else if (property.schema.type === SchemaType.SealedChoice) {
const sealedChoiceSchema = property.schema as SealedChoiceSchema;
if (sealedChoiceSchema.choices.length === 1) {
property.language.default.constantValue = sealedChoiceSchema.choices[0].value;
if (sealedChoiceSchema.language.default.skip !== false) {
sealedChoiceSchema.language.default.skip = true;
}
}
}
} else {
if (property.schema.type === SchemaType.SealedChoice) {
const sealedChoiceSchema = property.schema as SealedChoiceSchema;
if (sealedChoiceSchema.choices.length === 1) {
sealedChoiceSchema.language.default.skip = false;
}
}
}
}
Expand Down
224 changes: 109 additions & 115 deletions tests-upgrade/datamodels-datatypes-object/swagger.json
Original file line number Diff line number Diff line change
@@ -1,131 +1,125 @@
{

"swagger": "2.0",
"info": {
"title": "DatabricksClient",
"version": "2018-04-01",
"description": "ARM Databricks"
},
"host": "management.azure.com",
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"security": [
{
"azure_auth": [
"user_impersonation"
]
"swagger": "2.0",
"info": {
"title": "DatabricksClient",
"version": "2018-04-01",
"description": "ARM Databricks"
},
"host": "management.azure.com",
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"security": [
{
"azure_auth": [
"user_impersonation"
]
}
],
"securityDefinitions": {
"azure_auth": {
"type": "oauth2",
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"flow": "implicit",
"description": "Azure Active Directory OAuth2 Flow",
"scopes": {
"user_impersonation": "impersonate your user account"
}
],
"securityDefinitions": {
"azure_auth": {
"type": "oauth2",
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"flow": "implicit",
"description": "Azure Active Directory OAuth2 Flow",
"scopes": {
"user_impersonation": "impersonate your user account"
}
},
"paths": {
"/subscriptions/resourceGroup": {
"get": {
"tags": [
"Workspaces"
],
"operationId": "Workspaces_Get",
"description": "Gets the workspace.",
"responses": {
"200": {
"description": "OK-Return workspace."
}
}
}
},
"paths": {
"/subscriptions/resourceGroup": {
"get": {
"tags": [
"Workspaces"
],
"operationId": "Workspaces_Get",
"description": "Gets the workspace.",
"responses": {
"200": {
"description": "OK-Return workspace."
}
}
}
},
"definitions": {
"Model": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"code": {
"type": "integer"
}
}
},
"definitions": {
"Model": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"code": {
"type": "integer"
}
}
"Model2": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"Model2": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"username": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"id",
"username"
]
"username": {
"type": "string"
},
"Model3": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"readOnly": true
},
"password": {
"type": "string"
}
},
"required": [
"id",
"username"
]
},
"ContactInfo": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email"
},
"phone": {
"type": "string"
}
"name": {
"type": "string"
}
},
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"contact_info": {
"$ref": "#/definitions/ContactInfo"
}
"required": [
"id",
"username"
]
},
"Model3": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"readOnly": true
},
"password": {
"type": "string"
}
},
"Model4": {
"type": "object",
"minProperties": 2,
"maxProperties": 10
"required": [
"id",
"username"
]
},
"ContactInfo": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email"
},
"phone": {
"type": "string"
}
}
},
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"contact_info": {
"$ref": "#/definitions/ContactInfo"
}
}
}
}
}
Loading

0 comments on commit c02f5df

Please sign in to comment.