Skip to content

Commit 27c06e7

Browse files
shawncxmsxichendolauli
authored
fix some issues (#665)
Co-authored-by: xichen <[email protected]> Co-authored-by: Xiaogang <[email protected]>
1 parent f9c6089 commit 27c06e7

File tree

10 files changed

+76
-110
lines changed

10 files changed

+76
-110
lines changed

powershell/internal/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { codemodel, PropertyDetails, exportedModels as T, ModelState, JsonType,
1717
import { DeepPartial } from '@azure-tools/codegen';
1818
import { PwshModel } from '../utils/PwshModel';
1919
import { NewModelState } from '../utils/model-state';
20-
import { BooleanSchema, Schema as NewSchema, SchemaType } from '@azure-tools/codemodel';
20+
import { BooleanSchema, ConstantSchema, Schema as NewSchema, SchemaType } from '@azure-tools/codemodel';
2121

2222
export type Schema = T.SchemaT<LanguageDetails<SchemaDetails>, LanguageDetails<PropertyDetails>>;
2323

@@ -72,7 +72,7 @@ export class NewPSSchemaResolver extends NewSchemaDefinitionResolver {
7272
try {
7373
if (!this.inResolve) {
7474
this.inResolve = true;
75-
if (schema && schema.type === SchemaType.Boolean) {
75+
if (schema && (schema.type === SchemaType.Boolean || (schema.type === SchemaType.Constant && (<ConstantSchema>schema).valueType.type === SchemaType.Boolean))) {
7676
return new NewPSSwitch(<BooleanSchema>schema, required);
7777
}
7878
}

powershell/llcsharp/schema/date-time.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { OneOrMoreStatements } from '@azure-tools/codegen-csharp';
1111
import { Variable } from '@azure-tools/codegen-csharp';
1212
import { ClientRuntime } from '../clientruntime';
1313
import { Schema } from '../code-model';
14-
import { Schema as NewSchema, DateTimeSchema, UnixTimeSchema } from '@azure-tools/codemodel';
14+
import { Schema as NewSchema, DateTimeSchema, UnixTimeSchema, DateSchema } from '@azure-tools/codemodel';
1515
import { Primitive, NewPrimitive } from './primitive';
1616

1717
export class DateTime extends Primitive {
@@ -183,7 +183,7 @@ export class NewDateTime extends NewPrimitive {
183183
}
184184
return (`/* serializeToContainerMember doesn't support '${mediaType}' ${__filename}*/`);
185185
}
186-
constructor(schema: DateTimeSchema, public isRequired: boolean) {
186+
constructor(schema: DateTimeSchema | DateSchema, public isRequired: boolean) {
187187
super(schema);
188188
}
189189
// public static string DateFormat = "yyyy-MM-dd";

powershell/llcsharp/schema/date.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { StringExpression } from '@azure-tools/codegen-csharp';
77
import { Schema } from '../code-model';
8-
import { Schema as NewSchema, DateTimeSchema } from '@azure-tools/codemodel';
8+
import { DateSchema } from '@azure-tools/codemodel';
99
import { DateTime, NewDateTime } from './date-time';
1010

1111
export class Date extends DateTime {
@@ -17,7 +17,7 @@ export class Date extends DateTime {
1717

1818
export class NewDate extends NewDateTime {
1919
public DateTimeFormat = new StringExpression('yyyy-MM-dd');
20-
constructor(schema: DateTimeSchema, isRequired: boolean) {
20+
constructor(schema: DateSchema, isRequired: boolean) {
2121
super(schema, isRequired);
2222
}
2323
}

powershell/llcsharp/schema/schema-resolver.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { codeModelSchema, ArraySchema, CodeModel, Schema as NewSchema, StringSchema, BooleanSchema, NumberSchema, ByteArraySchema, DateTimeSchema, ObjectSchema, GroupSchema, isObjectSchema, SchemaType, GroupProperty, ParameterLocation, Operation, Parameter, VirtualParameter, getAllProperties, ImplementationLocation, OperationGroup, Request, SchemaContext, ConstantSchema, ChoiceSchema, DurationSchema } from '@azure-tools/codemodel';
6+
import { codeModelSchema, ArraySchema, CodeModel, Schema as NewSchema, StringSchema, BooleanSchema, NumberSchema, ByteArraySchema, DateTimeSchema, ObjectSchema, GroupSchema, isObjectSchema, SchemaType, GroupProperty, ParameterLocation, Operation, Parameter, VirtualParameter, getAllProperties, ImplementationLocation, OperationGroup, Request, SchemaContext, ConstantSchema, ChoiceSchema, DurationSchema, BinarySchema, DateSchema } from '@azure-tools/codemodel';
77

88
import { ModelState, codemodel, IntegerFormat, NumberFormat, StringFormat, JsonType } from '@azure-tools/codemodel-v3';
99
import { Schema } from '../code-model';
@@ -189,6 +189,8 @@ export class NewSchemaDefinitionResolver {
189189
return new NewString(<StringSchema>schema, required);
190190

191191
}
192+
case SchemaType.Binary:
193+
return new NewBinary(<BinarySchema>schema, required);
192194
case SchemaType.Duration:
193195
return new NewDuration(<DurationSchema>schema, required);
194196
case SchemaType.Uuid:
@@ -198,6 +200,8 @@ export class NewSchemaDefinitionResolver {
198200
return new NewDateTime1123(<DateTimeSchema>schema, required);
199201
}
200202
return new NewDateTime(<DateTimeSchema>schema, required);
203+
case SchemaType.Date:
204+
return new NewDate(<DateSchema>schema, required);
201205
case SchemaType.ByteArray:
202206
return new NewByteArray(<ByteArraySchema>schema, required);
203207
case SchemaType.Boolean:
@@ -210,6 +214,7 @@ export class NewSchemaDefinitionResolver {
210214
// skip-for-time-being
211215
// case IntegerFormat.UnixTime:
212216
// return new UnixTime(schema, required);
217+
case 16:
213218
case 32:
214219
return new NewNumeric(<NumberSchema>schema, required, required ? 'int' : 'int?');
215220
}
@@ -229,18 +234,8 @@ export class NewSchemaDefinitionResolver {
229234
return new NewNumeric(<NumberSchema>schema, required, required ? 'float' : 'float?');
230235

231236
case SchemaType.Constant:
232-
switch ((<ConstantSchema>schema).valueType.type) {
233-
case SchemaType.String:
234-
return new NewString(schema, required);
235-
case SchemaType.DateTime:
236-
if ((<any>schema).valueType.format === StringFormat.DateTimeRfc1123) {
237-
return new NewDateTime1123(<DateTimeSchema>schema, required);
238-
}
239-
return new NewDateTime(<DateTimeSchema>schema, required);
240-
default:
241-
state.error(`Unsupported constant type. Schema '${schema.language.csharp?.name}' is declared with invalid type '${schema.type}'`, message.UnknownJsonType);
242-
throw new Error('Unknown Model. Fatal.');
243-
}
237+
return this.resolveTypeDeclaration((<ConstantSchema>schema).valueType, required, state);
238+
244239
case SchemaType.Choice: {
245240
const choiceSchema = schema as ChoiceSchema;
246241
if ((<any>choiceSchema.choiceType).type === SchemaType.DateTime && (<any>choiceSchema.choiceType).format === StringFormat.DateTimeRfc1123) {
@@ -263,4 +258,4 @@ export class NewSchemaDefinitionResolver {
263258
state.error(`Schema '${schema.language.csharp?.name}' is declared with invalid type '${schema.type}'`, message.UnknownJsonType);
264259
throw new Error('Unknown Model. Fatal.');
265260
}
266-
}
261+
}

powershell/models/model-extensions.ts

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -284,57 +284,6 @@ export class NewModelExtensionsNamespace extends Namespace {
284284
continue;
285285
}
286286

287-
if (schema.type === SchemaType.Dictionary) {
288-
// xichen:
289-
// Case1: dictionary schema is only used in parents:
290-
// "definitions": {
291-
// "PetAPInPropertiesWithAPString": {
292-
// "type": "object",
293-
// "properties": {
294-
// "id": {
295-
// "type": "integer"
296-
// }
297-
// },
298-
// "additionalProperties": {
299-
// "type": "string"
300-
// }
301-
// }
302-
// }
303-
//
304-
// Case2: we will have a dictionary property:
305-
// "definitions": {
306-
// "PetAPInProperties": {
307-
// "type": "object",
308-
// "properties": {
309-
// "id": {
310-
// "type": "integer"
311-
// },
312-
// "additionalProperties": {
313-
// "type": "object",
314-
// "additionalProperties": {
315-
// "type": "number"
316-
// }
317-
// }
318-
// }
319-
// }
320-
// }
321-
//
322-
// We only create model class for case 2, because in m3 case 2 will has independent scheam for additionalProperties,
323-
// But for case 1, PetAPInPropertiesWithAPString will only have additionalProperties.
324-
//
325-
// This is to make generated code same as m3. Actually there wont be side effect if we skip this check.
326-
const objSchemas = schemas['objects'];
327-
const usedByProp = schemas['objects']?.some((objSchema) => {
328-
if ((<ObjectSchema>objSchema).properties?.some((prop) => prop.schema === schema)) {
329-
return true;
330-
}
331-
return false;
332-
});
333-
if (!usedByProp) {
334-
continue;
335-
}
336-
}
337-
338287
const td = this.resolver.resolveTypeDeclaration(schema, true, state);
339288
if (td instanceof NewObjectImplementation) {
340289

powershell/plugins/create-commands-v2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ export /* @internal */ class Inferrer {
287287
}));
288288

289289
// let's add a variant where it's expanded out.
290-
// *IF* the body is an object
291-
if (body.schema.type === SchemaType.Object) {
290+
// *IF* the body is an object or dictionary
291+
if (body.schema.type === SchemaType.Object || body.schema.type === SchemaType.Dictionary) {
292292
const opExpanded = await this.addCommandOperation(`${vname}Expanded`, parameters, operation, variant, state);
293293
opExpanded.details.default.dropBodyParameter = true;
294294
opExpanded.parameters.push(new IParameter(`${bodyParameterName}Body`, body.schema, {

powershell/plugins/cs-namer-v2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ async function setOperationNames(state: State, resolver: NewSchemaDefinitionReso
179179

180180
operation.language.csharp = {
181181
...details, // inherit
182-
name: getPascalIdentifier(operationName),
182+
name: operationName,
183183
};
184184

185185
// parameters are camelCased.
186186
for (const parameter of values(operation.parameters)) {
187187
const parameterDetails = parameter.language.default;
188188

189-
let propName = camelCase(fixLeadingNumber(deconstruct(parameterDetails.name)));
189+
let propName = camelCase(fixLeadingNumber(deconstruct(parameterDetails.serializedName)));
190190

191191
if (propName === 'default') {
192192
propName = '@default';

powershell/plugins/plugin-tweak-m4-model.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { CodeModel, DictionarySchema, getAllProperties, HttpHeader, ObjectSchema, Property, Schema, SchemaType } from '@azure-tools/codemodel';
5+
import { ArraySchema, CodeModel, DictionarySchema, getAllProperties, HttpHeader, ObjectSchema, Property, Schema, SchemaType } from '@azure-tools/codemodel';
66
import { serialize } from '@azure-tools/codegen';
77
import { PwshModel } from '../utils/PwshModel';
88
import { NewModelState } from '../utils/model-state';
@@ -19,7 +19,7 @@ async function tweakModel(state: State): Promise<PwshModel> {
1919

2020
addDictionaryApiVersion(model);
2121

22-
removeDictionaryDefaultDescription(model);
22+
removeM4DefaultDescription(model);
2323

2424
return model;
2525
}
@@ -80,62 +80,73 @@ function addResponseHeaderSchema(model: CodeModel) {
8080
}
8181

8282
function addDictionaryApiVersion(model: CodeModel) {
83-
// If object has dictionary property, this property's schema does not have api version information in m4. We should add this back.
83+
84+
model.schemas.dictionaries?.forEach((schema) => {
85+
if (schema.apiVersions) {
86+
return;
87+
}
88+
if (schema.elementType && schema.elementType.apiVersions) {
89+
schema.apiVersions = JSON.parse(JSON.stringify(schema.elementType.apiVersions));
90+
}
91+
})
92+
93+
// If we cannot find api version from element type, try to get it from object schema who refers the dict or any.
8494

8595
model.schemas.objects?.forEach((schema) => {
8696
if (!schema.apiVersions) {
8797
return;
8898
}
8999

90100
for (const prop of getAllProperties(schema)) {
91-
if ((prop.schema.type !== SchemaType.Dictionary && prop.schema.type !== SchemaType.Any) || prop.schema.apiVersions) {
101+
if (prop.schema.type !== SchemaType.Dictionary || prop.schema.apiVersions) {
92102
continue;
93103
}
94-
const dictSchema = prop.schema as DictionarySchema;
95-
if (dictSchema.elementType && dictSchema.elementType.apiVersions) {
96-
dictSchema.apiVersions = JSON.parse(JSON.stringify(dictSchema.elementType.apiVersions));
97-
} else {
98-
dictSchema.apiVersions = JSON.parse(JSON.stringify(schema.apiVersions));
99-
}
104+
prop.schema.apiVersions = JSON.parse(JSON.stringify(schema.apiVersions));
100105
}
101106
})
102107
}
103108

104-
function removeDictionaryDefaultDescription(model: CodeModel) {
105-
// For dictionary schema and property, if there is no description assigned, m4 will set a default description like: Dictionary of <type>
109+
function removeM4DefaultDescription(model: CodeModel) {
110+
// For dictionary and arrya schema and property, if there is no description assigned, m4 will set a default description like: Dictionary of <type> or Array of <type>
106111
// To keep same action as m3, we will set it to empty string
107112

108113
const visited = new Set<Schema>();
109-
[...model.schemas.objects ?? [], ...model.schemas.dictionaries ?? []].forEach((schema) => {
110-
recursiveRemoveDefaultDescription(schema, visited);
114+
[...model.schemas.objects ?? [], ...model.schemas.dictionaries ?? [], ...model.schemas.arrays ?? []].forEach((schema) => {
115+
recursiveRemoveM4DefaultDescription(schema, visited);
111116
})
112117
}
113118

114-
function recursiveRemoveDefaultDescription(schema: Schema, visited: Set<Schema>) {
115-
if (visited.has(schema) || (schema.type !== SchemaType.Object && schema.type !== SchemaType.Dictionary)) {
119+
function recursiveRemoveM4DefaultDescription(schema: Schema, visited: Set<Schema>) {
120+
if (visited.has(schema) || (schema.type !== SchemaType.Object && schema.type !== SchemaType.Dictionary && schema.type !== SchemaType.Array)) {
116121
return;
117122
}
118123
// Default description pattern in m4
119-
const defaultDescPattern = /Dictionary of <.*>$/;
124+
const defaultDictDescPattern = /Dictionary of <.?>$/;
125+
const defaultArrayDescPattern = /Array of .?$/;
120126
visited.add(schema);
121127
if (schema.type === SchemaType.Dictionary) {
122128
const dictSchema = schema as DictionarySchema;
123-
recursiveRemoveDefaultDescription(dictSchema.elementType, visited);
124-
if (defaultDescPattern.test(dictSchema.language.default.description)) {
129+
recursiveRemoveM4DefaultDescription(dictSchema.elementType, visited);
130+
if (defaultDictDescPattern.test(dictSchema.language.default.description)) {
125131
dictSchema.language.default.description = '';
126132
}
133+
} else if (schema.type === SchemaType.Array) {
134+
const arrSchema = schema as ArraySchema;
135+
recursiveRemoveM4DefaultDescription(arrSchema.elementType, visited);
136+
if (defaultArrayDescPattern.test(schema.language.default.description)) {
137+
schema.language.default.description = '';
138+
}
127139
} else if (schema.type === SchemaType.Object) {
128140
const objSchema = schema as ObjectSchema;
129141
for (const prop of getAllProperties(objSchema)) {
130-
recursiveRemoveDefaultDescription(prop.schema, visited);
131-
if (prop.schema.type === SchemaType.Dictionary && defaultDescPattern.test(prop.language.default.description)) {
142+
recursiveRemoveM4DefaultDescription(prop.schema, visited);
143+
if (prop.schema.type === SchemaType.Dictionary && (defaultDictDescPattern.test(prop.language.default.description) || defaultArrayDescPattern.test(prop.language.default.description))) {
132144
prop.language.default.description = '';
133145
}
134146
}
135147
}
136148
}
137149

138-
139150
export async function tweakM4ModelPlugin(service: Host) {
140151
const state = await new NewModelState<PwshModel>(service).init();
141152
service.WriteFile('code-model-v4-tweakm4codemodel.yaml', serialize(await tweakModel(state)), undefined, 'code-model-v4');

powershell/plugins/plugin-tweak-model.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { Property, codeModelSchema, CodeModel, StringSchema, ObjectSchema, GroupSchema, isObjectSchema, SchemaType, GroupProperty, ParameterLocation, Operation, Parameter, VirtualParameter, getAllProperties, ImplementationLocation, OperationGroup, Request, SchemaContext, ChoiceSchema, Scheme, Schema } from '@azure-tools/codemodel';
5+
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';
66
//import { ModelState } from '@azure-tools/codemodel-v3';
77
//import { KnownMediaType, knownMediaType, ParameterLocation, getPolymorphicBases, isSchemaObject, JsonType, Property, Schema, processCodeModel, StringFormat, codemodel, ModelState } from '@azure-tools/codemodel-v3';
88
import { pascalCase, deconstruct, fixLeadingNumber, serialize, KnownMediaType } from '@azure-tools/codegen';
@@ -99,7 +99,7 @@ async function tweakModelV2(state: State): Promise<PwshModel> {
9999
for (const operation of values(group.operations)) {
100100
for (const param of values(operation.parameters).where(each => each.protocol?.http?.in === ParameterLocation.Path)) {
101101
const name = param.language.default.name;
102-
const hasName = universalId.properties?.find((prop) => prop.language.default.name === name);
102+
const hasName = universalId.properties?.find((prop) => prop.language.default.name.toLocaleLowerCase() === name.toLocaleLowerCase());
103103
if (!hasName) {
104104
if (!universalId.properties) {
105105
universalId.properties = [];
@@ -277,10 +277,15 @@ async function tweakModelV2(state: State): Promise<PwshModel> {
277277
for (const group of values(model.operationGroups)) {
278278
for (const operation of values(group.operations)) {
279279
for (const parameter of values(operation.parameters)) {
280-
if (parameter.required && parameter.schema instanceof ChoiceSchema) {
281-
const choiceSchema = parameter.schema as ChoiceSchema;
282-
if (choiceSchema.choices.length === 1) {
283-
parameter.language.default.constantValue = choiceSchema.choices[0].value;
280+
if (parameter.required) {
281+
if (parameter.schema.type === SchemaType.Choice) {
282+
const choiceSchema = parameter.schema as ChoiceSchema;
283+
if (choiceSchema.choices.length === 1) {
284+
parameter.language.default.constantValue = choiceSchema.choices[0].value;
285+
}
286+
} else if (parameter.schema.type === SchemaType.Constant) {
287+
const constantSchema = parameter.schema as ConstantSchema;
288+
parameter.language.default.constantValue = constantSchema.value.value;
284289
}
285290
}
286291
}
@@ -290,12 +295,17 @@ async function tweakModelV2(state: State): Promise<PwshModel> {
290295
// identify properties that are constants
291296
for (const schema of values(schemas.objects)) {
292297
for (const property of values(schema.properties)) {
293-
if (property.required && property.schema instanceof ChoiceSchema) {
294-
const choiceSchema = property.schema as ChoiceSchema;
295-
if (choiceSchema.choices.length === 1) {
296-
// properties with an enum single value are constants
297-
// add the constant value
298-
property.language.default.constantValue = choiceSchema.choices[0].value;
298+
if (property.required) {
299+
if (property.schema.type === SchemaType.Choice) {
300+
const choiceSchema = property.schema as ChoiceSchema;
301+
if (choiceSchema.choices.length === 1) {
302+
// properties with an enum single value are constants
303+
// add the constant value
304+
property.language.default.constantValue = choiceSchema.choices[0].value;
305+
}
306+
} else if (property.schema.type === SchemaType.Constant) {
307+
const constantSchema = property.schema as ConstantSchema;
308+
property.language.default.constantValue = constantSchema.value.value;
299309
}
300310
}
301311
}

tests-upgrade/Configuration.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"extension-ms-paramlocation",
3838
"directive-aliasremoval",
3939
"directive-cmdlet",
40-
"directive-parameter"
40+
"directive-parameter",
41+
"datamodels-datatypes-file"
4142
],
4243
"BlackList": [
4344
"basic-get-querystr",

0 commit comments

Comments
 (0)