@@ -55,6 +55,7 @@ import {
5555 isVoidType ,
5656 resolveEncodedName ,
5757 IntrinsicType ,
58+ serializeValueAsJson ,
5859} from "@typespec/compiler" ;
5960import { TwoLevelMap } from "@typespec/compiler/utils" ;
6061import {
@@ -699,7 +700,7 @@ function convert2CMDSchema(
699700
700701 if ( param . defaultValue ) {
701702 schema . default = {
702- value : getDefaultValue ( context , param . defaultValue ) ,
703+ value : getDefaultValue ( context , param . defaultValue , param ) ,
703704 } ;
704705 }
705706 }
@@ -708,6 +709,10 @@ function convert2CMDSchema(
708709 ...schema ,
709710 ...applySchemaFormat ( context , param , schema as CMDSchemaBase ) ,
710711 } ;
712+ schema = {
713+ ...schema ,
714+ ...applyEncoding ( context , param , schema ) ,
715+ } ;
711716 schema = {
712717 ...schema ,
713718 ...applyExtensionsDecorators ( context , param , schema ) ,
@@ -759,6 +764,7 @@ function convert2CMDSchemaBase(context: AAZSchemaEmitterContext, type: Type): CM
759764 }
760765 if ( schema ) {
761766 schema = applySchemaFormat ( context , type , schema ) ;
767+ schema = applyEncoding ( context , type , schema ) ;
762768 schema = applyExtensionsDecorators ( context , type , schema ) ;
763769 }
764770
@@ -1898,6 +1904,20 @@ function emitArrayFormat(
18981904
18991905// TODO: add emitResourceIdFormat
19001906
1907+ function applyEncoding ( context : AAZSchemaEmitterContext , type : Type , schema : CMDSchemaBase ) : CMDSchemaBase {
1908+ if ( type . kind !== "Scalar" && type . kind !== "ModelProperty" ) {
1909+ return schema ;
1910+ }
1911+ const encodeData = getEncode ( context . program , type ) ;
1912+ if ( encodeData !== undefined ) {
1913+ schema = {
1914+ ...schema ,
1915+ ...convertScalar2CMDSchemaBase ( context , encodeData . type ) ,
1916+ } ;
1917+ }
1918+ return schema ;
1919+ }
1920+
19011921// apply extension decorators
19021922function applyExtensionsDecorators ( context : AAZSchemaEmitterContext , type : Type , schema : CMDSchemaBase ) : CMDSchemaBase {
19031923 const extensions = getExtensions ( context . program , type ) ;
@@ -2124,25 +2144,6 @@ function getClsDefinitionModel(schema: CMDClsSchemaBase): CMDObjectSchemaBase |
21242144 return schema . type . pendingSchema . schema ! ;
21252145}
21262146
2127- function getDefaultValue ( content : AAZSchemaEmitterContext , defaultType : Value ) : unknown {
2128- switch ( defaultType . valueKind ) {
2129- case "StringValue" :
2130- return defaultType . value ;
2131- case "NumericValue" :
2132- return defaultType . value . asNumber ( ) ?? undefined ;
2133- case "BooleanValue" :
2134- return defaultType . value ;
2135- case "ArrayValue" :
2136- return defaultType . values . map ( ( x ) => getDefaultValue ( content , x ) ) ;
2137- case "NullValue" :
2138- return null ;
2139- case "EnumValue" :
2140- return defaultType . value . value ?? defaultType . value . name ;
2141- default :
2142- reportDiagnostic ( content . program , {
2143- code : "invalid-default" ,
2144- format : { type : defaultType . valueKind } ,
2145- target : defaultType ,
2146- } ) ;
2147- }
2147+ function getDefaultValue ( context : AAZSchemaEmitterContext , defaultType : Value , modelProperty : ModelProperty ) : any {
2148+ return serializeValueAsJson ( context . program , defaultType , modelProperty ) ;
21482149}
0 commit comments