@azure-tools/typespec-java 0.15.18 → 0.16.1

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,158 @@ 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
+ var _a;
1405
+ return this.codeModel.schemas.add(new StringSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1406
+ summary: type.description,
1507
1407
  }));
1508
1408
  }
1509
- processByteArraySchema(type, name, base64Encoded) {
1510
- return this.codeModel.schemas.add(new ByteArraySchema(name, this.getDoc(type), {
1511
- summary: this.getSummary(type),
1409
+ processByteArraySchemaFromSdkType(type, name) {
1410
+ var _a;
1411
+ const base64Encoded = type.encode === "base64url";
1412
+ return this.codeModel.schemas.add(new ByteArraySchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1413
+ summary: type.description,
1512
1414
  format: base64Encoded ? "base64url" : "byte",
1513
1415
  }));
1514
1416
  }
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),
1417
+ processIntegerSchemaFromSdkType(type, name, precision) {
1418
+ var _a;
1419
+ return this.codeModel.schemas.add(new NumberSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", SchemaType.Integer, precision, {
1420
+ summary: type.description,
1518
1421
  }));
1519
1422
  }
1520
- processNumberSchema(type, name) {
1521
- return this.codeModel.schemas.add(new NumberSchema(name, this.getDoc(type), SchemaType.Number, 64, {
1522
- summary: this.getSummary(type),
1423
+ processNumberSchemaFromSdkType(type, name) {
1424
+ var _a;
1425
+ return this.codeModel.schemas.add(new NumberSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", SchemaType.Number, 64, {
1426
+ summary: type.description,
1523
1427
  }));
1524
1428
  }
1525
- processDecimalSchema(type, name) {
1429
+ processDecimalSchemaFromSdkType(type, name) {
1430
+ var _a;
1526
1431
  // "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),
1432
+ return this.codeModel.schemas.add(new NumberSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", SchemaType.Number, Infinity, {
1433
+ summary: type.description,
1529
1434
  }));
1530
1435
  }
1531
- processBooleanSchema(type, name) {
1532
- return this.codeModel.schemas.add(new BooleanSchema(name, this.getDoc(type), {
1533
- summary: this.getSummary(type),
1436
+ processBooleanSchemaFromSdkType(type, name) {
1437
+ var _a;
1438
+ return this.codeModel.schemas.add(new BooleanSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1439
+ summary: type.description,
1534
1440
  }));
1535
1441
  }
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),
1442
+ processArraySchemaFromSdkType(type, name) {
1443
+ var _a;
1444
+ const elementSchema = this.processSchemaFromSdkType(type.valueType, name);
1445
+ return this.codeModel.schemas.add(new ArraySchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", elementSchema, {
1446
+ summary: type.description,
1540
1447
  }));
1541
1448
  }
1542
- processDictionarySchema(type, name) {
1543
- const dictSchema = new DictionarySchema(name, this.getDoc(type), null, {
1544
- summary: this.getSummary(type),
1449
+ processDictionarySchemaFromSdkType(type, name) {
1450
+ var _a;
1451
+ const dictSchema = new DictionarySchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", null, {
1452
+ summary: type.description,
1545
1453
  });
1546
1454
  // cache this now before we accidentally recurse on this type.
1547
1455
  if (!this.schemaCache.has(type)) {
1548
1456
  this.schemaCache.set(type, dictSchema);
1549
1457
  }
1550
- const elementSchema = this.processSchema(type.indexer.value, name);
1458
+ const elementSchema = this.processSchemaFromSdkType(type.valueType, name);
1551
1459
  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
- }
1460
+ dictSchema.nullableItems = type.nullableValues;
1556
1461
  return this.codeModel.schemas.add(dictSchema);
1557
1462
  }
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;
1463
+ processChoiceSchemaFromSdkType(type, name) {
1464
+ var _a;
1465
+ const rawEnumType = type.__raw;
1466
+ const namespace = getNamespace(rawEnumType);
1467
+ const valueType = this.processSchemaFromSdkType(type.valueType, type.valueType.kind);
1565
1468
  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),
1469
+ 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)); });
1470
+ const schemaType = type.isFixed ? SealedChoiceSchema : ChoiceSchema;
1471
+ const schema = new schemaType(type.name ? type.name : name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1472
+ summary: type.description,
1570
1473
  choiceType: valueType,
1571
1474
  choices: choices,
1572
1475
  language: {
@@ -1578,118 +1481,71 @@ export class CodeModelBuilder {
1578
1481
  },
1579
1482
  },
1580
1483
  });
