Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for GET with projections #819

Merged
merged 6 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/compiler",
"comment": "Run projections on types returned from getEffectiveType",
"type": "patch"
}
],
"packageName": "@cadl-lang/compiler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/openapi3",
"comment": "Run projections on types returned from getEffectiveType",
"type": "patch"
}
],
"packageName": "@cadl-lang/openapi3"
}
17 changes: 15 additions & 2 deletions packages/compiler/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4110,6 +4110,19 @@ export function createChecker(program: Program): Checker {
return false;
}

function getProjectedEffectiveModelType(type: ModelType): ModelType {
if (!program.currentProjector) {
return type;
}

const projectedType = program.currentProjector.projectType(type);
if (projectedType.kind !== "Model") {
compilerAssert(false, "Fail");
}

return projectedType;
}

function getEffectiveModelType(
model: ModelType,
filter?: (property: ModelTypeProperty) => boolean
Expand All @@ -4120,7 +4133,7 @@ export function createChecker(program: Program): Checker {

if (model.name) {
// named model
return model;
return getProjectedEffectiveModelType(model);
}

// We would need to change the algorithm if this doesn't hold. We
Expand Down Expand Up @@ -4188,7 +4201,7 @@ export function createChecker(program: Program): Checker {
}
}

return match ?? model;
return match ? getProjectedEffectiveModelType(match) : model;
}

function filterModelProperties(
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export function $withOptionalProperties(context: DecoratorContext, target: Type)
target.properties.forEach((p) => (p.optional = true));
}

// -- @withUpdatableProperties decorator ----------------------
// -- @withUpdateableProperties decorator ----------------------

export function $withUpdateableProperties(context: DecoratorContext, target: Type) {
if (!validateDecoratorTarget(context, target, "@withUpdateableProperties", "Model")) {
Expand Down
22 changes: 15 additions & 7 deletions packages/openapi3/test/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ export async function createOpenAPITestHost() {
});
}

export async function createOpenAPITestRunner() {
export async function createOpenAPITestRunner({
withVersioning,
}: { withVersioning?: boolean } = {}) {
const host = await createOpenAPITestHost();
return createTestWrapper(
host,
(code) =>
`import "@cadl-lang/rest"; import "@cadl-lang/openapi"; import "@cadl-lang/openapi3"; using Cadl.Rest; using Cadl.Http; using OpenAPI; ${code}`,
{ emitters: { "@cadl-lang/openapi3": {} } }
);
const importAndUsings = `
import "@cadl-lang/rest"; import "@cadl-lang/openapi";
import "@cadl-lang/openapi3";
${withVersioning ? `import "@cadl-lang/versioning"` : ""};
using Cadl.Rest;
using Cadl.Http;
using OpenAPI;
${withVersioning ? "using Cadl.Versioning;" : ""}
`;
return createTestWrapper(host, (code) => `${importAndUsings} ${code}`, {
emitters: { "@cadl-lang/openapi3": {} },
});
}

function versionedOutput(path: string, version: string) {
Expand Down
35 changes: 30 additions & 5 deletions packages/openapi3/test/versioning.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DecoratorContext, NamespaceType } from "@cadl-lang/compiler";
import { createTestWrapper } from "@cadl-lang/compiler/testing";
import { deepStrictEqual, strictEqual } from "assert";
import { createOpenAPITestHost, openApiFor } from "./test-host.js";
import { createOpenAPITestHost, createOpenAPITestRunner, openApiFor } from "./test-host.js";

describe("openapi3: versioning", () => {
it("works with models", async () => {
Expand Down Expand Up @@ -118,19 +118,16 @@ describe("openapi3: versioning", () => {
`import "@cadl-lang/rest"; import "@cadl-lang/openapi";
import "@cadl-lang/openapi3"; import "@cadl-lang/versioning";
import "./test.js";

using Cadl.Rest; using Cadl.Http; using OpenAPI; using Cadl.Versioning; ${code}`,
{ emitters: { "@cadl-lang/openapi3": {} } }
);

await runner.compile(`

@versioned(Contoso.Library.Versions)
namespace Contoso.Library {
namespace Blah { }
enum Versions { v1 };
}

@armNamespace
@serviceTitle("Widgets 'r' Us")
@versionedDependency(Contoso.Library.Versions.v1)
Expand All @@ -140,7 +137,6 @@ describe("openapi3: versioning", () => {
@segment("widgets")
id: string;
}

interface Operations {
@test
op get(id: string): Widget;
Expand All @@ -150,4 +146,33 @@ describe("openapi3: versioning", () => {

strictEqual(storedNamespace, "Contoso.WidgetService");
});

// Test for https://github.com/microsoft/cadl/issues/812
it("doesn't throw errors when using UpdateableProperties", async () => {
// if this test throws a duplicate name diagnostic, check that getEffectiveType
// is returning the projected type.
const runner = await createOpenAPITestRunner({ withVersioning: true });
await runner.compile(`
@versioned(Library.Versions)
namespace Library {
enum Versions {
v1,
v2,
}
}

@serviceTitle("Service")
@versionedDependency(Library.Versions.v1)
namespace Service {
model Widget {
details?: WidgetDetails;
}

model WidgetDetails {}
interface Projects {
oops(...UpdateableProperties<Widget>): Widget;
}
}
`);
});
});