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: Inline enums properties with default values #152

Merged
merged 14 commits into from
Feb 6, 2024
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
5 changes: 5 additions & 0 deletions .changeset/dry-onions-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@azure-tools/typespec-autorest": patch
---

Fix: Inline enums properties with default values as `default` is not allowed next to `$ref`
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@
}
},
"optionalEnum": {
"$ref": "#/definitions/MyEnum",
"default": "a"
"type": "string",
"default": "a",
"enum": [
"a",
"b"
],
"x-ms-enum": {
"name": "MyEnum",
"modelAsString": true
}
}
}
},
Expand Down
7 changes: 5 additions & 2 deletions packages/typespec-autorest/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ function createOAPIEmitter(
property.description = description;
}

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

Expand Down Expand Up @@ -1793,7 +1793,10 @@ function createOAPIEmitter(
}

function resolveProperty(prop: ModelProperty, visibility: Visibility): OpenAPI2SchemaProperty {
const propSchema = getSchemaOrRef(prop.type, visibility);
const propSchema =
prop.type.kind === "Enum" && prop.default
? getSchemaForEnum(prop.type)
: getSchemaOrRef(prop.type, visibility);
return applyIntrinsicDecorators(prop, propSchema);
}

Expand Down
17 changes: 11 additions & 6 deletions packages/typespec-autorest/test/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe("typespec-autorest: model definitions", () => {
});
});

it("specify default value on enum property", async () => {
it("specify default value on enum property inline the enum", async () => {
const res = await oapiForModel(
"Foo",
`
Expand All @@ -287,15 +287,20 @@ describe("typespec-autorest: model definitions", () => {
}
`
);

ok(res.isRef);
ok(res.defs.Foo, "expected definition named Foo");
ok(res.defs.MyEnum, "expected definition named MyEnum");
deepStrictEqual(res.defs.Foo, {
type: "object",
properties: {
optionalEnum: {
$ref: "#/definitions/MyEnum",
type: "string",
enum: ["a-value", "b"],
"x-ms-enum": {
name: "MyEnum",
modelAsString: true,
values: [
{ name: "a", value: "a-value" },
{ name: "b", value: "b" },
],
},
default: "a-value",
},
},
Expand Down
29 changes: 0 additions & 29 deletions packages/typespec-autorest/test/openapi-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,6 @@ describe("typespec-autorest: definitions", () => {
});
});

it("specify default value on enum property", async () => {
const res = await oapiForModel(
"Foo",
`
model Foo {
optionalEnum?: MyEnum = MyEnum.a;
};

enum MyEnum {
a: "a-value",
b,
}
`
);

ok(res.isRef);
ok(res.defs.Foo, "expected definition named Foo");
ok(res.defs.MyEnum, "expected definition named MyEnum");
deepStrictEqual(res.defs.Foo, {
type: "object",
properties: {
optionalEnum: {
$ref: "#/definitions/MyEnum",
default: "a-value",
},
},
});
});

it("ignore uninstantiated template types", async () => {
const res = await openApiFor(
`
Expand Down
Loading