Skip to content

Commit

Permalink
Fix: Inline enums properties with default values (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Feb 6, 2024
1 parent d5e2b8a commit 2f6bbc4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
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

0 comments on commit 2f6bbc4

Please sign in to comment.