@azure-tools/typespec-java 0.13.5 → 0.13.7

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.
@@ -61,6 +61,7 @@ export declare class CodeModelBuilder {
61
61
  private processChoiceSchema;
62
62
  private processConstantSchemaForLiteral;
63
63
  private processConstantSchemaForEnumMember;
64
+ private processConstantSchemaForUnionVariant;
64
65
  private processChoiceSchemaForUnion;
65
66
  private processUnixTimeSchema;
66
67
  private processDateTimeSchema;
@@ -1 +1 @@
1
- {"version":3,"file":"code-model-builder.d.ts","sourceRoot":"","sources":["../../src/code-model-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAoBL,OAAO,EAGP,IAAI,EAEJ,KAAK,EASL,WAAW,EAUZ,MAAM,oBAAoB,CAAC;AAmC5B,OAAO,EACL,SAAS,EAIT,aAAa,EAUb,YAAY,EACZ,YAAY,EACZ,SAAS,EAKT,MAAM,EAIN,YAAY,EAeb,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAUnD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAW9C,OAAO,EACL,eAAe,EAehB,MAAM,iBAAiB,CAAC;AAmBzB,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAa;IAE/B,OAAO,CAAC,OAAO,CAAiB;IAEhC,OAAO,CAAC,SAAS,CAAY;IAE7B,QAAQ,CAAC,WAAW,gCAAyF;IAC7G,QAAQ,CAAC,iBAAiB,sCAA6C;IAEvE,OAAO,CAAC,iBAAiB,CAAkD;gBAExD,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,CAAC,cAAc,CAAC;IAyD7D,KAAK,IAAI,OAAO,CAAC,SAAS,CAAC;IAkBxC,OAAO,CAAC,WAAW;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AO,CAAC,KAAK;IAIb,OAAO,CAAC,kBAAkB;CAiB3B"}
@@ -1,9 +1,9 @@
1
- import { getDoc, getEffectiveModelType, getFormat, getFriendlyName, getKnownValues, getSummary, getVisibility, ignoreDiagnostics, isArrayModelType, isRecordModelType, isUnknownType, getDiscriminator, isNeverType, listServices, getNamespaceFullName, isNullType, getTypeName, getProjectedName, getEncode, getOverloadedOperation, isErrorModel, walkPropertiesInherited, getService, resolveEncodedName, isVoidType, } from "@typespec/compiler";
1
+ import { getDoc, getEffectiveModelType, getFormat, getFriendlyName, getKnownValues, getSummary, getVisibility, ignoreDiagnostics, isArrayModelType, isRecordModelType, isUnknownType, getDiscriminator, isNeverType, listServices, getNamespaceFullName, isNullType, getTypeName, getProjectedName, getEncode, getOverloadedOperation, isErrorModel, walkPropertiesInherited, getService, isVoidType, } from "@typespec/compiler";
2
2
  import { getResourceOperation, getSegment } from "@typespec/rest";
3
3
  import { getAuthentication, getServers, getStatusCodeDescription, getHttpOperation, getQueryParamOptions, getHeaderFieldOptions, isPathParam, } from "@typespec/http";
4
4
  import { getAddedOnVersions, getVersion } from "@typespec/versioning";
5
- import { isPollingLocation, getPagedResult, isFixed, getLroMetadata } from "@azure-tools/typespec-azure-core";
6
- import { listClients, listOperationGroups, listOperationsInOperationGroup, isApiVersion, shouldGenerateConvenient, createSdkContext, shouldGenerateProtocol, isInternal, getCrossLanguageDefinitionId, getClientNameOverride, shouldFlattenProperty, } from "@azure-tools/typespec-client-generator-core";
5
+ import { isPollingLocation, getPagedResult, isFixed, getLroMetadata, getUnionAsEnum, } from "@azure-tools/typespec-azure-core";
6
+ import { listClients, listOperationGroups, listOperationsInOperationGroup, isApiVersion, shouldGenerateConvenient, createSdkContext, shouldGenerateProtocol, isInternal, getCrossLanguageDefinitionId, getClientNameOverride, shouldFlattenProperty, getWireName, } from "@azure-tools/typespec-client-generator-core";
7
7
  import { fail } from "assert";
8
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
9
  import { KnownMediaType } from "@azure-tools/codegen";
@@ -20,7 +20,7 @@ import { PreNamer } from "./prenamer/prenamer.js";
20
20
  import { createPollOperationDetailsSchema } from "./external-schemas.js";
21
21
  import { ClientContext } from "./models.js";
22
22
  import { stringArrayContainsIgnoreCase, getJavaNamespace, getNamespace, pascalCase, logWarning, trace, } from "./utils.js";
23
- import { ProcessingCache, isModelReferredInTemplate, pushDistinct, modelContainsDerivedModel, getNameForTemplate, getDurationFormat, hasScalarAsBase, isNullableType, isSameLiteralTypes, getAccess, getUsage, getUnionDescription, modelIs, getModelNameForProperty, isAllValueInteger, } from "./type-utils.js";
23
+ import { ProcessingCache, isModelReferredInTemplate, pushDistinct, modelContainsDerivedModel, getNameForTemplate, getDurationFormat, hasScalarAsBase, isNullableType, getAccess, getUsage, getUnionDescription, modelIs, getModelNameForProperty, isAllValueInteger, } from "./type-utils.js";
24
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
25
  import { isArmCommonType } from "./type-utils.js";
26
26
  import pkg from "lodash";
@@ -211,6 +211,7 @@ export class CodeModelBuilder {
211
211
  for (const client of clients) {
212
212
  const models = Array.from(client.service.models.values());
213
213
  Array.from(client.service.enums.values()).forEach((it) => models.push(it));
214
+ Array.from(client.service.unions.values()).forEach((it) => models.push(it));
214
215
  // lambda to mark model as public
215
216
  const modelAsPublic = (model) => {
216
217
  // check it does not contain Union
@@ -222,7 +223,7 @@ export class CodeModelBuilder {
222
223
  // )}' cannot be set as access=public, as it refers Union '${getUnionDescription(union, this.typeNameOptions)}'`;
223
224
  // throw new Error(errorMsg);
224
225
  // }
225
- const schema = this.processSchema(model, model.name);
226
+ const schema = this.processSchema(model, "");
226
227
  this.trackSchemaUsage(schema, {
227
228
  usage: [SchemaContext.Public],
228
229
  });
@@ -234,14 +235,14 @@ export class CodeModelBuilder {
234
235
  modelAsPublic(model);
235
236
  }
236
237
  else if (access === "internal") {
237
- const schema = this.processSchema(model, model.name);
238
+ const schema = this.processSchema(model, "");
238
239
  this.trackSchemaUsage(schema, {
239
240
  usage: [SchemaContext.Internal],
240
241
  });
241
242
  }
242
243
  const usage = getUsage(model);
243
244
  if (usage) {
244
- const schema = this.processSchema(model, model.name);
245
+ const schema = this.processSchema(model, "");
245
246
  this.trackSchemaUsage(schema, {
246
247
  usage: usage,
247
248
  });
@@ -1109,27 +1110,35 @@ export class CodeModelBuilder {
1109
1110
  processResponse(op, resp, candidateResponseSchema = undefined) {
1110
1111
  var _a, _b, _c, _d, _e, _f;
1111
1112
  // TODO: what to do if more than 1 response?
1113
+ // It happens when the response type is Union, on one status code.
1112
1114
  let response;
1113
1115
  let headers = undefined;
1114
- if (resp.responses && resp.responses.length > 0 && resp.responses[0].headers) {
1116
+ if (resp.responses && resp.responses.length > 0) {
1115
1117
  // headers
1116
1118
  headers = [];
1117
- for (const [key, header] of Object.entries(resp.responses[0].headers)) {
1118
- const schema = this.processSchema(header, key);
1119
- headers.push(new HttpHeader(key, schema, {
1120
- language: {
1121
- default: {
1122
- name: key,
1123
- description: this.getDoc(header),
1124
- },
1125
- },
1126
- }));
1119
+ for (const response of resp.responses.values()) {
1120
+ if (response.headers) {
1121
+ for (const [key, header] of Object.entries(response.headers)) {
1122
+ const schema = this.processSchema(header, key);
1123
+ headers.push(new HttpHeader(key, schema, {
1124
+ language: {
1125
+ default: {
1126
+ name: key,
1127
+ description: this.getDoc(header),
1128
+ },
1129
+ },
1130
+ }));
1131
+ }
1132
+ }
1127
1133
  }
1128
1134
  }
1135
+ let responseBody = undefined;
1129
1136
  let bodyType = undefined;
1130
1137
  let trackConvenienceApi = (_a = op.convenienceApi) !== null && _a !== void 0 ? _a : false;
1131
1138
  if (resp.responses && resp.responses.length > 0 && resp.responses[0].body) {
1132
- const responseBody = resp.responses[0].body;
1139
+ responseBody = resp.responses[0].body;
1140
+ }
1141
+ if (responseBody) {
1133
1142
  const unknownResponseBody = responseBody.contentTypes.length > 0 && !isKnownContentType(responseBody.contentTypes);
1134
1143
  bodyType = this.findResponseBody(responseBody.type);
1135
1144
  if (unknownResponseBody && bodyType.kind === "Scalar" && bodyType.name === "bytes") {
@@ -1287,7 +1296,6 @@ export class CodeModelBuilder {
1287
1296
  case "String":
1288
1297
  return this.processConstantSchemaForLiteral(type, nameHint);
1289
1298
  case "Number":
1290
- // TODO: float
1291
1299
  return this.processConstantSchemaForLiteral(type, nameHint);
1292
1300
  case "Boolean":
1293
1301
  return this.processConstantSchemaForLiteral(type, nameHint);
@@ -1326,6 +1334,14 @@ export class CodeModelBuilder {
1326
1334
  case "EnumMember":
1327
1335
  // e.g. "type: TypeEnum.EnumValue1"
1328
1336
  return this.processConstantSchemaForEnumMember(type, this.getName(type));
1337
+ case "UnionVariant":
1338
+ // e.g. "type: Union.Variant1"
1339
+ if (type.type.kind === "String" || type.type.kind === "Number" || type.type.kind === "Boolean") {
1340
+ return this.processConstantSchemaForUnionVariant(type, this.getName(type));
1341
+ }
1342
+ else {
1343
+ throw new Error(`Unsupported type reference to UnionVariant.`);
1344
+ }
1329
1345
  }
1330
1346
  throw new Error(`Unrecognized type: '${type.kind}'.`);
1331
1347
  }
@@ -1523,23 +1539,51 @@ export class CodeModelBuilder {
1523
1539
  value: new ConstantValue((_a = type.value) !== null && _a !== void 0 ? _a : type.name),
1524
1540
  }));
1525
1541
  }
1526
- processChoiceSchemaForUnion(type, variants, name) {
1542
+ processConstantSchemaForUnionVariant(type, name) {
1543
+ var _a;
1544
+ const valueType = this.processSchema(type.union, this.getName(type.union));
1545
+ return this.codeModel.schemas.add(new ConstantSchema(name, this.getDoc(type), {
1546
+ summary: this.getSummary(type),
1547
+ valueType: valueType,
1548
+ value: new ConstantValue((_a = type.type.value) !== null && _a !== void 0 ? _a : type.name),
1549
+ }));
1550
+ }
1551
+ processChoiceSchemaForUnion(type, unionEnum, name) {
1527
1552
  // variants is Literal
1528
- const kindSet = new Set(variants.map((it) => it.type.kind));
1529
- // "choice1" | "choice2" is sealed
1530
- // "choice1" | "choice2" | string is extensible
1531
- const sealed = kindSet.size === 1;
1532
- variants = variants.filter((it) => it.type.kind === "String" || it.type.kind === "Number" || it.type.kind === "Boolean");
1533
- const kind = variants[0].type.kind;
1534
- const valueType = kind === "String"
1553
+ const sealed = !unionEnum.open;
1554
+ const variants = [...unionEnum.flattenedMembers.values()];
1555
+ const kindIsString = typeof variants[0].value === "string";
1556
+ const valueType = kindIsString
1535
1557
  ? this.stringSchema
1536
- : kind === "Boolean"
1537
- ? this.booleanSchema
1538
- : isAllValueInteger(variants.map((it) => it.type.value))
1539
- ? this.integerSchema
1540
- : this.doubleSchema;
1558
+ : isAllValueInteger(variants.map((it) => it.value))
1559
+ ? this.integerSchema
1560
+ : this.doubleSchema;
1541
1561
  const choices = [];
1542
- variants.forEach((it) => choices.push(new ChoiceValue(it.type.value.toString(), this.getDoc(it), it.type.value)));
1562
+ const getUnionVariant = (member) => {
1563
+ if (member.variant) {
1564
+ return member.variant;
1565
+ }
1566
+ else if (member.type) {
1567
+ // TODO: remove this; in current runtime, the UnionVariant is from this "type" instead of "variant"
1568
+ return member.type;
1569
+ }
1570
+ else {
1571
+ return undefined;
1572
+ }
1573
+ };
1574
+ variants.forEach((it) => {
1575
+ const unionVariant = getUnionVariant(it);
1576
+ let name = it.value.toString();
1577
+ let doc = it.value.toString();
1578
+ if (unionVariant) {
1579
+ const candidateName = this.getName(unionVariant);
1580
+ if (candidateName) {
1581
+ name = candidateName;
1582
+ }
1583
+ doc = this.getDoc(unionVariant);
1584
+ }
1585
+ choices.push(new ChoiceValue(name, doc, it.value));
1586
+ });
1543
1587
  const namespace = getNamespace(type);
1544
1588
  const schemaType = sealed ? SealedChoiceSchema : ChoiceSchema;
1545
1589
  const schema = new schemaType(name, this.getDoc(type), {
@@ -1591,7 +1635,7 @@ export class CodeModelBuilder {
1591
1635
  }));
1592
1636
  }
1593
1637
  processObjectSchema(type, name) {
1594
- var _a, _b;
1638
+ var _a, _b, _c;
1595
1639
  const namespace = getNamespace(type);
1596
1640
  if ((this.isArm() &&
1597
1641
  (namespace === null || namespace === void 0 ? void 0 : namespace.startsWith("Azure.ResourceManager")) &&
@@ -1697,20 +1741,27 @@ export class CodeModelBuilder {
1697
1741
  const parentWithDiscriminator = objectSchema.parents.all.find((it) => it instanceof ObjectSchema && it.discriminator);
1698
1742
  if (parentWithDiscriminator) {
1699
1743
  discriminatorPropertyName = parentWithDiscriminator.discriminator.propertyName;
1700
- const discriminatorProperty = Array.from(type.properties.values()).find((it) => it.name === discriminatorPropertyName && (it.type.kind === "String" || it.type.kind === "EnumMember"));
1744
+ const discriminatorProperty = Array.from(type.properties.values()).find((it) => it.name === discriminatorPropertyName &&
1745
+ (it.type.kind === "String" || it.type.kind === "EnumMember" || it.type.kind === "UnionVariant"));
1701
1746
  if (discriminatorProperty) {
1702
1747
  if (discriminatorProperty.type.kind === "String") {
1703
- // value of StringLiteral of the discriminator property
1748
+ // value as StringLiteral
1704
1749
  objectSchema.discriminatorValue = discriminatorProperty.type.value;
1705
1750
  }
1706
1751
  else if (discriminatorProperty.type.kind === "EnumMember") {
1707
- // value of EnumMember of the discriminator property
1708
- // lint requires value be string, not number
1752
+ // value as EnumMember
1753
+ // lint requires value be string
1709
1754
  objectSchema.discriminatorValue =
1710
1755
  (_a = discriminatorProperty.type.value) !== null && _a !== void 0 ? _a : discriminatorProperty.type.name;
1711
1756
  }
1757
+ else if (discriminatorProperty.type.kind === "UnionVariant") {
1758
+ // value as UnionVariant
1759
+ objectSchema.discriminatorValue =
1760
+ (_b = discriminatorProperty.type.type.value) !== null && _b !== void 0 ? _b : discriminatorProperty.type.name;
1761
+ }
1712
1762
  }
1713
1763
  else {
1764
+ // it is possible that the property is Union, e.g. 'kind: "type1" | "type2"'; but such Type appears not to be a concrete model.
1714
1765
  // fallback to name of the Model
1715
1766
  objectSchema.discriminatorValue = name;
1716
1767
  }
@@ -1726,7 +1777,7 @@ export class CodeModelBuilder {
1726
1777
  objectSchema.addProperty(this.processModelProperty(prop));
1727
1778
  }
1728
1779
  // process all children
1729
- (_b = type.derivedModels) === null || _b === void 0 ? void 0 : _b.filter(modelContainsDerivedModel).forEach((it) => this.processSchema(it, this.getName(it)));
1780
+ (_c = type.derivedModels) === null || _c === void 0 ? void 0 : _c.filter(modelContainsDerivedModel).forEach((it) => this.processSchema(it, this.getName(it)));
1730
1781
  return objectSchema;
1731
1782
  }
1732
1783
  getEffectiveSchemaType(type) {
@@ -1884,9 +1935,10 @@ export class CodeModelBuilder {
1884
1935
  // nullable
1885
1936
  return this.processSchema(nonNullVariants[0].type, name);
1886
1937
  }
1887
- if (isSameLiteralTypes(nonNullVariants)) {
1938
+ const unionEnum = ignoreDiagnostics(getUnionAsEnum(type));
1939
+ if (unionEnum) {
1888
1940
  // enum
1889
- return this.processChoiceSchemaForUnion(type, nonNullVariants, name);
1941
+ return this.processChoiceSchemaForUnion(type, unionEnum, name);
1890
1942
  }
1891
1943
  // TODO: name from typespec-client-generator-core
1892
1944
  const namespace = getNamespace(type);
@@ -1929,6 +1981,7 @@ export class CodeModelBuilder {
1929
1981
  }));
1930
1982
  }
1931
1983
  getUnionVariantName(type, option) {
1984
+ var _a;
1932
1985
  switch (type.kind) {
1933
1986
  case "Scalar": {
1934
1987
  const scalarName = type.name;
@@ -1977,6 +2030,8 @@ export class CodeModelBuilder {
1977
2030
  return pascalCase(type.valueAsString);
1978
2031
  case "Boolean":
1979
2032
  return pascalCase(type.value ? "True" : "False");
2033
+ case "Union":
2034
+ return (_a = type.name) !== null && _a !== void 0 ? _a : "Union";
1980
2035
  default:
1981
2036
  throw new Error(`Unrecognized type for union variable: '${type.kind}'.`);
1982
2037
  }
@@ -2036,17 +2091,14 @@ export class CodeModelBuilder {
2036
2091
  this.logWarning(`Rename anonymous TypeSpec ${target.kind} to '${newName}'`);
2037
2092
  return newName;
2038
2093
  }
2094
+ if (typeof target.name === "symbol") {
2095
+ return "";
2096
+ }
2039
2097
  return target.name || "";
2040
2098
  }
2041
2099
  getSerializedName(target) {
2042
- // First get projected name, if not found, call resolveEncodedName
2043
- // TODO: deprecate getProjectedName
2044
- const jsonProjectedName = getProjectedName(this.program, target, "json");
2045
- if (jsonProjectedName) {
2046
- return jsonProjectedName;
2047
- }
2048
- // TODO: the MIME better be from SchemaUsage.serializationFormats. However, at this step, the values is not propagated to inner models.
2049
- return resolveEncodedName(this.program, target, "application/json");
2100
+ // TODO: currently this is only for JSON
2101
+ return getWireName(this.sdkContext, target);
2050
2102
  }
2051
2103
  isReadOnly(target) {
2052
2104
  // const key = isKey(this.program, target);