From 9b68344963f8669f31421f26fe9828a1b2a15c63 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Mon, 24 Oct 2022 14:52:14 -0700 Subject: [PATCH] Deserialize enum responses. --- powershell/llcsharp/schema/enum.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/powershell/llcsharp/schema/enum.ts b/powershell/llcsharp/schema/enum.ts index 798329dbbe..0410b51248 100644 --- a/powershell/llcsharp/schema/enum.ts +++ b/powershell/llcsharp/schema/enum.ts @@ -6,7 +6,8 @@ import { Schema } from '../code-model'; import { Schema as NewSchema } from '@azure-tools/codemodel'; import { String } from './string'; -import { dotnet, toExpression } from '@azure-tools/codegen-csharp'; +import { dotnet, Expression, ExpressionOrLiteral, toExpression } from '@azure-tools/codegen-csharp'; +import { KnownMediaType } from '@azure-tools/codemodel-v3'; export class EnumImplementation extends String { public isXmlAttribute = false; @@ -29,4 +30,13 @@ export class EnumImplementation extends String { get declaration(): string { return `${this.schema.language.csharp?.namespace}.${this.schema.language.csharp?.name}${this.isRequired ? '' : '?'}`; } + + /** emits an expression to deserialize content from a content/response */ + deserializeFromResponse(mediaType: KnownMediaType, content: ExpressionOrLiteral, defaultValue: Expression): Expression | undefined { + switch (mediaType) { + case KnownMediaType.Json: + return toExpression(`${content}.Content.ReadAsStringAsync().ContinueWith( body => ${this.deserializeFromString(mediaType, 'body.Result', defaultValue)})`); + } + return toExpression(`null /* deserializeFromResponse doesn't support '${mediaType}' ${__filename}*/`); + } }