1581
- schema.crossLanguageDefinitionId = getCrossLanguageDefinitionId(type);
1484
+ schema.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
1582
1485
  return this.codeModel.schemas.add(schema);
1583
1486
  }
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),
1487
+ processConstantSchemaFromSdkType(type, name) {
1488
+ var _a;
1489
+ const valueType = this.processSchemaFromSdkType(type.valueType, type.valueType.kind);
1490
+ return this.codeModel.schemas.add(new ConstantSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1491
+ summary: type.description,
1594
1492
  valueType: valueType,
1595
1493
  value: new ConstantValue(type.value),
1596
1494
  }));
1597
1495
  }
1598
- processConstantSchemaForEnumMember(type, name) {
1599
- 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),
1496
+ processConstantSchemaFromEnumValueFromSdkType(type, name) {
1497
+ var _a, _b;
1498
+ const valueType = this.processSchemaFromSdkType(type.enumType, type.enumType.name);
1499
+ return this.codeModel.schemas.add(new ConstantSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1500
+ summary: type.description,
1603
1501
  valueType: valueType,
1604
- value: new ConstantValue((_a = type.value) !== null && _a !== void 0 ? _a : type.name),
1502
+ value: new ConstantValue((_b = type.value) !== null && _b !== void 0 ? _b : type.name),
1605
1503
  }));
1606
1504
  }
1607
- processConstantSchemaForUnionVariant(type, name) {
1505
+ processUnixTimeSchemaFromSdkType(type, name) {
1608
1506
  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),
1507
+ return this.codeModel.schemas.add(new UnixTimeSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1508
+ summary: type.description,
1659
1509
  }));
1660
1510
  }
