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

Default values supporting scalar and object #4423

Merged
merged 15 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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,7 @@
---
changeKind: feature
packages:
- "@typespec/openapi3"
---

Added support to use Scalar and Object as default types
6 changes: 5 additions & 1 deletion packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,14 @@ export function getDefaultValue(program: Program, defaultType: Value): any {
return null;
case "EnumValue":
return defaultType.value.value ?? defaultType.value.name;
case "ScalarValue":
return serializeValueAsJson(program, defaultType, defaultType.type);
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
case "ObjectValue":
return serializeValueAsJson(program, defaultType, defaultType.type);
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
default:
reportDiagnostic(program, {
code: "invalid-default",
format: { type: defaultType.valueKind },
format: { type: defaultType },
target: defaultType,
});
}
Expand Down
39 changes: 38 additions & 1 deletion packages/openapi3/test/primitive-types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepStrictEqual, ok, strictEqual } from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { OpenAPI3Schema } from "../src/types.js";
import { oapiForModel, openApiFor } from "./test-host.js";

Expand Down Expand Up @@ -115,6 +115,43 @@ describe("openapi3: primitives", () => {
});
});

it("scalar used as a default value", async () => {
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
const res = await oapiForModel(
"Pet",
`
scalar shortName { init name(value: string);}

model Pet { name: shortName = shortName.name("Shorty"); }
`
);

expect(res.schemas.Pet.properties.name.default).toEqual("Shorty");
});

it("known scalar used as a default value", async () => {
const res = await oapiForModel(
"Test",
`
model Test { minDate: utcDateTime = utcDateTime.fromISO("2024-01-2T3:04:05Z"); }
`
);

expect(res.schemas.Test.properties.minDate.default).toEqual("2024-01-2T3:04:05Z");
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
expect(res.schemas.Test.properties.minDate.format).toEqual("date-time");
expect(res.schemas.Test.properties.minDate.type).toEqual("string");
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
});

it("object value used as a default value", async () => {
const res = await oapiForModel(
"Test",
`
model Test { Pet: {name: string;} = #{ name: "Dog"}; }
`
);

expect(res.schemas.Test.properties.Pet.default.name).toEqual("Dog");
});

it("merge the data from parent", async () => {
const res = await oapiForModel(
"Pet",
Expand Down
Loading