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 #1547

Merged
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,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-autorest"
---

Added support to use Scalar and Object as default types
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 () => {
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
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");
});
});
Loading