@azure-tools/typespec-java 0.15.18 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,36 +1,35 @@
1
- import { getDoc, getEffectiveModelType, getFormat, getFriendlyName, getKnownValues, getSummary, getVisibility, ignoreDiagnostics, isArrayModelType, isRecordModelType, isUnknownType, getDiscriminator, isNeverType, listServices, getNamespaceFullName, isNullType, getTypeName, getProjectedName, getEncode, getOverloadedOperation, isVoidType, isErrorModel, } from "@typespec/compiler";
1
+ import { AnySchema, ApiVersion, ArraySchema, BinaryResponse, BinarySchema, BooleanSchema, ByteArraySchema, ChoiceValue, DateSchema, DateTimeSchema, DictionarySchema, Discriminator, GroupProperty, GroupSchema, HttpHeader, HttpParameter, ImplementationLocation, KeySecurityScheme, Language, Metadata, NumberSchema, OAuth2SecurityScheme, ObjectSchema, OperationGroup, Parameter, ParameterLocation, Property, Relations, Response, SchemaResponse, SchemaType, Security, SerializationStyle, StringSchema, TimeSchema, UnixTimeSchema, UriSchema, VirtualParameter, } from "@autorest/codemodel";
2
+ import { KnownMediaType } from "@azure-tools/codegen";
3
+ import { getLroMetadata, getPagedResult, isPollingLocation } from "@azure-tools/typespec-azure-core";
4
+ import { createSdkContext, getAllModels, getClientNameOverride, getClientType, getCrossLanguageDefinitionId, getDefaultApiVersion, getWireName, isApiVersion, isInternal, isSdkBuiltInKind, isSdkIntKind, listClients, listOperationGroups, listOperationsInOperationGroup, shouldGenerateConvenient, shouldGenerateProtocol, } from "@azure-tools/typespec-client-generator-core";
5
+ import { getDoc, getEffectiveModelType, getEncode, getFriendlyName, getNamespaceFullName, getOverloadedOperation, getProjectedName, getSummary, getTypeName, getVisibility, ignoreDiagnostics, isArrayModelType, isErrorModel, isRecordModelType, isVoidType, listServices, } from "@typespec/compiler";
6
+ import { Visibility, getAuthentication, getHeaderFieldOptions, getHttpOperation, getQueryParamOptions, getServers, getStatusCodeDescription, isPathParam, } from "@typespec/http";
2
7
  import { getResourceOperation, getSegment } from "@typespec/rest";
3
- import { getAuthentication, getServers, getStatusCodeDescription, getHttpOperation, getQueryParamOptions, getHeaderFieldOptions, isPathParam, } from "@typespec/http";
4
8
  import { getAddedOnVersions, getVersion } from "@typespec/versioning";
5
- import { isPollingLocation, getPagedResult, getLroMetadata, getUnionAsEnum, } from "@azure-tools/typespec-azure-core";
6
- import { listClients, listOperationGroups, listOperationsInOperationGroup, isApiVersion, shouldGenerateConvenient, createSdkContext, shouldGenerateProtocol, isInternal, getCrossLanguageDefinitionId, getClientNameOverride, shouldFlattenProperty, getWireName, getDefaultApiVersion, } from "@azure-tools/typespec-client-generator-core";
7
9
  import { fail } from "assert";
8
- import { AnySchema, ArraySchema, BinaryResponse, BinarySchema, BooleanSchema, ByteArraySchema, ChoiceValue, DateTimeSchema, DateSchema, DictionarySchema, Discriminator, HttpHeader, HttpParameter, ImplementationLocation, NumberSchema, ObjectSchema, Parameter, ParameterLocation, Property, Relations, Response, SchemaResponse, SchemaType, StringSchema, TimeSchema, Security, OAuth2SecurityScheme, KeySecurityScheme, OperationGroup, UriSchema, VirtualParameter, GroupSchema, GroupProperty, ApiVersion, SerializationStyle, Metadata, UnixTimeSchema, Language, } from "@autorest/codemodel";
9
- import { KnownMediaType } from "@azure-tools/codegen";
10
- import { CodeModel } from "./common/code-model.js";
10
+ import pkg from "lodash";
11
11
  import { Client as CodeModelClient, ObjectScheme } from "./common/client.js";
12
- import { ConvenienceApi, Operation as CodeModelOperation, Request } from "./common/operation.js";
13
- import { SchemaContext } from "./common/schemas/usage.js";
12
+ import { CodeModel } from "./common/code-model.js";
13
+ import { LongRunningMetadata } from "./common/long-running-metadata.js";
14
+ import { Operation as CodeModelOperation, ConvenienceApi, Request } from "./common/operation.js";
14
15
  import { ChoiceSchema, SealedChoiceSchema } from "./common/schemas/choice.js";
15
16
  import { ConstantSchema, ConstantValue } from "./common/schemas/constant.js";
16
17
  import { OrSchema } from "./common/schemas/relationship.js";
17
- import { LongRunningMetadata } from "./common/long-running-metadata.js";
18
18
  import { DurationSchema } from "./common/schemas/time.js";
19
- import { PreNamer } from "./prenamer/prenamer.js";
19
+ import { SchemaContext } from "./common/schemas/usage.js";
20
20
  import { createPollOperationDetailsSchema, getFileDetailsSchema } from "./external-schemas.js";
21
21
  import { ClientContext } from "./models.js";
22
- import { stringArrayContainsIgnoreCase, getJavaNamespace, getNamespace, pascalCase, logWarning, trace, } from "./utils.js";
23
- import { ProcessingCache, isModelReferredInTemplate, pushDistinct, modelContainsDerivedModel, getNameForTemplate, getDurationFormat, hasScalarAsBase, isNullableType, getAccess, getUsage, getUnionDescription, modelIs, getNamePrefixForProperty, isAllValueInteger, isStable, } from "./type-utils.js";
24
- import { getServiceVersion, operationIsJsonMergePatch, isPayloadProperty, ORIGIN_API_VERSION, SPECIAL_HEADER_NAMES, loadExamples, isLroNewPollingStrategy, operationIsMultipleContentTypes, cloneOperationParameter, operationIsMultipart, isKnownContentType, CONTENT_TYPE_KEY, } from "./operation-utils.js";
25
- import { isArmCommonType } from "./type-utils.js";
26
- import pkg from "lodash";
22
+ import { CONTENT_TYPE_KEY, ORIGIN_API_VERSION, SPECIAL_HEADER_NAMES, cloneOperationParameter, getServiceVersion, isKnownContentType, isLroNewPollingStrategy, isPayloadProperty, loadExamples, operationIsJsonMergePatch, operationIsMultipart, operationIsMultipleContentTypes, } from "./operation-utils.js";
23
+ import { PreNamer } from "./prenamer/prenamer.js";
24
+ import { ProcessingCache, getAccess, getDurationFormatFromSdkType, getNameForTemplate, getNamePrefixForProperty, getUnionDescription, getUsage, hasScalarAsBase, isArmCommonType, isModelReferredInTemplate, isNullableType, isStable, modelIs, pushDistinct, } from "./type-utils.js";
25
+ import { getJavaNamespace, getNamespace, logWarning, pascalCase, stringArrayContainsIgnoreCase, trace, } from "./utils.js";
27
26
  const { isEqual } = pkg;
