Skip to content

Commit

Permalink
fix enum default value
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh committed Dec 26, 2023
1 parent 3597db5 commit 5e11c5e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypespecParameter } from "../interfaces";
import { generateDecorators } from "../utils/decorators";
import { generateDocs } from "../utils/docs";
import { transformValue } from "../utils/values";
import { transformDefaultValue } from "../utils/values";

export function generateParameter(parameter: TypespecParameter): string {
const definitions: string[] = [];
Expand All @@ -12,7 +12,7 @@ export function generateParameter(parameter: TypespecParameter): string {
decorators && definitions.push(decorators);
let defaultValue = "";
if (parameter.defaultValue) {
defaultValue = ` = ${transformValue(parameter.defaultValue)}`;
defaultValue = ` = ${transformDefaultValue(parameter.type, parameter.defaultValue)}`;
}
definitions.push(`"${parameter.name}"${parameter.isOptional ? "?" : ""}: ${parameter.type}${defaultValue}`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
isSealedChoiceSchema,
isStringSchema,
} from "../utils/schemas";
import { transformValue } from "../utils/values";
import { getDefaultValue, transformValue } from "../utils/values";

const typespecTypes = new Map<SchemaType, string>([
[SchemaType.Date, "plainDate"],
Expand Down Expand Up @@ -122,7 +122,7 @@ export function transformObjectProperty(propertySchema: Property, codeModel: Cod
isOptional: propertySchema.required !== true,
type: visited.name,
decorators: getPropertyDecorators(propertySchema),
defaultValue: propertySchema.schema.defaultValue,
defaultValue: getDefaultValue(propertySchema.schema),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getPropertyDecorators } from "../utils/decorators";
import { getLogger } from "../utils/logger";
import { getLanguageMetadata } from "../utils/metadata";
import { isConstantSchema } from "../utils/schemas";
import { getDefaultValue } from "../utils/values";

export function transformOperationGroup(
{ language, operations }: OperationGroup,
Expand Down Expand Up @@ -176,7 +177,7 @@ export function transformParameter(parameter: Parameter, codeModel: CodeModel):
location: transformParameterLocation(parameter),
decorators: getPropertyDecorators(parameter),
serializedName: parameter.language.default.serializedName ?? parameter.language.default.name,
defaultValue: parameter.schema.defaultValue,
defaultValue: getDefaultValue(parameter.schema),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypespecObjectProperty } from "../interfaces";
import { generateDecorators } from "./decorators";
import { generateDocs } from "./docs";
import { transformValue } from "./values";
import { transformDefaultValue } from "./values";

export function getModelPropertiesDeclarations(properties: TypespecObjectProperty[]): string[] {
const definitions: string[] = [];
Expand All @@ -13,7 +13,7 @@ export function getModelPropertiesDeclarations(properties: TypespecObjectPropert
property.fixMe && property.fixMe.length && definitions.push(property.fixMe.join("\n"));
let defaultValue = "";
if (property.defaultValue) {
defaultValue = ` = ${transformValue(property.defaultValue)}`;
defaultValue = ` = ${transformDefaultValue(property.type, property.defaultValue)}`;
}
definitions.push(`"${property.name}"${getOptionalOperator(property)}: ${property.type}${defaultValue};`);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/extensions/openapi-to-typespec/src/utils/values.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import { Schema } from "@autorest/codemodel";
import { isChoiceSchema } from "./schemas";

export function transformValue(value: string | number | boolean) {
if (typeof value === "string") {
return `"${value}"`;
}

return value;
}

export function transformDefaultValue(type: string, value: string | number | boolean) {
if (type === "string" || type === "int32" || type === "boolean") {
return transformValue(value);
} else {
return `${type}.${value}`;
}
}

export function getDefaultValue(schema: Schema) {
if (schema.defaultValue === undefined) {
return undefined;
}
if (isChoiceSchema(schema)) {
for (const choice of schema.choices) {
if (schema.defaultValue === choice.value.toString()) {
return choice.language.default.name;
}
}
} else {
return schema.defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ApiOperationPolicy {

@doc("Policy Export Format.")
@query("format")
format?: PolicyExportFormat = "xml";
format?: PolicyExportFormat = PolicyExportFormat.xml;
}
>;
@doc("Gets the entity state (Etag) version of the API operation policy specified by its identifier.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ interface ApiPolicyOperations {

@doc("Policy Export Format.")
@query("format")
format?: PolicyExportFormat = "xml";
format?: PolicyExportFormat = PolicyExportFormat.xml;

@doc("Subscription credentials which uniquely identify Microsoft Azure subscription. The subscription ID forms part of the URI for every service call.")
@path
Expand Down Expand Up @@ -1175,7 +1175,7 @@ interface PolicyOperations {

@doc("Policy Export Format.")
@query("format")
format?: PolicyExportFormat = "xml";
format?: PolicyExportFormat = PolicyExportFormat.xml;

@doc("Subscription credentials which uniquely identify Microsoft Azure subscription. The subscription ID forms part of the URI for every service call.")
@path
Expand Down Expand Up @@ -1342,7 +1342,7 @@ interface ProductPolicyOperations {

@doc("Policy Export Format.")
@query("format")
format?: PolicyExportFormat = "xml";
format?: PolicyExportFormat = PolicyExportFormat.xml;

@doc("Subscription credentials which uniquely identify Microsoft Azure subscription. The subscription ID forms part of the URI for every service call.")
@path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ interface QuestionAnsweringProjectsOperations {

@doc("Knowledge base Import or Export format.")
@query("format")
format?: Format = "json";
format?: Format = Format.json;

@doc("Kind of the asset of the project.")
@query("assetKind")
Expand Down Expand Up @@ -147,7 +147,7 @@ metadata, and assets.

@doc("Knowledge base Import or Export format.")
@query("format")
format?: Format = "json";
format?: Format = Format.json;

@doc("Kind of the asset of the project.")
@query("assetKind")
Expand Down

0 comments on commit 5e11c5e

Please sign in to comment.