Skip to content

Commit

Permalink
Merge branch 'main' into almend/Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
AlitzelMendez committed Sep 17, 2024
2 parents cfbea38 + 2b288d0 commit 17d804c
Show file tree
Hide file tree
Showing 11 changed files with 750 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-autorest"
---

Added support to use Scalar and Object as default types
7 changes: 7 additions & 0 deletions .chronus/changes/commonTypeManagedId-2024-8-17-8-36-58.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Correct ManagedServiceIdentityType versioning issue
2 changes: 1 addition & 1 deletion core
Submodule core updated 87 files
+1 −0 .gitignore
+2 −0 eng/tsp-core/pipelines/pr-tools.yml
+1 −0 eng/tsp-core/pipelines/publish.yml
+3 −0 eng/tsp-core/scripts/create-tryit-comment.ts
+2 −0 packages/spec-api/CHANGELOG.md
+55 −0 packages/spec-api/package.json
+106 −0 packages/spec-api/src/expectation.ts
+39 −0 packages/spec-api/src/index.ts
+31 −0 packages/spec-api/src/mock-request.ts
+216 −0 packages/spec-api/src/request-validations.ts
+26 −0 packages/spec-api/src/response-utils.ts
+92 −0 packages/spec-api/src/routes.ts
+50 −0 packages/spec-api/src/scenarios.ts
+82 −0 packages/spec-api/src/types.ts
+21 −0 packages/spec-api/src/validation-error.ts
+53 −0 packages/spec-api/test/expectation.test.ts
+8 −0 packages/spec-api/tsconfig.build.json
+10 −0 packages/spec-api/tsconfig.json
+11 −0 packages/spec-api/vitest.config.ts
+1 −0 packages/spec-core/CHANGELOG.md
+3 −0 packages/spec-core/cmd/cli.mjs
+69 −0 packages/spec-core/package.json
+87 −0 packages/spec-core/src/actions/check-coverage.ts
+55 −0 packages/spec-core/src/actions/generate-scenario-summary.ts
+1 −0 packages/spec-core/src/actions/index.ts
+77 −0 packages/spec-core/src/actions/serve.ts
+41 −0 packages/spec-core/src/actions/upload-coverage-report.ts
+30 −0 packages/spec-core/src/actions/upload-scenario-manifest.ts
+75 −0 packages/spec-core/src/actions/validate-mock-apis.ts
+17 −0 packages/spec-core/src/actions/validate-scenarios.ts
+53 −0 packages/spec-core/src/app/app.ts
+16 −0 packages/spec-core/src/app/config.ts
+2 −0 packages/spec-core/src/app/index.ts
+71 −0 packages/spec-core/src/app/request-processor.ts
+293 −0 packages/spec-core/src/cli/cli.ts
+16 −0 packages/spec-core/src/config/config-schema.ts
+15 −0 packages/spec-core/src/config/config.ts
+57 −0 packages/spec-core/src/config/schema-validator.ts
+3 −0 packages/spec-core/src/config/types.ts
+3 −0 packages/spec-core/src/constants.ts
+10 −0 packages/spec-core/src/coverage/common.ts
+13 −0 packages/spec-core/src/coverage/coverage-report.ts
+123 −0 packages/spec-core/src/coverage/coverage-tracker.ts
+1 −0 packages/spec-core/src/coverage/index.ts
+43 −0 packages/spec-core/src/coverage/scenario-manifest.ts
+10 −0 packages/spec-core/src/logger.ts
+15 −0 packages/spec-core/src/routes/admin.ts
+7 −0 packages/spec-core/src/routes/index.ts
+197 −0 packages/spec-core/src/scenarios-resolver.ts
+1 −0 packages/spec-core/src/server/index.ts
+99 −0 packages/spec-core/src/server/server.ts
+46 −0 packages/spec-core/src/spec-utils/import-spec.ts
+1 −0 packages/spec-core/src/spec-utils/index.ts
+165 −0 packages/spec-core/src/spec-utils/module-resolver.ts
+17 −0 packages/spec-core/src/utils/body-utils.test.ts
+8 −0 packages/spec-core/src/utils/body-utils.ts
+49 −0 packages/spec-core/src/utils/diagnostic-reporter.ts
+36 −0 packages/spec-core/src/utils/exec.ts
+14 −0 packages/spec-core/src/utils/file-utils.ts
+5 −0 packages/spec-core/src/utils/index.ts
+54 −0 packages/spec-core/src/utils/misc-utils.ts
+3 −0 packages/spec-core/src/utils/path-utils.ts
+4 −0 packages/spec-core/src/utils/request-utils.ts
+12 −0 packages/spec-core/tsconfig.build.json
+8 −0 packages/spec-core/tsconfig.json
+1 −0 packages/spec-coverage-sdk/CHANGELOG.md
+36 −0 packages/spec-coverage-sdk/package.json
+164 −0 packages/spec-coverage-sdk/src/client.ts
+1 −0 packages/spec-coverage-sdk/src/constants.ts
+13 −0 packages/spec-coverage-sdk/src/index.ts
+68 −0 packages/spec-coverage-sdk/src/types.ts
+8 −0 packages/spec-coverage-sdk/tsconfig.build.json
+8 −0 packages/spec-coverage-sdk/tsconfig.json
+1 −0 packages/spec-lib/CHANGELOG.md
+48 −0 packages/spec-lib/generated-defs/TypeSpec.SpecLib.ts
+5 −0 packages/spec-lib/generated-defs/TypeSpec.SpecLib.ts-test.ts
+37 −0 packages/spec-lib/lib/lib.tsp
+44 −0 packages/spec-lib/package.json
+227 −0 packages/spec-lib/src/decorators.ts
+18 −0 packages/spec-lib/src/index.ts
+33 −0 packages/spec-lib/src/lib.ts
+12 −0 packages/spec-lib/src/tsp-index.ts
+55 −0 packages/spec-lib/src/validate.ts
+4 −0 packages/spec-lib/tsconfig.build.json
+4 −0 packages/spec-lib/tsconfig.config.json
+10 −0 packages/spec-lib/tsconfig.json
+694 −0 pnpm-lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"description": "User assigned managed identity."
},
{
"name": "SystemAndUserAssignedV3",
"name": "SystemAssigned,UserAssigned",
"value": "SystemAssigned,UserAssigned",
"description": "System and user assigned managed identity."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"description": "User assigned managed identity."
},
{
"name": "SystemAndUserAssignedV4",
"name": "SystemAssigned, UserAssigned",
"value": "SystemAssigned, UserAssigned",
"description": "System and user assigned managed identity."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"description": "User assigned managed identity."
},
{
"name": "SystemAndUserAssigned",
"name": "SystemAssigned,UserAssigned",
"value": "SystemAssigned,UserAssigned",
"description": "System and user assigned managed identity."
}
Expand Down
6 changes: 0 additions & 6 deletions packages/typespec-autorest/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,6 @@ export const $lib = createTypeSpecLibrary({
"Empty unions are not supported for OpenAPI v2 - enums must have at least one value.",
},
},
"invalid-default": {
severity: "error",
messages: {
default: paramMessage`Invalid type '${"type"}' for a default value`,
},
},
"invalid-multi-collection-format": {
severity: "error",
messages: {
Expand Down
34 changes: 8 additions & 26 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
reportDeprecated,
resolveEncodedName,
resolvePath,
serializeValueAsJson,
} from "@typespec/compiler";
import { TwoLevelMap } from "@typespec/compiler/utils";
import {
Expand Down Expand Up @@ -1316,7 +1317,7 @@ export async function getOpenAPIForService(
in: "formData",
...base,
...(getFormDataSchema(param.type, schemaContext, base.name) as any),
default: param.defaultValue && getDefaultValue(param.defaultValue),
default: param.defaultValue && getDefaultValue(param.defaultValue, param),
};

Object.assign(
Expand Down Expand Up @@ -1376,7 +1377,7 @@ export async function getOpenAPIForService(
collectionFormat === "csv" && schema.items === undefined // If csv
? undefined
: (collectionFormat as any),
default: param.param.defaultValue && getDefaultValue(param.param.defaultValue),
default: param.param.defaultValue && getDefaultValue(param.param.defaultValue, param.param),
...base,
...schema,
};
Expand All @@ -1390,7 +1391,7 @@ export async function getOpenAPIForService(

const result: OpenAPI2PathParameter = {
in: "path",
default: param.param.defaultValue && getDefaultValue(param.param.defaultValue),
default: param.param.defaultValue && getDefaultValue(param.param.defaultValue, param.param),
...base,
...getSimpleParameterSchema(param.param, schemaContext, base.name),
};
Expand All @@ -1415,7 +1416,7 @@ export async function getOpenAPIForService(
}
return {
in: "header",
default: param.defaultValue && getDefaultValue(param.defaultValue),
default: param.defaultValue && getDefaultValue(param.defaultValue, param),
...base,
collectionFormat: collectionFormat as any,
...getSimpleParameterSchema(param, schemaContext, base.name),
Expand Down Expand Up @@ -1801,27 +1802,8 @@ export async function getOpenAPIForService(
return getSchemaForType(variant.type, schemaContext)!;
}

function getDefaultValue(defaultType: Value): any {
switch (defaultType.valueKind) {
case "StringValue":
return defaultType.value;
case "NumericValue":
return defaultType.value.asNumber() ?? undefined;
case "BooleanValue":
return defaultType.value;
case "ArrayValue":
return defaultType.values.map((x) => getDefaultValue(x));
case "NullValue":
return null;
case "EnumValue":
return defaultType.value.value ?? defaultType.value.name;
default:
reportDiagnostic(program, {
code: "invalid-default",
format: { type: defaultType.valueKind },
target: defaultType,
});
}
function getDefaultValue(defaultType: Value, modelProperty: ModelProperty): any {
return serializeValueAsJson(program, defaultType, modelProperty);
}

function includeDerivedModel(model: Model): boolean {
Expand Down Expand Up @@ -1960,7 +1942,7 @@ export async function getOpenAPIForService(
applySummary(prop, property);

if (prop.defaultValue && !("$ref" in property)) {
property.default = getDefaultValue(prop.defaultValue);
property.default = getDefaultValue(prop.defaultValue, prop);
}

if (isReadonlyProperty(program, prop)) {
Expand Down
23 changes: 23 additions & 0 deletions packages/typespec-autorest/test/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,27 @@ describe("typespec-autorest: model definitions", () => {
});
});
});

it("encode know scalar as a default value", async () => {
const res = await oapiForModel(
"Test",
`
model Test { @encode("rfc7231") minDate: utcDateTime = utcDateTime.fromISO("2024-01-01T11:32:00Z"); }
`,
);

expect(res.defs.Test.properties.minDate.default).toEqual("Mon, 01 Jan 2024 11:32:00 GMT");
});

it("object value used as a default value", async () => {
const res = await oapiForModel(
"Test",
`
model Test { Pet: {name: string; @encode("rfc7231")birthday: utcDateTime} = #{ name: "Dog", birthday:utcDateTime.fromISO("2024-01-01T11:32:00Z")}}
`,
);

expect(res.defs.Test.properties.Pet.default.name).toEqual("Dog");
expect(res.defs.Test.properties.Pet.default.birthday).toEqual("Mon, 01 Jan 2024 11:32:00 GMT");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,24 @@ model UserAssignedIdentity {
*/
union ManagedServiceIdentityType {
/** No managed identity. */
None: "None",
"None",

/** System assigned managed identity. */
SystemAssigned: "SystemAssigned",
"SystemAssigned",

/** User assigned managed identity. */
UserAssigned: "UserAssigned",
"UserAssigned",

/** System and user assigned managed identity. */
@renamedFrom(Versions.v3, "SystemAndUserAssigned")
@added(Versions.v3)
@removed(Versions.v4)
SystemAndUserAssignedV3: "SystemAssigned,UserAssigned",
@added(Versions.v5)
"SystemAssigned,UserAssigned",

/** System and user assigned managed identity. */
@added(Versions.v4)
@renamedFrom(Versions.v4, "SystemAndUserAssigned")
@removed(Versions.v5)
SystemAndUserAssignedV4: "SystemAssigned, UserAssigned",

/** System and user assigned managed identity. */
@added(Versions.v5)
SystemAndUserAssigned: "SystemAssigned,UserAssigned",
"SystemAssigned, UserAssigned",

string,
}
Expand Down
Loading

0 comments on commit 17d804c

Please sign in to comment.