1661
- processDateTimeSchema(type, name, rfc1123) {
1662
- return this.codeModel.schemas.add(new DateTimeSchema(name, this.getDoc(type), {
1663
- summary: this.getSummary(type),
1511
+ processDateTimeSchemaFromSdkType(type, name, rfc1123) {
1512
+ var _a;
1513
+ return this.codeModel.schemas.add(new DateTimeSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1514
+ summary: type.description,
1664
1515
  format: rfc1123 ? "date-time-rfc1123" : "date-time",
1665
1516
  }));
1666
1517
  }
1667
- processDateSchema(type, name) {
1668
- return this.codeModel.schemas.add(new DateSchema(name, this.getDoc(type), {
1669
- summary: this.getSummary(type),
1518
+ processDateSchemaFromSdkType(type, name) {
1519
+ var _a;
1520
+ return this.codeModel.schemas.add(new DateSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1521
+ summary: type.description,
1670
1522
  }));
1671
1523
  }
1672
- processTimeSchema(type, name) {
1673
- return this.codeModel.schemas.add(new TimeSchema(name, this.getDoc(type), {
1674
- summary: this.getSummary(type),
1524
+ processTimeSchemaFromSdkType(type, name) {
1525
+ var _a;
1526
+ return this.codeModel.schemas.add(new TimeSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1527
+ summary: type.description,
1675
1528
  }));
1676
1529
  }
1677
- processDurationSchema(type, name, format = "duration-rfc3339") {
1678
- return this.codeModel.schemas.add(new DurationSchema(name, this.getDoc(type), {
1679
- summary: this.getSummary(type),
1530
+ processDurationSchemaFromSdkType(type, name, format = "duration-rfc3339") {
1531
+ var _a;
1532
+ return this.codeModel.schemas.add(new DurationSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1533
+ summary: type.description,
1680
1534
  format: format,
1681
1535
  }));
1682
1536
  }
1683
- processUrlSchema(type, name) {
1684
- return this.codeModel.schemas.add(new UriSchema(name, this.getDoc(type), {
1685
- summary: this.getSummary(type),
1537
+ processUrlSchemaFromSdkType(type, name) {
1538
+ var _a;
1539
+ return this.codeModel.schemas.add(new UriSchema(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1540
+ summary: type.description,
1686
1541
  }));
1687
1542
  }
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),
1543
+ processObjectSchemaFromSdkType(type, name) {
1544
+ var _a, _b;
1545
+ const rawModelType = type.__raw;
1546
+ const namespace = getNamespace(rawModelType);
1547
+ const objectSchema = new ObjectScheme(name, (_a = type.details) !== null && _a !== void 0 ? _a : "", {
1548
+ summary: type.description,
1693
1549
  language: {
1694
1550
  default: {
1695
1551
  namespace: namespace,
@@ -1699,47 +1555,23 @@ export class CodeModelBuilder {
1699
1555
  },
1700
1556
  },
1701
1557
  });
1702
- objectSchema.crossLanguageDefinitionId = getCrossLanguageDefinitionId(type);
1558
+ objectSchema.crossLanguageDefinitionId = type.crossLanguageDefinitionId;
1703
1559
  this.codeModel.schemas.add(objectSchema);
1704
1560
  // cache this now before we accidentally recurse on this type.
1705
1561
  if (!this.schemaCache.has(type)) {
1706
1562
  this.schemaCache.set(type, objectSchema);
1707
1563
  }
1708
1564
  // 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
- }));
1565
+ if (type.discriminatedSubtypes && type.discriminatorProperty) {
1566
+ objectSchema.discriminator = new Discriminator(this.processModelPropertyFromSdkType(type.discriminatorProperty));
1567
+ for (const discriminatorValue in type.discriminatedSubtypes) {
1568
+ const subType = type.discriminatedSubtypes[discriminatorValue];
1569
+ this.processSchemaFromSdkType(subType, subType.name);
1737
1570
  }
1738
- objectSchema.discriminator.propertyName = discriminatorPropertyName;
1739
1571
  }
1740
- // parent
1572
+ // type is a subtype
1741
1573
  if (type.baseModel) {
1742
- const parentSchema = this.processSchema(type.baseModel, this.getName(type.baseModel));
1574
+ const parentSchema = this.processSchemaFromSdkType(type.baseModel, type.baseModel.name);
1743
1575
  objectSchema.parents = new Relations();
1744
1576
  objectSchema.parents.immediate.push(parentSchema);
1745
1577
  if (parentSchema instanceof ObjectSchema) {
@@ -1756,69 +1588,34 @@ export class CodeModelBuilder {
1756
1588
  });
1757
1589
  }
1758
1590
  }
1759
- else {
1760
- // parentSchema could be DictionarySchema, which means the model is "additionalProperties"
1761
- pushDistinct(objectSchema.parents.all, parentSchema);
1762
- }
1591
+ objectSchema.discriminatorValue = type.discriminatorValue;
1763
1592
  }
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();
1593
+ if (type.additionalProperties) {
1594
+ // model has additional property
1595
+ const sdkDictType = {
1596
+ kind: "dict",
1597
+ keyType: {
1598
+ kind: "string",
1599
+ encode: "string",
1600
+ nullable: false,
1601
+ },
1602
+ description: type.description,
1603
+ nullableValues: false,
1604
+ nullable: false,
1605
+ valueType: type.additionalProperties,
1606
+ };
1607
+ const parentSchema = this.processSchemaFromSdkType(sdkDictType, "Record");
1608
+ objectSchema.parents = (_b = objectSchema.parents) !== null && _b !== void 0 ? _b : new Relations();
1777
1609
  objectSchema.parents.immediate.push(parentSchema);
1778
1610
  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
- }
1611
+ objectSchema.discriminatorValue = type.discriminatorValue;
1810
1612
  }
1811
1613
  // 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;
1614
+ for (const prop of type.properties) {
1615
+ if (prop.kind === "property" && !prop.discriminator) {
1616
+ objectSchema.addProperty(this.processModelPropertyFromSdkType(prop));
1817
1617
  }
1818
- objectSchema.addProperty(this.processModelProperty(prop));
1819
1618
  }
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
1619
  return objectSchema;
1823
1620
  }
1824
1621
  getEffectiveSchemaType(type) {
@@ -1839,81 +1636,13 @@ export class CodeModelBuilder {
1839
1636
  }
1840
1637
  return type;
1841
1638
  }
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);
1639
+ processModelPropertyFromSdkType(prop) {
1640
+ var _a;
1641
+ const rawModelPropertyType = prop.__raw;
1642
+ // TODO: This case is related with literal.tsp, once TCGC supports giving a name, we can use TCGC generatedName
1643
+ const schemaNameHint = pascalCase(getNamePrefixForProperty(rawModelPropertyType)) + pascalCase(prop.name);
1644
+ let schema = this.processSchemaFromSdkType(prop.type, schemaNameHint);
1645
+ let nullable = prop.nullable;
1917
1646
  let extensions = undefined;
1918
1647
  if (this.isSecret(prop)) {
1919
1648
  extensions = extensions !== null && extensions !== void 0 ? extensions : {};
@@ -1921,66 +1650,49 @@ export class CodeModelBuilder {
1921
1650
  // if the property does not return in response, it had to be nullable
1922
1651
  nullable = true;
1923
1652
  }
1924
- if (shouldFlattenProperty(this.sdkContext, prop)) {
1653
+ if (prop.kind === "property" && prop.flatten) {
1925
1654
  extensions = extensions !== null && extensions !== void 0 ? extensions : {};
1926
1655
  extensions["x-ms-client-flatten"] = true;
1927
1656
  }
1928
- return new Property(this.getName(prop), this.getDoc(prop), schema, {
1929
- summary: this.getSummary(prop),
1657
+ const mutability = this.getMutability(prop);
1658
+ if (mutability) {
1659
+ extensions = extensions !== null && extensions !== void 0 ? extensions : {};
1660
+ extensions["x-ms-mutability"] = mutability;
1661
+ }
1662
+ if (prop.kind === "property" && prop.isMultipartFileInput) {
1663
+ schema = this.processMultipartFormDataFilePropertySchemaFromSdkType(prop, this.namespace);
1664
+ }
1665
+ return new Property(prop.name, (_a = prop.details) !== null && _a !== void 0 ? _a : "", schema, {
1666
+ summary: prop.description,
1930
1667
  required: !prop.optional,
1931
1668
  nullable: nullable,
1932
1669
  readOnly: this.isReadOnly(prop),
1933
- // clientDefaultValue: this.getDefaultValue(prop.default),
1934
- serializedName: this.getSerializedName(prop),
1670
+ serializedName: prop.kind === "property" ? prop.serializedName : undefined,
1935
1671
  extensions: extensions,
1936
1672
  });
1937
1673
  }
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);
1674
+ processUnionSchemaFromSdkType(type, name) {
1675
+ var _a, _b;
1676
+ if (!(type.__raw && type.__raw.kind === "Union")) {
1677
+ throw new Error(`Invalid type for union: '${type.kind}'.`);
1968
1678
  }
1679
+ const rawUnionType = type.__raw;
1969
1680
  // 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),
1681
+ const namespace = getNamespace(rawUnionType);
1682
+ const baseName = (_a = type.name) !== null && _a !== void 0 ? _a : pascalCase(name) + "Model";
1683
+ this.logWarning(`Convert TypeSpec Union '${getUnionDescription(rawUnionType, this.typeNameOptions)}' to Class '${baseName}'`);
1684
+ const unionSchema = new OrSchema(baseName + "Base", (_b = type.details) !== null && _b !== void 0 ? _b : "", {
1685
+ summary: type.description,
1975
1686
  });
1976
1687
  unionSchema.anyOf = [];
1977
- nonNullVariants.forEach((it) => {
1978
- const variantName = this.getUnionVariantName(it.type, { depth: 0 });
1688
+ type.values.forEach((it) => {
1689
+ var _a, _b;
1690
+ const variantName = this.getUnionVariantName(it.__raw, { depth: 0 });
1979
1691
  const modelName = variantName + baseName;
1980
1692
  const propertyName = "value";
1981
1693
  // these ObjectSchema is not added to codeModel.schemas
1982
- const objectSchema = new ObjectSchema(modelName, this.getDoc(type), {
1983
- summary: this.getSummary(type),
1694
+ const objectSchema = new ObjectSchema(modelName, (_a = it.details) !== null && _a !== void 0 ? _a : "", {
1695
+ summary: it.description,
1984
1696
  language: {
1985
1697
  default: {
1986
1698
  namespace: namespace,
@@ -1990,11 +1702,10 @@ export class CodeModelBuilder {
1990
1702
  },
1991
1703
  },
1992
1704
  });
1993
- const variantSchema = this.processSchema(it.type, variantName);
1994
- objectSchema.addProperty(new Property(propertyName, this.getDoc(type), variantSchema, {
1995
- summary: this.getSummary(type),
1705
+ const variantSchema = this.processSchemaFromSdkType(it, variantName);
1706
+ objectSchema.addProperty(new Property(propertyName, (_b = type.details) !== null && _b !== void 0 ? _b : "", variantSchema, {
1707
+ summary: type.description,
1996
1708
  required: true,
1997
- nullable: true,
1998
1709
  readOnly: false,
1999
1710
  }));
2000
1711
  unionSchema.anyOf.push(objectSchema);
@@ -2008,6 +1719,9 @@ export class CodeModelBuilder {
2008
1719
  }
2009
1720
  getUnionVariantName(type, option) {
2010
1721
  var _a;
1722
+ if (type === undefined) {
1723
+ throw new Error("type is undefined.");
1724
+ }
2011
1725
  switch (type.kind) {
2012
1726
  case "Scalar": {
2013
1727
  const scalarName = type.name;
@@ -2062,40 +1776,26 @@ export class CodeModelBuilder {
2062
1776
  throw new Error(`Unrecognized type for union variable: '${type.kind}'.`);
2063
1777
  }
2064
1778
  }
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
- }
1779
+ processMultipartFormDataFilePropertySchemaFromSdkType(property, namespace) {
1780
+ var _a;
1781
+ if (property.type.kind === "bytes") {
1782
+ return getFileDetailsSchema(property.name, namespace, this.codeModel.schemas, this.binarySchema, this.stringSchema);
2075
1783
  }
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
- }
1784
+ else if (property.type.kind === "array" && property.type.valueType.kind === "bytes") {
1785
+ 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
1786
  }
2090
- return undefined;
1787
+ throw new Error(`Invalid type for multipart form data: '${property.type.kind}'.`);
2091
1788
  }
2092
1789
  getDoc(target) {
2093
- return getDoc(this.program, target) || "";
1790
+ return target ? getDoc(this.program, target) || "" : "";
2094
1791
  }
2095
1792
  getSummary(target) {
2096
- return getSummary(this.program, target);
1793
+ return target ? getSummary(this.program, target) : undefined;
2097
1794
  }
2098
1795
  getName(target, nameHint = undefined) {
1796
+ if (!target) {
1797
+ return nameHint || "";
1798
+ }
2099
1799
  // 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
1800
  const emitterClientName = getClientNameOverride(this.sdkContext, target);
2101
1801
  if (emitterClientName && typeof emitterClientName === "string") {
@@ -2139,13 +1839,12 @@ export class CodeModelBuilder {
2139
1839
  return getWireName(this.sdkContext, target);
2140
1840
  }
2141
1841
  isReadOnly(target) {
2142
- // const key = isKey(this.program, target);
2143
- const segment = getSegment(this.program, target) !== undefined;
1842
+ const segment = target.__raw ? getSegment(this.program, target.__raw) !== undefined : false;
2144
1843
  if (segment) {
2145
1844
  return true;
2146
1845
  }
2147
1846
  else {
2148
- const visibility = getVisibility(this.program, target);
1847
+ const visibility = target.__raw ? getVisibility(this.program, target.__raw) : undefined;
2149
1848
  if (visibility) {
2150
1849
  return (!visibility.includes("write") &&
2151
1850
  !visibility.includes("create") &&
@@ -2159,14 +1858,37 @@ export class CodeModelBuilder {
2159
1858
  }
2160
1859
  }
2161
1860
  isSecret(target) {
2162
- const visibility = getVisibility(this.program, target);
2163
- if (visibility) {
2164
- return !visibility.includes("read");
1861
+ if (target.kind === "property" && target.visibility) {
1862
+ return !target.visibility.includes(Visibility.Read);
2165
1863
  }
2166
1864
  else {
2167
1865
  return false;
2168
1866
  }
2169
1867
  }
1868
+ getMutability(target) {
1869
+ if (target.kind === "property" && target.visibility) {
1870
+ const mutability = [];
1871
+ if (target.visibility.includes(Visibility.Create)) {
1872
+ mutability.push("create");
1873
+ }
1874
+ if (target.visibility.includes(Visibility.Update)) {
1875
+ mutability.push("update");
1876
+ }
1877
+ if (target.visibility.includes(Visibility.Read)) {
1878
+ mutability.push("read");
1879
+ }
1880
+ if (mutability.length === 3) {
1881
+ // if all 3 (supported) mutability values are present, there is no need to set the x-ms-mutability
1882
+ return undefined;
1883
+ }
1884
+ else {
1885
+ return mutability;
1886
+ }
1887
+ }
1888
+ else {
1889
+ return undefined;
1890
+ }
1891
+ }
2170
1892
  getConvenienceApiName(op) {
2171
1893
  // check @convenienceMethod
2172
1894
  if (shouldGenerateConvenient(this.sdkContext, op)) {
@@ -2352,22 +2074,5 @@ export class CodeModelBuilder {
2352
2074
  isArm() {
2353
2075
  return Boolean(this.codeModel.arm);
2354
2076
  }
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
2077
  }
2373
2078
  //# sourceMappingURL=code-model-builder.js.map