22 * Copyright (c) Microsoft Corporation. All rights reserved.
33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
5- import { CodeModel , DictionarySchema , getAllProperties , HttpHeader , ObjectSchema , Property , Schema , SchemaType } from '@azure-tools/codemodel' ;
5+ import { ArraySchema , CodeModel , DictionarySchema , getAllProperties , HttpHeader , ObjectSchema , Property , Schema , SchemaType } from '@azure-tools/codemodel' ;
66import { serialize } from '@azure-tools/codegen' ;
77import { PwshModel } from '../utils/PwshModel' ;
88import { NewModelState } from '../utils/model-state' ;
@@ -19,7 +19,7 @@ async function tweakModel(state: State): Promise<PwshModel> {
1919
2020 addDictionaryApiVersion ( model ) ;
2121
22- removeDictionaryDefaultDescription ( model ) ;
22+ removeM4DefaultDescription ( model ) ;
2323
2424 return model ;
2525}
@@ -80,62 +80,73 @@ function addResponseHeaderSchema(model: CodeModel) {
8080}
8181
8282function addDictionaryApiVersion ( model : CodeModel ) {
83- // If object has dictionary property, this property's schema does not have api version information in m4. We should add this back.
83+
84+ model . schemas . dictionaries ?. forEach ( ( schema ) => {
85+ if ( schema . apiVersions ) {
86+ return ;
87+ }
88+ if ( schema . elementType && schema . elementType . apiVersions ) {
89+ schema . apiVersions = JSON . parse ( JSON . stringify ( schema . elementType . apiVersions ) ) ;
90+ }
91+ } )
92+
93+ // If we cannot find api version from element type, try to get it from object schema who refers the dict or any.
8494
8595 model . schemas . objects ?. forEach ( ( schema ) => {
8696 if ( ! schema . apiVersions ) {
8797 return ;
8898 }
8999
90100 for ( const prop of getAllProperties ( schema ) ) {
91- if ( ( prop . schema . type !== SchemaType . Dictionary && prop . schema . type !== SchemaType . Any ) || prop . schema . apiVersions ) {
101+ if ( prop . schema . type !== SchemaType . Dictionary || prop . schema . apiVersions ) {
92102 continue ;
93103 }
94- const dictSchema = prop . schema as DictionarySchema ;
95- if ( dictSchema . elementType && dictSchema . elementType . apiVersions ) {
96- dictSchema . apiVersions = JSON . parse ( JSON . stringify ( dictSchema . elementType . apiVersions ) ) ;
97- } else {
98- dictSchema . apiVersions = JSON . parse ( JSON . stringify ( schema . apiVersions ) ) ;
99- }
104+ prop . schema . apiVersions = JSON . parse ( JSON . stringify ( schema . apiVersions ) ) ;
100105 }
101106 } )
102107}
103108
104- function removeDictionaryDefaultDescription ( model : CodeModel ) {
105- // For dictionary schema and property, if there is no description assigned, m4 will set a default description like: Dictionary of <type>
109+ function removeM4DefaultDescription ( model : CodeModel ) {
110+ // For dictionary and arrya schema and property, if there is no description assigned, m4 will set a default description like: Dictionary of <type> or Array of <type>
106111 // To keep same action as m3, we will set it to empty string
107112
108113 const visited = new Set < Schema > ( ) ;
109- [ ...model . schemas . objects ?? [ ] , ...model . schemas . dictionaries ?? [ ] ] . forEach ( ( schema ) => {
110- recursiveRemoveDefaultDescription ( schema , visited ) ;
114+ [ ...model . schemas . objects ?? [ ] , ...model . schemas . dictionaries ?? [ ] , ... model . schemas . arrays ?? [ ] ] . forEach ( ( schema ) => {
115+ recursiveRemoveM4DefaultDescription ( schema , visited ) ;
111116 } )
112117}
113118
114- function recursiveRemoveDefaultDescription ( schema : Schema , visited : Set < Schema > ) {
115- if ( visited . has ( schema ) || ( schema . type !== SchemaType . Object && schema . type !== SchemaType . Dictionary ) ) {
119+ function recursiveRemoveM4DefaultDescription ( schema : Schema , visited : Set < Schema > ) {
120+ if ( visited . has ( schema ) || ( schema . type !== SchemaType . Object && schema . type !== SchemaType . Dictionary && schema . type !== SchemaType . Array ) ) {
116121 return ;
117122 }
118123 // Default description pattern in m4
119- const defaultDescPattern = / D i c t i o n a r y o f < .* > $ / ;
124+ const defaultDictDescPattern = / D i c t i o n a r y o f < .? > $ / ;
125+ const defaultArrayDescPattern = / A r r a y o f .? $ / ;
120126 visited . add ( schema ) ;
121127 if ( schema . type === SchemaType . Dictionary ) {
122128 const dictSchema = schema as DictionarySchema ;
123- recursiveRemoveDefaultDescription ( dictSchema . elementType , visited ) ;
124- if ( defaultDescPattern . test ( dictSchema . language . default . description ) ) {
129+ recursiveRemoveM4DefaultDescription ( dictSchema . elementType , visited ) ;
130+ if ( defaultDictDescPattern . test ( dictSchema . language . default . description ) ) {
125131 dictSchema . language . default . description = '' ;
126132 }
133+ } else if ( schema . type === SchemaType . Array ) {
134+ const arrSchema = schema as ArraySchema ;
135+ recursiveRemoveM4DefaultDescription ( arrSchema . elementType , visited ) ;
136+ if ( defaultArrayDescPattern . test ( schema . language . default . description ) ) {
137+ schema . language . default . description = '' ;
138+ }
127139 } else if ( schema . type === SchemaType . Object ) {
128140 const objSchema = schema as ObjectSchema ;
129141 for ( const prop of getAllProperties ( objSchema ) ) {
130- recursiveRemoveDefaultDescription ( prop . schema , visited ) ;
131- if ( prop . schema . type === SchemaType . Dictionary && defaultDescPattern . test ( prop . language . default . description ) ) {
142+ recursiveRemoveM4DefaultDescription ( prop . schema , visited ) ;
143+ if ( prop . schema . type === SchemaType . Dictionary && ( defaultDictDescPattern . test ( prop . language . default . description ) || defaultArrayDescPattern . test ( prop . language . default . description ) ) ) {
132144 prop . language . default . description = '' ;
133145 }
134146 }
135147 }
136148}
137149
138-
139150export async function tweakM4ModelPlugin ( service : Host ) {
140151 const state = await new NewModelState < PwshModel > ( service ) . init ( ) ;
141152 service . WriteFile ( 'code-model-v4-tweakm4codemodel.yaml' , serialize ( await tweakModel ( state ) ) , undefined , 'code-model-v4' ) ;
0 commit comments