28
27
  export class CodeModelBuilder {
29
28
  constructor(program1, context) {
30
29
  var _a, _b;
31
30
  this.loggingEnabled = false;
32
- this.schemaCache = new ProcessingCache((type, name) => this.processSchemaImpl(type, name));
33
- this.typeUnionRefCache = new Map(); // Union means it ref a Union type, null means it does not ref any Union, nndefined means type visited but not completed
31
+ this.schemaCache = new ProcessingCache((type, name) => this.processSchemaFromSdkTypeImpl(type, name));
32
+ this.typeUnionRefCache = new Map(); // Union means it ref a Union type, null means it does not ref any Union, undefined means type visited but not completed
34
33
  this.operationExamples = new Map();
35
34
  this.options = context.options;
36
35
  this.program = program1;
@@ -108,7 +107,8 @@ export class CodeModelBuilder {
108
107
  parameter = this.createApiVersionParameter(it.name, ParameterLocation.Uri);
109
108
  }
110
109
  else {
111
- const schema = this.processSchema(it.type, it.name);
110
+ const sdkType = getClientType(this.sdkContext, it.type);
111
+ const schema = this.processSchemaFromSdkType(sdkType, it.name);
112
112
  this.trackSchemaUsage(schema, {
113
113
  usage: [SchemaContext.Input, SchemaContext.Output /*SchemaContext.Public*/],
114
114
  });
@@ -119,7 +119,6 @@ export class CodeModelBuilder {
119
119
  protocol: {
120
120
  http: new HttpParameter(ParameterLocation.Uri),
121
121
  },
122
- clientDefaultValue: this.getDefaultValue(it.default),
123
122
  language: {
124
123
  default: {
125
124
  serializedName: it.name,
@@ -219,54 +218,36 @@ export class CodeModelBuilder {
219
218
  }
220
219
  }
221
220
  processModels(clients) {
222
- var _a;
223
- const processedModels = new Set();
224
- for (const client of clients) {
225
- const models = Array.from(client.service.models.values());
226
- Array.from(client.service.enums.values()).forEach((it) => models.push(it));
227
- Array.from(client.service.unions.values()).forEach((it) => models.push(it));
228
- (_a = client.service.namespaces) === null || _a === void 0 ? void 0 : _a.forEach((it) => {
229
- Array.from(it.models.values()).forEach((it) => models.push(it));
230
- Array.from(it.enums.values()).forEach((it) => models.push(it));
231
- Array.from(it.unions.values()).forEach((it) => models.push(it));
221
+ const processedSdkModels = new Set();
222
+ // lambda to mark model as public
223
+ const modelAsPublic = (model) => {
224
+ const schema = this.processSchemaFromSdkType(model, "");
225
+ this.trackSchemaUsage(schema, {
226
+ usage: [SchemaContext.Public],
232
227
  });
233
- // lambda to mark model as public
234
- const modelAsPublic = (model) => {
235
- // check it does not contain Union
236
- // const union = unionReferredByType(this.program, model, this.typeUnionRefCache);
237
- // if (union) {
238
- // const errorMsg = `Model '${getTypeName(
239
- // model,
240
- // this.typeNameOptions,
241
- // )}' cannot be set as access=public, as it refers Union '${getUnionDescription(union, this.typeNameOptions)}'`;
242
- // throw new Error(errorMsg);
243
- // }
244
- const schema = this.processSchema(model, "");
245
- this.trackSchemaUsage(schema, {
246
- usage: [SchemaContext.Public],
247
- });
248
- };
249
- for (const model of models) {
250
- if (!processedModels.has(model)) {
251
- const access = getAccess(model);
252
- if (access === "public") {
253
- modelAsPublic(model);
254
- }
255
- else if (access === "internal") {
256
- const schema = this.processSchema(model, "");
257
- this.trackSchemaUsage(schema, {
258
- usage: [SchemaContext.Internal],
259
- });
260
- }
261
- const usage = getUsage(model);
262
- if (usage) {
263
- const schema = this.processSchema(model, "");
264
- this.trackSchemaUsage(schema, {
265
- usage: usage,
266
- });
267
- }
268
- processedModels.add(model);
228
+ };
229
+ const sdkModels = getAllModels(this.sdkContext);
230
+ // process sdk models
231
+ for (const model of sdkModels) {
232
+ if (!processedSdkModels.has(model)) {
233
+ const access = getAccess(model.__raw);
234
+ if (access === "public") {
235
+ modelAsPublic(model);
236
+ }
237
+ else if (access === "internal") {
238
+ const schema = this.processSchemaFromSdkType(model, model.name);
239
+ this.trackSchemaUsage(schema, {
240
+ usage: [SchemaContext.Internal],
241
+ });
242
+ }
243
+ const usage = getUsage(model.__raw);
244
+ if (usage) {
245
+ const schema = this.processSchemaFromSdkType(model, "");
246
+ this.trackSchemaUsage(schema, {
247
+ usage: usage,
248
+ });
269
249
  }
250
+ processedSdkModels.add(model);
270
251
  }
271
252
  }
272
253
  }
@@ -684,7 +665,8 @@ export class CodeModelBuilder {
684
665
  }
685
666
  else {
686
667
  const pollType = this.findResponseBody(lroMetadata.pollingInfo.responseModel);
687
- pollingSchema = this.processSchema(pollType, "pollResult");
668
+ const sdkType = getClientType(this.sdkContext, pollType);
669
+ pollingSchema = this.processSchemaFromSdkType(sdkType, "pollResult");
688
670
  }
689
671
  }
690
672
  // finalSchema
@@ -695,25 +677,24 @@ export class CodeModelBuilder {
695
677
  lroMetadata.finalEnvelopeResult !== "void") {
696
678
  const finalResult = useNewPollStrategy ? lroMetadata.finalResult : lroMetadata.finalEnvelopeResult;
697
679
  const finalType = this.findResponseBody(finalResult);
698
- finalSchema = this.processSchema(finalType, "finalResult");
680
+ const sdkType = getClientType(this.sdkContext, finalType);
681
+ finalSchema = this.processSchemaFromSdkType(sdkType, "finalResult");
699
682
  }
700
683
  // track usage
701
684
  if (pollingSchema) {
702
685
  this.trackSchemaUsage(pollingSchema, { usage: [SchemaContext.Output] });
703
- if (op.internalApi) {
704
- this.trackSchemaUsage(pollingSchema, { usage: [SchemaContext.Internal] });
705
- }
706
- else if (op.convenienceApi) {
707
- this.trackSchemaUsage(pollingSchema, { usage: [SchemaContext.Public] });
686
+ if (op.convenienceApi) {
687
+ this.trackSchemaUsage(pollingSchema, {
688
+ usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public],
689
+ });
708
690
  }
709
691
  }
710
692
  if (finalSchema) {
711
693
  this.trackSchemaUsage(finalSchema, { usage: [SchemaContext.Output] });
712
- if (op.internalApi) {
713
- this.trackSchemaUsage(pollingSchema, { usage: [SchemaContext.Internal] });
714
- }
715
- else if (op.convenienceApi) {
716
- this.trackSchemaUsage(finalSchema, { usage: [SchemaContext.Public] });
694
+ if (op.convenienceApi) {
695
+ this.trackSchemaUsage(finalSchema, {
696
+ usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public],
697
+ });
717
698
  }
718
699
  }
719
700
  op.lroMetadata = new LongRunningMetadata(true, pollingSchema, finalSchema, pollingStrategy);
@@ -775,16 +756,18 @@ export class CodeModelBuilder {
775
756
  else {
776
757
  // schema
777
758
  let schema;
759
+ const sdkType = getClientType(this.sdkContext, param.param);
778
760
  if (param.type === "header" &&
779
761
  param.param.type.kind === "Scalar" &&
780
762
  getEncode(this.program, param.param) === undefined &&
781
763
  getEncode(this.program, param.param.type) === undefined &&
782
- (hasScalarAsBase(param.param.type, "utcDateTime") || hasScalarAsBase(param.param.type, "offsetDateTime"))) {
764
+ (hasScalarAsBase(param.param.type, "utcDateTime") || hasScalarAsBase(param.param.type, "offsetDateTime")) &&
765
+ (sdkType.kind === "utcDateTime" || sdkType.kind === "offsetDateTime")) {
783
766
  // utcDateTime in header maps to rfc7231
784
- schema = this.processDateTimeSchema(param.param.type, param.param.name, true);
767
+ schema = this.processDateTimeSchemaFromSdkType(sdkType, param.param.name, true);
785
768
  }
786
769
  else {
787
- schema = this.processSchema(param.param, param.param.name);
770
+ schema = this.processSchemaFromSdkType(sdkType, param.param.name);
788
771
  }
789
772
  // skip-url-encoding
790
773
  let extensions = undefined;
@@ -857,7 +840,6 @@ export class CodeModelBuilder {
857
840
  explode: explode,
858
841
  }),
859
842
  },
860
- // clientDefaultValue: this.getDefaultValue(param.param.default),
861
843
  language: {
862
844
  default: {
863
845
  serializedName: param.name,
@@ -867,11 +849,8 @@ export class CodeModelBuilder {
867
849
  });
868
850
  op.addParameter(parameter);
869
851
  this.trackSchemaUsage(schema, { usage: [SchemaContext.Input] });
870
- if (op.internalApi) {
871
- this.trackSchemaUsage(schema, { usage: [SchemaContext.Internal] });
872
- }
873
- else if (op.convenienceApi) {
874
- this.trackSchemaUsage(schema, { usage: [SchemaContext.Public] });
852
+ if (op.convenienceApi) {
853
+ this.trackSchemaUsage(schema, { usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public] });
875
854
  }
876
855
  if (param.name.toLowerCase() === CONTENT_TYPE_KEY) {
877
856
  let mediaTypes = ["application/json"];
@@ -995,11 +974,10 @@ export class CodeModelBuilder {
995
974
  nullable: true,
996
975
  });
997
976
  this.trackSchemaUsage(requestConditionsSchema, { usage: [SchemaContext.Input] });
998
- if (op.internalApi) {
999
- this.trackSchemaUsage(requestConditionsSchema, { usage: [SchemaContext.Internal] });
1000
- }
1001
- else if (op.convenienceApi) {
1002
- this.trackSchemaUsage(requestConditionsSchema, { usage: [SchemaContext.Public] });
977
+ if (op.convenienceApi) {
978
+ this.trackSchemaUsage(requestConditionsSchema, {
979
+ usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public],
980
+ });
1003
981
  }
1004
982
  // update group schema for properties
1005
983
  for (const parameter of request.parameters) {
@@ -1030,6 +1008,7 @@ export class CodeModelBuilder {
1030
1008
  const unknownRequestBody = op.requests[0].protocol.http.mediaTypes &&
1031
1009
  op.requests[0].protocol.http.mediaTypes.length > 0 &&
1032
1010
  !isKnownContentType(op.requests[0].protocol.http.mediaTypes);
1011
+ const sdkType = getClientType(this.sdkContext, body, httpOperation.operation);
1033
1012
  let schema;
1034
1013
  if (unknownRequestBody &&
1035
1014
  body.kind === "ModelProperty" &&
@@ -1039,8 +1018,9 @@ export class CodeModelBuilder {
1039
1018
  schema = this.processBinarySchema(body.type);
1040
1019
  }
1041
1020
  else {
1042
- schema = this.processSchema(body, body.name);
1021
+ schema = this.processSchemaFromSdkType(sdkType, body.name);
1043
1022
  }
1023
+ const isAnonymousModel = sdkType.kind === "model" && sdkType.isGeneratedName === true;
1044
1024
  const parameter = new Parameter(this.getName(body), this.getDoc(body), schema, {
1045
1025
  summary: this.getSummary(body),
1046
1026
  implementation: ImplementationLocation.Method,
@@ -1048,26 +1028,20 @@ export class CodeModelBuilder {
1048
1028
  protocol: {
1049
1029
  http: new HttpParameter(ParameterLocation.Body),
1050
1030
  },
1051
- // clientDefaultValue: this.getDefaultValue(body.default),
1052
1031
  });
1053
1032
  op.addParameter(parameter);
1054
1033
  this.trackSchemaUsage(schema, { usage: [SchemaContext.Input] });
1055
- if (op.internalApi) {
1056
- this.trackSchemaUsage(schema, { usage: [SchemaContext.Internal] });
1057
- }
1058
- else if (op.convenienceApi) {
1059
- this.trackSchemaUsage(schema, { usage: [SchemaContext.Public] });
1034
+ if (op.convenienceApi) {
1035
+ // model/schema does not need to be Public or Internal, if it is not to be used in convenience API
1036
+ this.trackSchemaUsage(schema, { usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public] });
1060
1037
  }
1061
1038
  if (operationIsJsonMergePatch(httpOperation)) {
1062
1039
  this.trackSchemaUsage(schema, { usage: [SchemaContext.JsonMergePatch] });
1063
1040
  }
1064
1041
  if (op.convenienceApi && operationIsMultipart(httpOperation)) {
1065
- if (schema instanceof ObjectSchema) {
1066
- this.processMultipartFormDataSchema(schema);
1067
- }
1068
1042
  this.trackSchemaUsage(schema, { serializationFormats: [KnownMediaType.Multipart] });
1069
1043
  }
1070
- if (schema instanceof ObjectSchema && !schema.language.default.name) {
1044
+ if (schema instanceof ObjectSchema && isAnonymousModel) {
1071
1045
  // anonymous model
1072
1046
  // name the schema for documentation
1073
1047
  schema.language.default.name = pascalCase(op.language.default.name) + "Request";
@@ -1147,11 +1121,10 @@ export class CodeModelBuilder {
1147
1121
  }));
1148
1122
  });
1149
1123
  this.trackSchemaUsage(optionBagSchema, { usage: [SchemaContext.Input] });
1150
- if (op.internalApi) {
1151
- this.trackSchemaUsage(optionBagSchema, { usage: [SchemaContext.Internal] });
1152
- }
1153
- else if (op.convenienceApi) {
1154
- this.trackSchemaUsage(optionBagSchema, { usage: [SchemaContext.Public] });
1124
+ if (op.convenienceApi) {
1125
+ this.trackSchemaUsage(optionBagSchema, {
1126
+ usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public],
1127
+ });
1155
1128
  }
1156
1129
  // option bag parameter
1157
1130
  const optionBagParameter = new Parameter("options", optionBagSchema.language.default.description, optionBagSchema, {
@@ -1182,7 +1155,8 @@ export class CodeModelBuilder {
1182
1155
  for (const response of resp.responses.values()) {
1183
1156
  if (response.headers) {
1184
1157
  for (const [key, header] of Object.entries(response.headers)) {
1185
- const schema = this.processSchema(header, key);
1158
+ const sdkType = getClientType(this.sdkContext, header);
1159
+ const schema = this.processSchemaFromSdkType(sdkType, key);
1186
1160
  headers.push(new HttpHeader(key, schema, {
1187
1161
  language: {
1188
1162
  default: {
@@ -1266,7 +1240,8 @@ export class CodeModelBuilder {
1266
1240
  schema = op.lroMetadata.pollResultType;
1267
1241
  }
1268
1242
  else {
1269
- schema = this.processSchema(bodyType, op.language.default.name + "Response");
1243
+ const sdkType = getClientType(this.sdkContext, bodyType);
1244
+ schema = this.processSchemaFromSdkType(sdkType, op.language.default.name + "Response");
1270
1245
  }
1271
1246
  }
1272
1247
  response = new SchemaResponse(schema, {
@@ -1314,11 +1289,10 @@ export class CodeModelBuilder {
1314
1289
  op.addResponse(response);
1315
1290
  if (response instanceof SchemaResponse) {
1316
1291
  this.trackSchemaUsage(response.schema, { usage: [SchemaContext.Output] });
1317
- if (op.internalApi) {
1318
- this.trackSchemaUsage(response.schema, { usage: [SchemaContext.Internal] });
1319
- }
1320
- else if (trackConvenienceApi) {
1321
- this.trackSchemaUsage(response.schema, { usage: [SchemaContext.Public] });
1292
+ if (trackConvenienceApi) {
1293
+ this.trackSchemaUsage(response.schema, {
1294
+ usage: [op.internalApi ? SchemaContext.Internal : SchemaContext.Public],
1295
+ });
1322
1296
  }
1323
1297
  }
1324
1298
  }
@@ -1344,229 +1318,150 @@ export class CodeModelBuilder {
1344
1318
  (resp.statusCodes === "*" ? "An unexpected error response" : getStatusCodeDescription(resp.statusCodes)) ||
1345
1319
  "");
1346
1320
  }
1347
- processSchema(type, nameHint) {
1321
+ processSchemaFromSdkType(type, nameHint) {
1348
1322
  return this.schemaCache.process(type, nameHint) || fail("Unable to process schema.");
1349
1323
  }
1350
- processSchemaImpl(type, nameHint) {
1351
- switch (type.kind) {
1352
- case "Intrinsic":
1353
- if (isUnknownType(type)) {
1354
- return this.processAnySchema(type, nameHint);
1355
- }
1356
- else {
1357
- throw new Error(`Unrecognized intrinsic type: '${type.name}'.`);
1358
- }
1359
- case "String":
1360
- return this.processConstantSchemaForLiteral(type, nameHint);
1361
- case "Number":
1362
- return this.processConstantSchemaForLiteral(type, nameHint);
1363
- case "Boolean":
1364
- return this.processConstantSchemaForLiteral(type, nameHint);
1365
- case "Enum":
1366
- return this.processChoiceSchema(type, this.getName(type), true);
1367
- case "Union":
1368
- return this.processUnionSchema(type, this.getName(type, nameHint));
1369
- case "ModelProperty": {
1370
- let schema = undefined;
1371
- const knownValues = getKnownValues(this.program, type);
1372
- if (knownValues) {
1373
- // use it for extensible enum
1374
- schema = this.processChoiceSchema(knownValues, this.getName(knownValues), false);
1375
- }
1376
- else {
1377
- const schemaNameHint = type.type.kind === "Scalar" && this.program.checker.isStdType(type.type)
1378
- ? nameHint // std scalar won't need a nameHint
1379
- : pascalCase(getNamePrefixForProperty(type)) + pascalCase(nameHint);
1380
- schema = this.processSchema(type.type, schemaNameHint);
1381
- }
1382
- return this.applyModelPropertyDecorators(type, nameHint, schema);
1324
+ processSchemaFromSdkTypeImpl(type, nameHint) {
1325
+ if (isSdkBuiltInKind(type.kind)) {
1326
+ return this.processBuiltInFromSdkType(type, nameHint);
1327
+ }
1328
+ else {
1329
+ switch (type.kind) {
1330
+ case "enum":
1331
+ return this.processChoiceSchemaFromSdkType(type, type.name);
1332
+ case "enumvalue":
1333
+ return this.processConstantSchemaFromEnumValueFromSdkType(type, nameHint);
1334
+ case "union":
1335
+ return this.processUnionSchemaFromSdkType(type, type.name);
1336
+ case "model":
1337
+ return this.processObjectSchemaFromSdkType(type, type.name);
1338
+ case "dict":
1339
+ return this.processDictionarySchemaFromSdkType(type, nameHint);
1340
+ case "array":
1341
+ return this.processArraySchemaFromSdkType(type, nameHint);
1342
+ case "duration":
1343
+ return this.processDurationSchemaFromSdkType(type, nameHint, getDurationFormatFromSdkType(type));
1344
+ case "constant":
1345
+ return this.processConstantSchemaFromSdkType(type, nameHint);
1346
+ case "utcDateTime":
1347
+ case "offsetDateTime":
1348
+ if (type.encode === "unixTimestamp") {
1349
+ return this.processUnixTimeSchemaFromSdkType(type, nameHint);
1350
+ }
1351
+ else {
1352
+ return this.processDateTimeSchemaFromSdkType(type, nameHint, type.encode === "rfc7231");
1353
+ }
1383
1354
  }
1384
- case "Scalar":
1385
- return this.processScalar(type, undefined, nameHint);
1386
- case "Model":
1387
- if (isArrayModelType(this.program, type)) {
1388
- return this.processArraySchema(type, nameHint);
1389
- }
1390
- else if (isRecordModelType(this.program, type) && type.properties.size == 0) {
1391
- // "pure" Record that does not have properties in it
1392
- return this.processDictionarySchema(type, nameHint);
1393
- }
1394
- else {
1395
- return this.processObjectSchema(type, this.getName(type, nameHint));
1396
- }
1397
- case "EnumMember":
1398
- // e.g. "type: TypeEnum.EnumValue1"
1399
- return this.processConstantSchemaForEnumMember(type, this.getName(type));
1400
- case "UnionVariant":
1401
- // e.g. "type: Union.Variant1"
1402
- if (type.type.kind === "String" || type.type.kind === "Number" || type.type.kind === "Boolean") {
1403
- return this.processConstantSchemaForUnionVariant(type, this.getName(type));
1404
- }
1405
- else {
1406
- throw new Error(`Unsupported type reference to UnionVariant.`);
1407
- }
1408
1355
  }
1409
1356
  throw new Error(`Unrecognized type: '${type.kind}'.`);
1410
1357
  }
1411
- processScalar(type, formatFromDerived, nameHint) {
1412
- const scalarName = type.name;
1413
- if (this.program.checker.isStdType(type)) {
1414
- nameHint = scalarName;
1415
- switch (scalarName) {
1416
- case "string": {
1417
- const format = formatFromDerived !== null && formatFromDerived !== void 0 ? formatFromDerived : getFormat(this.program, type);
1418
- if (format) {
1419
- return this.processFormatString(type, format, nameHint);
1420
- }
1421
- return this.processStringSchema(type, nameHint);
1422
- }
1358
+ processBuiltInFromSdkType(type, nameHint) {
1359
+ nameHint = nameHint || type.kind;
1360
+ if (isSdkIntKind(type.kind)) {
1361
+ const integerSize = type.kind === "safeint" || type.kind.includes("int64") ? 64 : 32;
1362
+ return this.processIntegerSchemaFromSdkType(type, nameHint, integerSize);
1363
+ }
1364
+ else {
1365
+ switch (type.kind) {
1366
+ case "any":
1367
+ return this.processAnySchemaFromSdkType();
1368
+ case "string":
1369
+ case "password":
1370
+ case "guid":
1371
+ case "ipAddress":
1372
+ case "uuid":
1373
+ case "ipV4Address":
1374
+ case "ipV6Address":
1375
+ case "eTag":
1376
+ case "armId":
1377
+ case "azureLocation":
1378
+ return this.processStringSchemaFromSdkType(type, type.kind);
1379
+ case "float":
1380
+ case "float32":
1381
+ case "float64":
1382
+ return this.processNumberSchemaFromSdkType(type, nameHint);
1383
+ case "decimal":
1384
+ case "decimal128":
1385
+ return this.processDecimalSchemaFromSdkType(type, nameHint);
1423
1386
  case "bytes":
1424
- return this.processByteArraySchema(type, nameHint, false);
1387
+ return this.processByteArraySchemaFromSdkType(type, nameHint);
1425
1388
  case "boolean":
1426
- return this.processBooleanSchema(type, nameHint);
1389
+ return this.processBooleanSchemaFromSdkType(type, nameHint);
1427
1390
  case "plainTime":
1428
- return this.processTimeSchema(type, nameHint);
1391
+ return this.processTimeSchemaFromSdkType(type, nameHint);
1429
1392
  case "plainDate":
1430
- return this.processDateSchema(type, nameHint);
1431
- case "utcDateTime":
1432
- case "offsetDateTime":
1433
- return this.processDateTimeSchema(type, nameHint, false);
1434
- case "duration":
1435
- return this.processDurationSchema(type, nameHint);
1393
+ return this.processDateSchemaFromSdkType(type, nameHint);
1436
1394
  case "url":
1437
- return this.processUrlSchema(type, nameHint);
1438
- }
1439
- if (scalarName.startsWith("decimal")) {
1440
- // decimal
1441
- return this.processDecimalSchema(type, nameHint);
1442
- }
1443
- else if (scalarName.startsWith("int") || scalarName.startsWith("uint") || scalarName === "safeint") {
1444
- // integer
1445
- const integerSize = scalarName === "safeint" || scalarName.includes("int64") ? 64 : 32;
1446
- return this.processIntegerSchema(type, nameHint, integerSize);
1447
- }
1448
- else if (scalarName.startsWith("float")) {
1449
- // float point
1450
- return this.processNumberSchema(type, nameHint);
1451
- }
1452
- else {
1453
- throw new Error(`Unrecognized scalar type: '${scalarName}'.`);
1454
- }
1455
- }
1456
- else {
1457
- const knownValues = getKnownValues(this.program, type);
1458
- if (knownValues) {
1459
- // use it for extensible enum
1460
- return this.processChoiceSchema(knownValues, this.getName(type), false);
1461
- }
1462
- else {
1463
- const encode = getEncode(this.program, type);
1464
- if (encode) {
1465
- // process as encode
1466
- if (encode.encoding === "seconds" && hasScalarAsBase(type, "duration")) {
1467
- return this.processDurationSchema(type, nameHint, getDurationFormat(encode));
1468
- }
1469
- else if ((encode.encoding === "rfc3339" || encode.encoding === "rfc7231" || encode.encoding === "unixTimestamp") &&
1470
- (hasScalarAsBase(type, "utcDateTime") || hasScalarAsBase(type, "offsetDateTime"))) {
1471
- if (encode.encoding === "unixTimestamp") {
1472
- return this.processUnixTimeSchema(type, nameHint);
1473
- }
1474
- else {
1475
- return this.processDateTimeSchema(type, nameHint, encode.encoding === "rfc7231");
1476
- }
1477
- }
1478
- else if (encode.encoding === "base64url" && hasScalarAsBase(type, "bytes")) {
1479
- return this.processByteArraySchema(type, nameHint, true);
1480
- }
1481
- }
1482
- if (type.baseScalar) {
1483
- // fallback to baseScalar
1484
- const schema = this.processScalar(type.baseScalar, getFormat(this.program, type), nameHint);
1485
- const doc = getDoc(this.program, type);
1486
- const summary = getSummary(this.program, type);
1487
- if (doc) {
1488
- schema.language.default.description = doc;
1489
- }
1490
- if (summary) {
1491
- schema.summary = summary;
1492
- }
1493
- return schema;
1494
- }
1495
- else {
1496
- throw new Error(`Unrecognized scalar type: '${scalarName}'.`);
1497
- }
1395
+ case "uri":
1396
+ return this.processUrlSchemaFromSdkType(type, nameHint);
1498
1397
  }
1499
1398
  }
1500
1399
  }
1501
- processAnySchema(type, name) {
1400
+ processAnySchemaFromSdkType() {
1502
1401
  return this.anySchema;
1503
1402
  }
1504
- processStringSchema(type, name) {
1505
- return this.codeModel.schemas.add(new StringSchema(name, this.getDoc(type), {
1506
- summary: this.getSummary(type),
1403
+ processStringSchemaFromSdkType(type, name) {
1404
+ return this.codeModel.schemas.add(new StringSchema(name, this.getDoc(type.__raw), {
1405
+ summary: this.getSummary(type.__raw),
1507
1406
  }));
1508
1407
  }
1509
- processByteArraySchema(type, name, base64Encoded) {
1510
- return this.codeModel.schemas.add(new ByteArraySchema(name, this.getDoc(type), {
1511
- summary: this.getSummary(type),
1408
+ processByteArraySchemaFromSdkType(type, name) {
1409
+ const base64Encoded = type.encode === "base64url";
1410
+ return this.codeModel.schemas.add(new ByteArraySchema(name, this.getDoc(type.__raw), {
1411
+ summary: this.getSummary(type.__raw),
1512
1412
  format: base64Encoded ? "base64url" : "byte",
1513
1413
  }));
1514
1414
  }
1515
- processIntegerSchema(type, name, precision) {
1516
- return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type), SchemaType.Integer, precision, {
1517
- summary: this.getSummary(type),
1415
+ processIntegerSchemaFromSdkType(type, name, precision) {
1416
+ return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type.__raw), SchemaType.Integer, precision, {
1417
+ summary: this.getSummary(type.__raw),
1518
1418
  }));
1519
1419
  }
1520
- processNumberSchema(type, name) {
1521
- return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type), SchemaType.Number, 64, {
1522
- summary: this.getSummary(type),
1420
+ processNumberSchemaFromSdkType(type, name) {
1421
+ return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type.__raw), SchemaType.Number, 64, {
1422
+ summary: this.getSummary(type.__raw),
1523
1423
  }));
1524
1424
  }
1525
- processDecimalSchema(type, name) {
1425
+ processDecimalSchemaFromSdkType(type, name) {
1526
1426
  // "Infinity" maps to "BigDecimal" in Java
1527
- return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type), SchemaType.Number, Infinity, {
1528
- summary: this.getSummary(type),
1427
+ return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type.__raw), SchemaType.Number, Infinity, {
1428
+ summary: this.getSummary(type.__raw),
1529
1429
  }));
1530
1430
  }
1531
- processBooleanSchema(type, name) {
1532
- return this.codeModel.schemas.add(new BooleanSchema(name, this.getDoc(type), {
1533
- summary: this.getSummary(type),
1431
+ processBooleanSchemaFromSdkType(type, name) {
1432
+ return this.codeModel.schemas.add(new BooleanSchema(name, this.getDoc(type.__raw), {
1433
+ summary: this.getSummary(type.__raw),
1534
1434
  }));
1535
1435
  }
1536
- processArraySchema(type, name) {
1537
- const elementSchema = this.processSchema(type.indexer.value, name);
1538
- return this.codeModel.schemas.add(new ArraySchema(name, this.getDoc(type), elementSchema, {
1539
- summary: this.getSummary(type),
1436
+ processArraySchemaFromSdkType(type, name) {
1437
+ const elementSchema = this.processSchemaFromSdkType(type.valueType, name);
1438
+ return this.codeModel.schemas.add(new ArraySchema(name, this.getDoc(type.__raw), elementSchema, {
1439
+ summary: this.getSummary(type.__raw),
1540
1440
  }));
1541
1441
  }
1542
- processDictionarySchema(type, name) {
1543
- const dictSchema = new DictionarySchema(name, this.getDoc(type), null, {
1544
- summary: this.getSummary(type),
1442
+ processDictionarySchemaFromSdkType(type, name) {
1443
+ const dictSchema = new DictionarySchema(name, type.details ? type.details : "", null, {
1444
+ summary: type.description,
1545
1445
  });
1546
1446
  // cache this now before we accidentally recurse on this type.
1547
1447
  if (!this.schemaCache.has(type)) {
1548
1448
  this.schemaCache.set(type, dictSchema);
1549
1449
  }
1550
- const elementSchema = this.processSchema(type.indexer.value, name);
1450
+ const elementSchema = this.processSchemaFromSdkType(type.valueType, name);
1551
1451
  dictSchema.elementType = elementSchema;
1552
- if (type.indexer.value.kind === "Union") {
1553
- dictSchema.nullableItems =
1554
- Array.from(type.indexer.value.variants.values()).findIndex((it) => isNullType(it.type)) >= 0;
1555
- }
1452
+ dictSchema.nullableItems = type.nullableValues;
1556
1453
  return this.codeModel.schemas.add(dictSchema);
1557
1454
  }
1558
- processChoiceSchema(type, name, sealed) {
1559
- const namespace = getNamespace(type);
1560
- const valueType = typeof type.members.values().next().value.value === "number"
1561
- ? isAllValueInteger(Array.from(type.members.values()).map((it) => it.value))
1562
- ? this.integerSchema
1563
- : this.doubleSchema
1564
- : this.stringSchema;
1455
+ processChoiceSchemaFromSdkType(type, name) {
1456
+ var _a;
1457
+ const rawEnumType = type.__raw;
1458
+ const namespace = getNamespace(rawEnumType);
1459
+ const valueType = this.processSchemaFromSdkType(type.valueType, type.valueType.kind);
1565
1460
  const choices = [];
1566
- type.members.forEach((it) => { var _a; return choices.push(new ChoiceValue(it.name, this.getDoc(it), (_a = it.value) !== null && _a !== void 0 ? _a : it.name)); });
1567
- const schemaType = sealed ? SealedChoiceSchema : ChoiceSchema;
1568
- const schema = new schemaType(name, this.getDoc(type), {
1569
- summary: this.getSummary(type),
1461
+ type.values.forEach((it) => { var _a, _b; return choices.push(new ChoiceValue(it.name, (_a = it.description) !== null && _a !== void 0 ? _a : "", (_b = it.value) !== null && _b !== void 0 ? _b : it.name)); });
1462
+ const schemaType = type.isFixed ? SealedChoiceSchema : ChoiceSchema;
1463
+ const schema = new schemaType(type.name ? type.name : name, (_a = type.description) !== null && _a !== void 0 ? _a : "", {
1464
+ summary: this.getSummary(rawEnumType),
1570
1465
  choiceType: valueType,
1571
1466
  choices: choices,
1572
1467
  language: {
@@ -1578,118 +1473,64 @@ export class CodeModelBuilder {
1578
1473
  },
1579
1474
  },
1580
1475
  });
1581
- schema.crossLanguageDefinitionId = getCrossLanguageDefinitionId(type);
1476
+ schema.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
1582
1477
  return this.codeModel.schemas.add(schema);
1583
1478
  }
1584
- processConstantSchemaForLiteral(type, name) {
1585
- const valueType = type.kind === "String"
1586
- ? this.stringSchema
1587
- : type.kind === "Boolean"
1588
- ? this.booleanSchema
1589
- : isAllValueInteger([type.value])
1590
- ? this.integerSchema
1591
- : this.doubleSchema;
1592
- return this.codeModel.schemas.add(new ConstantSchema(name, this.getDoc(type), {
1593
- summary: this.getSummary(type),
1479
+ processConstantSchemaFromSdkType(type, name) {
1480
+ const valueType = this.processSchemaFromSdkType(type.valueType, type.valueType.kind);
1481
+ return this.codeModel.schemas.add(new ConstantSchema(name, this.getDoc(type.__raw), {
1482
+ summary: this.getSummary(type.__raw),
1594
1483
  valueType: valueType,
1595
1484
  value: new ConstantValue(type.value),
1596
1485
  }));
1597
1486
  }
1598
- processConstantSchemaForEnumMember(type, name) {
1487
+ processConstantSchemaFromEnumValueFromSdkType(type, name) {
1599
1488
  var _a;
1600
- const valueType = this.processSchema(type.enum, this.getName(type.enum));
1601
- return this.codeModel.schemas.add(new ConstantSchema(name, this.getDoc(type), {
1602
- summary: this.getSummary(type),
1489
+ const valueType = this.processSchemaFromSdkType(type.enumType, type.enumType.name);
1490
+ return this.codeModel.schemas.add(new ConstantSchema(name, this.getDoc(type.__raw), {
1491
+ summary: this.getSummary(type.__raw),
1603
1492
  valueType: valueType,
1604
1493
  value: new ConstantValue((_a = type.value) !== null && _a !== void 0 ? _a : type.name),
1605
1494
  }));
1606
1495
  }
1607
- processConstantSchemaForUnionVariant(type, name) {
1608
- var _a;
1609
- const valueType = this.processSchema(type.union, this.getName(type.union));
1610
- return this.codeModel.schemas.add(new ConstantSchema(name, this.getDoc(type), {
1611
- summary: this.getSummary(type),
1612
- valueType: valueType,
1613
- value: new ConstantValue((_a = type.type.value) !== null && _a !== void 0 ? _a : type.name),
1614
- }));
1615
- }
1616
- processChoiceSchemaForUnion(type, unionEnum, name) {
1617
- // variants is Literal
1618
- const sealed = !unionEnum.open;
1619
- const variants = [...unionEnum.flattenedMembers.values()];
1620
- const kindIsString = typeof variants[0].value === "string";
1621
- const valueType = kindIsString
1622
- ? this.stringSchema
1623
- : isAllValueInteger(variants.map((it) => it.value))
1624
- ? this.integerSchema
1625
- : this.doubleSchema;
1626
- const choices = [];
1627
- for (const [name, member] of unionEnum.flattenedMembers.entries()) {
1628
- // get name from "UnionVariant | EnumMember"
1629
- let valueName = this.getName(member.type);
1630
- const valueDoc = this.getDoc(member.type);
1631
- if (!valueName) {
1632
- // fallback the name to the dict
1633
- valueName = typeof name === "string" ? name : `${member.value}`;
1634
- }
1635
- // TODO: may need to use getWireName on "member.value"
1636
- choices.push(new ChoiceValue(valueName, valueDoc, member.value));
1637
- }
1638
- const namespace = getNamespace(type);
1639
- const schemaType = sealed ? SealedChoiceSchema : ChoiceSchema;
1640
- const schema = new schemaType(name, this.getDoc(type), {
1641
- summary: this.getSummary(type),
1642
- choiceType: valueType,
1643
- choices: choices,
1644
- language: {
1645
- default: {
1646
- namespace: namespace,
1647
- },
1648
- java: {
1649
- namespace: getJavaNamespace(namespace),
1650
- },
1651
- },
1652
- });
1653
- schema.crossLanguageDefinitionId = getCrossLanguageDefinitionId(type);
1654
- return this.codeModel.schemas.add(schema);
1655
- }
1656
- processUnixTimeSchema(type, name) {
1657
- return this.codeModel.schemas.add(new UnixTimeSchema(name, this.getDoc(type), {
1658
- summary: this.getSummary(type),
1496
+ processUnixTimeSchemaFromSdkType(type, name) {
1497
+ return this.codeModel.schemas.add(new UnixTimeSchema(name, this.getDoc(type.__raw), {
1498
+ summary: this.getSummary(type.__raw),
1659
1499
  }));
1660
1500
  }
1661
- processDateTimeSchema(type, name, rfc1123) {
1662
- return this.codeModel.schemas.add(new DateTimeSchema(name, this.getDoc(type), {
1663
- summary: this.getSummary(type),
1501
+ processDateTimeSchemaFromSdkType(type, name, rfc1123) {
1502
+ return this.codeModel.schemas.add(new DateTimeSchema(name, this.getDoc(type.__raw), {
1503
+ summary: this.getSummary(type.__raw),
1664
1504
  format: rfc1123 ? "date-time-rfc1123" : "date-time",
1665
1505
  }));
1666
1506
  }
1667
- processDateSchema(type, name) {
1668
- return this.codeModel.schemas.add(new DateSchema(name, this.getDoc(type), {
1669
- summary: this.getSummary(type),
1507
+ processDateSchemaFromSdkType(type, name) {
1508
+ return this.codeModel.schemas.add(new DateSchema(name, this.getDoc(type.__raw), {
1509
+ summary: this.getSummary(type.__raw),
1670
1510
  }));
1671
1511
  }
1672
- processTimeSchema(type, name) {
1673
- return this.codeModel.schemas.add(new TimeSchema(name, this.getDoc(type), {
1674
- summary: this.getSummary(type),
1512
+ processTimeSchemaFromSdkType(type, name) {
1513
+ return this.codeModel.schemas.add(new TimeSchema(name, this.getDoc(type.__raw), {
1514
+ summary: this.getSummary(type.__raw),
1675
1515
  }));
1676
1516
  }
1677
- processDurationSchema(type, name, format = "duration-rfc3339") {
1678
- return this.codeModel.schemas.add(new DurationSchema(name, this.getDoc(type), {
1679
- summary: this.getSummary(type),
1517
+ processDurationSchemaFromSdkType(type, name, format = "duration-rfc3339") {
1518
+ return this.codeModel.schemas.add(new DurationSchema(name, this.getDoc(type.__raw), {
1519
+ summary: this.getSummary(type.__raw),
1680
1520
  format: format,
1681
1521
  }));
1682
1522
  }
1683
- processUrlSchema(type, name) {
1684
- return this.codeModel.schemas.add(new UriSchema(name, this.getDoc(type), {
1685
- summary: this.getSummary(type),
1523
+ processUrlSchemaFromSdkType(type, name) {
1524
+ return this.codeModel.schemas.add(new UriSchema(name, this.getDoc(type.__raw), {
1525
+ summary: this.getSummary(type.__raw),
1686
1526
  }));
1687
1527
  }
1688
- processObjectSchema(type, name) {
1689
- var _a, _b, _c;
1690
- const namespace = getNamespace(type);
1691
- const objectSchema = new ObjectScheme(name, this.getDoc(type), {
1692
- summary: this.getSummary(type),
1528
+ processObjectSchemaFromSdkType(type, name) {
1529
+ var _a;
1530
+ const rawModelType = type.__raw;
1531
+ const namespace = getNamespace(rawModelType);
1532
+ const objectSchema = new ObjectScheme(name, this.getDoc(rawModelType), {
1533
+ summary: this.getSummary(rawModelType),
1693
1534
  language: {
1694
1535
  default: {
1695
1536
  namespace: namespace,
@@ -1699,47 +1540,23 @@ export class CodeModelBuilder {
1699
1540
  },
1700
1541
  },
1701
1542
  });
1702
- objectSchema.crossLanguageDefinitionId = getCrossLanguageDefinitionId(type);
1543
+ objectSchema.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
1703
1544
  this.codeModel.schemas.add(objectSchema);
1704
1545
  // cache this now before we accidentally recurse on this type.
1705
1546
  if (!this.schemaCache.has(type)) {
1706
1547
  this.schemaCache.set(type, objectSchema);
1707
1548
  }
1708
1549
  // discriminator
1709
- let discriminatorPropertyName = undefined;
1710
- const discriminator = getDiscriminator(this.program, type);
1711
- if (discriminator) {
1712
- discriminatorPropertyName = discriminator.propertyName;
1713
- // find the discriminator property from model
1714
- // the property is required for getting its serializedName
1715
- let discriminatorProperty = Array.from(type.properties.values()).find((it) => it.name === discriminatorPropertyName);
1716
- if (!discriminatorProperty) {
1717
- // try find the discriminator property from any of its derived models
1718
- for (const deriveModel of type.derivedModels) {
1719
- discriminatorProperty = Array.from(deriveModel.properties.values()).find((it) => it.name === discriminatorPropertyName);
1720
- if (discriminatorProperty) {
1721
- // found
1722
- break;
1723
- }
1724
- }
1725
- }
1726
- if (discriminatorProperty) {
1727
- objectSchema.discriminator = new Discriminator(this.processModelProperty(discriminatorProperty));
1728
- objectSchema.discriminator.property.isDiscriminator = true;
1729
- }
1730
- else {
1731
- // fallback to property name, if cannot find the discriminator property
1732
- objectSchema.discriminator = new Discriminator(new Property(discriminatorPropertyName, discriminatorPropertyName, this.stringSchema, {
1733
- isDiscriminator: true,
1734
- required: true,
1735
- serializedName: discriminatorPropertyName,
1736
- }));
1550
+ if (type.discriminatedSubtypes && type.discriminatorProperty) {
1551
+ objectSchema.discriminator = new Discriminator(this.processModelPropertyFromSdkType(type.discriminatorProperty));
1552
+ for (const discriminatorValue in type.discriminatedSubtypes) {
1553
+ const subType = type.discriminatedSubtypes[discriminatorValue];
1554
+ this.processSchemaFromSdkType(subType, subType.name);
1737
1555
  }
1738
- objectSchema.discriminator.propertyName = discriminatorPropertyName;
1739
1556
  }
1740
- // parent
1557
+ // type is a subtype
1741
1558
  if (type.baseModel) {
1742
- const parentSchema = this.processSchema(type.baseModel, this.getName(type.baseModel));
1559
+ const parentSchema = this.processSchemaFromSdkType(type.baseModel, type.baseModel.name);
1743
1560
  objectSchema.parents = new Relations();
1744
1561
  objectSchema.parents.immediate.push(parentSchema);
1745
1562
  if (parentSchema instanceof ObjectSchema) {
@@ -1756,69 +1573,34 @@ export class CodeModelBuilder {
1756
1573
  });
1757
1574
  }
1758
1575
  }
1759
- else {
1760
- // parentSchema could be DictionarySchema, which means the model is "additionalProperties"
1761
- pushDistinct(objectSchema.parents.all, parentSchema);
1762
- }
1576
+ objectSchema.discriminatorValue = type.discriminatorValue;
1763
1577
  }
1764
- else if (isRecordModelType(this.program, type)) {
1765
- // "pure" Record processed elsewhere
1766
- // "mixed" Record that have properties, treat the model as "additionalProperties"
1767
- /* type should have sourceModel, as
1768
- model Type is Record<> {
1769
- prop1: string
1770
- }
1771
- */
1772
- const parentSchema = type.sourceModel
1773
- ? this.processSchema(type.sourceModel, this.getName(type.sourceModel))
1774
- : // process self as pure Record (instead of Model = Record + properties), do not use cache
1775
- this.processDictionarySchema(type, this.getName(type));
1776
- objectSchema.parents = new Relations();
1578
+ if (type.additionalProperties) {
1579
+ // model has additional property
1580
+ const sdkDictType = {
1581
+ kind: "dict",
1582
+ keyType: {
1583
+ kind: "string",
1584
+ encode: "string",
1585
+ nullable: false,
1586
+ },
1587
+ description: type.description,
1588
+ nullableValues: false,
1589
+ nullable: false,
1590
+ valueType: type.additionalProperties,
1591
+ };
1592
+ const parentSchema = this.processSchemaFromSdkType(sdkDictType, "Record");
1593
+ objectSchema.parents = (_a = objectSchema.parents) !== null && _a !== void 0 ? _a : new Relations();
1777
1594
  objectSchema.parents.immediate.push(parentSchema);
1778
1595
  pushDistinct(objectSchema.parents.all, parentSchema);
1779
- }
1780
- // value of the discriminator property
1781
- if (objectSchema.parents) {
1782
- const parentWithDiscriminator = objectSchema.parents.all.find((it) => it instanceof ObjectSchema && it.discriminator);
1783
- if (parentWithDiscriminator) {
1784
- discriminatorPropertyName = parentWithDiscriminator.discriminator.propertyName;
1785
- const discriminatorProperty = Array.from(type.properties.values()).find((it) => it.name === discriminatorPropertyName &&
1786
- (it.type.kind === "String" || it.type.kind === "EnumMember" || it.type.kind === "UnionVariant"));
1787
- if (discriminatorProperty) {
1788
- if (discriminatorProperty.type.kind === "String") {
1789
- // value as StringLiteral
1790
- objectSchema.discriminatorValue = discriminatorProperty.type.value;
1791
- }
1792
- else if (discriminatorProperty.type.kind === "EnumMember") {
1793
- // value as EnumMember
1794
- // lint requires value be string
1795
- objectSchema.discriminatorValue =
1796
- (_a = discriminatorProperty.type.value) !== null && _a !== void 0 ? _a : discriminatorProperty.type.name;
1797
- }
1798
- else if (discriminatorProperty.type.kind === "UnionVariant") {
1799
- // value as UnionVariant
1800
- objectSchema.discriminatorValue =
1801
- (_b = discriminatorProperty.type.type.value) !== null && _b !== void 0 ? _b : discriminatorProperty.type.name;
1802
- }
1803
- }
1804
- else {
1805
- // it is possible that the property is Union, e.g. 'kind: "type1" | "type2"'; but such Type appears not to be a concrete model.
1806
- // fallback to name of the Model
1807
- objectSchema.discriminatorValue = name;
1808
- }
1809
- }
1596
+ objectSchema.discriminatorValue = type.discriminatorValue;
1810
1597
  }
1811
1598
  // properties
1812
- for (const prop of type.properties.values()) {
1813
- if (prop.name === discriminatorPropertyName || // skip the discriminator property
1814
- isNeverType(prop.type) || // skip property of type "never"
1815
- !isPayloadProperty(this.program, prop)) {
1816
- continue;
1599
+ for (const prop of type.properties) {
1600
+ if (prop.kind === "property" && !prop.discriminator) {
1601
+ objectSchema.addProperty(this.processModelPropertyFromSdkType(prop));
1817
1602
  }
1818
- objectSchema.addProperty(this.processModelProperty(prop));
1819
1603
  }
1820
- // process all children
1821
- (_c = type.derivedModels) === null || _c === void 0 ? void 0 : _c.filter(modelContainsDerivedModel).forEach((it) => this.processSchema(it, this.getName(it)));
1822
1604
  return objectSchema;
1823
1605
  }
1824
1606
  getEffectiveSchemaType(type) {
@@ -1839,81 +1621,12 @@ export class CodeModelBuilder {
1839
1621
  }
1840
1622
  return type;
1841
1623
  }
1842
- applyModelPropertyDecorators(prop, nameHint, schema) {
1843
- // if (schema instanceof StringSchema) {
1844
- // const decorators = {
1845
- // minLength: getMinLength(this.program, prop),
1846
- // maxLength: getMaxLength(this.program, prop),
1847
- // pattern: getPattern(this.program, prop),
1848
- // };
1849
- // if (Object.values(decorators).some((it) => it !== undefined)) {
1850
- // schema = new StringSchema(schema.language.default.name, schema.language.default.description, {
1851
- // language: schema.language,
1852
- // summary: schema.summary,
1853
- // extensions: schema.extensions,
1854
- // ...decorators,
1855
- // });
1856
- // }
1857
- // } else if (schema instanceof NumberSchema) {
1858
- // const decorators = {
1859
- // minimum: getMinValue(this.program, prop),
1860
- // maximum: getMaxValue(this.program, prop),
1861
- // };
1862
- // if (Object.values(decorators).some((it) => it !== undefined)) {
1863
- // schema = new NumberSchema(
1864
- // schema.language.default.name,
1865
- // schema.language.default.description,
1866
- // schema.type,
1867
- // schema.precision,
1868
- // {
1869
- // language: schema.language,
1870
- // summary: schema.summary,
1871
- // extensions: schema.extensions,
1872
- // ...decorators,
1873
- // },
1874
- // );
1875
- // }
1876
- // }
1877
- const format = getFormat(this.program, prop);
1878
- if (format) {
1879
- // TODO: deprecate format
1880
- if (prop.type.kind === "Scalar" && schema instanceof StringSchema) {
1881
- schema = this.processFormatString(prop.type, format, nameHint);
1882
- }
1883
- }
1884
- else {
1885
- // encode
1886
- const encode = getEncode(this.program, prop);
1887
- if (encode) {
1888
- let type = prop.type;
1889
- if (prop.type.kind === "Union" && isNullableType(prop.type)) {
1890
- const nonNullVariants = Array.from(prop.type.variants.values()).filter((it) => !isNullType(it.type));
1891
- type = nonNullVariants[0].type;
1892
- }
1893
- if (type && type.kind === "Scalar") {
1894
- if (encode.encoding === "seconds" && hasScalarAsBase(type, "duration")) {
1895
- schema = this.processDurationSchema(type, nameHint, getDurationFormat(encode));
1896
- }
1897
- else if ((encode.encoding === "rfc3339" || encode.encoding === "rfc7231" || encode.encoding === "unixTimestamp") &&
1898
- (hasScalarAsBase(type, "utcDateTime") || hasScalarAsBase(type, "offsetDateTime"))) {
1899
- if (encode.encoding === "unixTimestamp") {
1900
- return this.processUnixTimeSchema(type, nameHint);
1901
- }
1902
- else {
1903
- return this.processDateTimeSchema(type, nameHint, encode.encoding === "rfc7231");
1904
- }
1905
- }
1906
- else if (encode.encoding === "base64url" && hasScalarAsBase(type, "bytes")) {
1907
- return this.processByteArraySchema(type, nameHint, true);
1908
- }
1909
- }
1910
- }
1911
- }
1912
- return schema;
1913
- }
1914
- processModelProperty(prop) {
1915
- const schema = this.processSchema(prop, prop.name);
1916
- let nullable = isNullableType(prop.type);
1624
+ processModelPropertyFromSdkType(prop) {
1625
+ const rawModelPropertyType = prop.__raw;
1626
+ // TODO: This case is related with literal.tsp, once TCGC supports giving a name, we can use TCGC generatedName
1627
+ const schemaNameHint = pascalCase(getNamePrefixForProperty(rawModelPropertyType)) + pascalCase(prop.name);
1628
+ let schema = this.processSchemaFromSdkType(prop.type, schemaNameHint);
1629
+ let nullable = prop.nullable;
1917
1630
  let extensions = undefined;
1918
1631
  if (this.isSecret(prop)) {
1919
1632
  extensions = extensions !== null && extensions !== void 0 ? extensions : {};
@@ -1921,66 +1634,43 @@ export class CodeModelBuilder {
1921
1634
  // if the property does not return in response, it had to be nullable
1922
1635
  nullable = true;
1923
1636
  }
1924
- if (shouldFlattenProperty(this.sdkContext, prop)) {
1637
+ if (prop.kind === "property" && prop.flatten) {
1925
1638
  extensions = extensions !== null && extensions !== void 0 ? extensions : {};
1926
1639
  extensions["x-ms-client-flatten"] = true;
1927
1640
  }
1928
- return new Property(this.getName(prop), this.getDoc(prop), schema, {
1929
- summary: this.getSummary(prop),
1641
+ if (prop.kind === "property" && prop.isMultipartFileInput) {
1642
+ schema = this.processMultipartFormDataFilePropertySchemaFromSdkType(prop, this.namespace);
1643
+ }
1644
+ return new Property(prop.name, this.getDoc(rawModelPropertyType), schema, {
1645
+ summary: this.getSummary(rawModelPropertyType),
1930
1646
  required: !prop.optional,
1931
1647
  nullable: nullable,
1932
1648
  readOnly: this.isReadOnly(prop),
1933
- // clientDefaultValue: this.getDefaultValue(prop.default),
1934
- serializedName: this.getSerializedName(prop),
1649
+ serializedName: prop.kind === "property" ? prop.serializedName : undefined,
1935
1650
  extensions: extensions,
1936
1651
  });
1937
1652
  }
1938
- processFormatString(type, format, nameHint) {
1939
- switch (format) {
1940
- case "byte":
1941
- return this.processByteArraySchema(type, nameHint, true);
1942
- case "binary":
1943
- return this.processByteArraySchema(type, nameHint, false);
1944
- case "date-time":
1945
- return this.processDateTimeSchema(type, nameHint, false);
1946
- case "date-time-rfc1123":
1947
- return this.processDateTimeSchema(type, nameHint, true);
1948
- case "password":
1949
- case "url":
1950
- case "uuid":
1951
- case "eTag":
1952
- return this.processStringSchema(type, nameHint);
1953
- default:
1954
- this.logWarning(`Unrecognized string format: '${format}'.`);
1955
- return this.processStringSchema(type, nameHint);
1956
- }
1957
- }
1958
- processUnionSchema(type, name) {
1959
- const nonNullVariants = Array.from(type.variants.values()).filter((it) => !isNullType(it.type));
1960
- if (nonNullVariants.length === 1) {
1961
- // nullable
1962
- return this.processSchema(nonNullVariants[0].type, name);
1963
- }
1964
- const unionEnum = ignoreDiagnostics(getUnionAsEnum(type));
1965
- if (unionEnum) {
1966
- // enum
1967
- return this.processChoiceSchemaForUnion(type, unionEnum, name);
1653
+ processUnionSchemaFromSdkType(type, name) {
1654
+ var _a;
1655
+ if (!(type.__raw && type.__raw.kind === "Union")) {
1656
+ throw new Error(`Invalid type for union: '${type.kind}'.`);
1968
1657
  }
1658
+ const rawUnionType = type.__raw;
1969
1659
  // TODO: name from typespec-client-generator-core
1970
- const namespace = getNamespace(type);
1971
- const baseName = pascalCase(name) + "Model";
1972
- this.logWarning(`Convert TypeSpec Union '${getUnionDescription(type, this.typeNameOptions)}' to Class '${baseName}'`);
1973
- const unionSchema = new OrSchema(baseName + "Base", this.getDoc(type), {
1974
- summary: this.getSummary(type),
1660
+ const namespace = getNamespace(rawUnionType);
1661
+ const baseName = (_a = type.name) !== null && _a !== void 0 ? _a : pascalCase(name) + "Model";
1662
+ this.logWarning(`Convert TypeSpec Union '${getUnionDescription(rawUnionType, this.typeNameOptions)}' to Class '${baseName}'`);
1663
+ const unionSchema = new OrSchema(baseName + "Base", this.getDoc(rawUnionType), {
1664
+ summary: this.getSummary(rawUnionType),
1975
1665
  });
1976
1666
  unionSchema.anyOf = [];
1977
- nonNullVariants.forEach((it) => {
1978
- const variantName = this.getUnionVariantName(it.type, { depth: 0 });
1667
+ type.values.forEach((it) => {
1668
+ const variantName = this.getUnionVariantName(it.__raw, { depth: 0 });
1979
1669
  const modelName = variantName + baseName;
1980
1670
  const propertyName = "value";
1981
1671
  // these ObjectSchema is not added to codeModel.schemas
1982
- const objectSchema = new ObjectSchema(modelName, this.getDoc(type), {
1983
- summary: this.getSummary(type),
1672
+ const objectSchema = new ObjectSchema(modelName, this.getDoc(rawUnionType), {
1673
+ summary: this.getSummary(rawUnionType),
1984
1674
  language: {
1985
1675
  default: {
1986
1676
  namespace: namespace,
@@ -1990,11 +1680,10 @@ export class CodeModelBuilder {
1990
1680
  },
1991
1681
  },
1992
1682
  });
1993
- const variantSchema = this.processSchema(it.type, variantName);
1994
- objectSchema.addProperty(new Property(propertyName, this.getDoc(type), variantSchema, {
1995
- summary: this.getSummary(type),
1683
+ const variantSchema = this.processSchemaFromSdkType(it, variantName);
1684
+ objectSchema.addProperty(new Property(propertyName, this.getDoc(rawUnionType), variantSchema, {
1685
+ summary: this.getSummary(rawUnionType),
1996
1686
  required: true,
1997
- nullable: true,
1998
1687
  readOnly: false,
1999
1688
  }));
2000
1689
  unionSchema.anyOf.push(objectSchema);
@@ -2008,6 +1697,9 @@ export class CodeModelBuilder {
2008
1697
  }
2009
1698
  getUnionVariantName(type, option) {
2010
1699
  var _a;
1700
+ if (type === undefined) {
1701
+ throw new Error("type is undefined.");
1702
+ }
2011
1703
  switch (type.kind) {
2012
1704
  case "Scalar": {
2013
1705
  const scalarName = type.name;
@@ -2062,40 +1754,26 @@ export class CodeModelBuilder {
2062
1754
  throw new Error(`Unrecognized type for union variable: '${type.kind}'.`);
2063
1755
  }
2064
1756
  }
2065
- processMultipartFormDataSchema(schema) {
2066
- if (schema.properties) {
2067
- for (const property of schema.properties) {
2068
- if (property.schema instanceof ByteArraySchema) {
2069
- property.schema = getFileDetailsSchema(property.language.default.name, schema.language.default.namespace, this.codeModel.schemas, this.binarySchema, this.stringSchema);
2070
- }
2071
- else if (property.schema instanceof ArraySchema && property.schema.elementType instanceof ByteArraySchema) {
2072
- property.schema = new ArraySchema(property.language.default.name, property.language.default.description, getFileDetailsSchema(property.language.default.name, schema.language.default.namespace, this.codeModel.schemas, this.binarySchema, this.stringSchema));
2073
- }
2074
- }
1757
+ processMultipartFormDataFilePropertySchemaFromSdkType(property, namespace) {
1758
+ var _a;
1759
+ if (property.type.kind === "bytes") {
1760
+ return getFileDetailsSchema(property.name, namespace, this.codeModel.schemas, this.binarySchema, this.stringSchema);
2075
1761
  }
2076
- }
2077
- getDefaultValue(type) {
2078
- if (type) {
2079
- switch (type.kind) {
2080
- case "String":
2081
- return type.value;
2082
- case "Number":
2083
- return type.value;
2084
- case "Boolean":
2085
- return type.value;
2086
- // case "Tuple":
2087
- // return type.values.map(getDefaultValue);
2088
- }
1762
+ else if (property.type.kind === "array" && property.type.valueType.kind === "bytes") {
1763
+ return new ArraySchema(property.name, (_a = property.description) !== null && _a !== void 0 ? _a : "", getFileDetailsSchema(property.name, namespace, this.codeModel.schemas, this.binarySchema, this.stringSchema));
2089
1764
  }
2090
- return undefined;
1765
+ throw new Error(`Invalid type for multipart form data: '${property.type.kind}'.`);
2091
1766
  }
2092
1767
  getDoc(target) {
2093
- return getDoc(this.program, target) || "";
1768
+ return target ? getDoc(this.program, target) || "" : "";
2094
1769
  }
2095
1770
  getSummary(target) {
2096
- return getSummary(this.program, target);
1771
+ return target ? getSummary(this.program, target) : undefined;
2097
1772
  }
2098
1773
  getName(target, nameHint = undefined) {
1774
+ if (!target) {
1775
+ return nameHint || "";
1776
+ }
2099
1777
  // TODO: once getLibraryName API in typespec-client-generator-core can get projected name from language and client, as well as can handle template case, use getLibraryName API
2100
1778
  const emitterClientName = getClientNameOverride(this.sdkContext, target);
2101
1779
  if (emitterClientName && typeof emitterClientName === "string") {
@@ -2139,13 +1817,12 @@ export class CodeModelBuilder {
2139
1817
  return getWireName(this.sdkContext, target);
2140
1818
  }
2141
1819
  isReadOnly(target) {
2142
- // const key = isKey(this.program, target);
2143
- const segment = getSegment(this.program, target) !== undefined;
1820
+ const segment = target.__raw ? getSegment(this.program, target.__raw) !== undefined : false;
2144
1821
  if (segment) {
2145
1822
  return true;
2146
1823
  }
2147
1824
  else {
2148
- const visibility = getVisibility(this.program, target);
1825
+ const visibility = target.__raw ? getVisibility(this.program, target.__raw) : undefined;
2149
1826
  if (visibility) {
2150
1827
  return (!visibility.includes("write") &&
2151
1828
  !visibility.includes("create") &&
@@ -2159,9 +1836,8 @@ export class CodeModelBuilder {
2159
1836
  }
2160
1837
  }
2161
1838
  isSecret(target) {
2162
- const visibility = getVisibility(this.program, target);
2163
- if (visibility) {
2164
- return !visibility.includes("read");
1839
+ if (target.kind === "property" && target.visibility) {
1840
+ return !target.visibility.includes(Visibility.Read);
2165
1841
  }
2166
1842
  else {
2167
1843
  return false;
@@ -2352,22 +2028,5 @@ export class CodeModelBuilder {
2352
2028
  isArm() {
2353
2029
  return Boolean(this.codeModel.arm);
2354
2030
  }
2355
- isSchemaUsageEmpty(schema) {
2356
- if (schema instanceof ObjectSchema ||
2357
- schema instanceof GroupSchema ||
2358
- schema instanceof ChoiceSchema ||
2359
- schema instanceof SealedChoiceSchema ||
2360
- schema instanceof OrSchema ||
2361
- schema instanceof ConstantSchema) {
2362
- return !(schema.usage && schema.usage.length > 0);
2363
- }
2364
- else if (schema instanceof DictionarySchema) {
2365
- return this.isSchemaUsageEmpty(schema.elementType);
2366
- }
2367
- else if (schema instanceof ArraySchema) {
2368
- return this.isSchemaUsageEmpty(schema.elementType);
2369
- }
2370
- return false;
2371
- }
2372
2031
  }
2373
2032
  //# sourceMappingURL=code-model-builder.js.map