1
1
using System . Collections . Generic ;
2
2
using System . Threading . Tasks ;
3
- using Newtonsoft . Json ;
4
3
using System ;
5
4
using Microsoft . Azure . Management . ApiManagement . ArmTemplates . Common ;
6
5
@@ -23,7 +22,7 @@ public APITemplateCreator(FileReader fileReader, PolicyTemplateCreator policyTem
23
22
this . releaseTemplateCreator = releaseTemplateCreator ;
24
23
}
25
24
26
- public List < Template > CreateAPITemplates ( APIConfig api )
25
+ public async Task < List < Template > > CreateAPITemplatesAsync ( APIConfig api )
27
26
{
28
27
// determine if api needs to be split into multiple templates
29
28
bool isSplit = isSplitAPI ( api ) ;
@@ -43,18 +42,18 @@ public List<Template> CreateAPITemplates(APIConfig api)
43
42
if ( isSplit == true )
44
43
{
45
44
// create 2 templates, an initial template with metadata and a subsequent template with the swagger content
46
- apiTemplates . Add ( CreateAPITemplate ( api , isSplit , true ) ) ;
47
- apiTemplates . Add ( CreateAPITemplate ( api , isSplit , false ) ) ;
45
+ apiTemplates . Add ( await CreateAPITemplateAsync ( api , isSplit , true ) ) ;
46
+ apiTemplates . Add ( await CreateAPITemplateAsync ( api , isSplit , false ) ) ;
48
47
}
49
48
else
50
49
{
51
50
// create a unified template that includes both the metadata and swagger content
52
- apiTemplates . Add ( CreateAPITemplate ( api , isSplit , false ) ) ;
51
+ apiTemplates . Add ( await CreateAPITemplateAsync ( api , isSplit , false ) ) ;
53
52
}
54
53
return apiTemplates ;
55
54
}
56
55
57
- public Template CreateAPITemplate ( APIConfig api , bool isSplit , bool isInitial )
56
+ public async Task < Template > CreateAPITemplateAsync ( APIConfig api , bool isSplit , bool isInitial )
58
57
{
59
58
// create empty template
60
59
Template apiTemplate = CreateEmptyTemplate ( ) ;
@@ -67,7 +66,7 @@ public Template CreateAPITemplate(APIConfig api, bool isSplit, bool isInitial)
67
66
68
67
List < TemplateResource > resources = new List < TemplateResource > ( ) ;
69
68
// create api resource
70
- APITemplateResource apiTemplateResource = this . CreateAPITemplateResource ( api , isSplit , isInitial ) ;
69
+ APITemplateResource apiTemplateResource = await this . CreateAPITemplateResourceAsync ( api , isSplit , isInitial ) ;
71
70
resources . Add ( apiTemplateResource ) ;
72
71
// add the api child resources (api policies, diagnostics, etc) if this is the unified or subsequent template
73
72
if ( ! isSplit || ! isInitial )
@@ -102,7 +101,7 @@ public List<TemplateResource> CreateChildResourceTemplates(APIConfig api)
102
101
return resources ;
103
102
}
104
103
105
- public APITemplateResource CreateAPITemplateResource ( APIConfig api , bool isSplit , bool isInitial )
104
+ public async Task < APITemplateResource > CreateAPITemplateResourceAsync ( APIConfig api , bool isSplit , bool isInitial )
106
105
{
107
106
// create api resource
108
107
APITemplateResource apiTemplateResource = new APITemplateResource ( )
@@ -147,15 +146,48 @@ public APITemplateResource CreateAPITemplateResource(APIConfig api, bool isSplit
147
146
}
148
147
if ( ! isSplit || ! isInitial )
149
148
{
150
- // add swagger properties for subsequent and unified templates
149
+ // add open api spec properties for subsequent and unified templates
150
+ string format ;
151
+ string value ;
152
+
153
+ // determine if the open api spec is remote or local, yaml or json
151
154
Uri uriResult ;
155
+ string fileContents = await this . fileReader . RetrieveFileContentsAsync ( api . openApiSpec ) ;
156
+ bool isJSON = this . fileReader . isJSON ( fileContents ) ;
152
157
bool isUrl = Uri . TryCreate ( api . openApiSpec , UriKind . Absolute , out uriResult ) && ( uriResult . Scheme == Uri . UriSchemeHttp || uriResult . Scheme == Uri . UriSchemeHttps ) ;
153
- // used to escape sequences in local swagger json
154
- object deserializedFileContents = isUrl ? null : JsonConvert . DeserializeObject < object > ( this . fileReader . RetrieveLocalFileContents ( api . openApiSpec ) ) ;
155
- // if openApiSpec is a url inline the url, if it is a local file inline the file contents
156
- apiTemplateResource . properties . format = isUrl ? "swagger-link-json" : "swagger-json" ;
157
- apiTemplateResource . properties . value = isUrl ? api . openApiSpec : JsonConvert . SerializeObject ( deserializedFileContents ) ;
158
- // supplied via optional arguments
158
+
159
+ if ( isUrl == true )
160
+ {
161
+ value = api . openApiSpec ;
162
+ if ( isJSON == true )
163
+ {
164
+ // open api spec is remote json file, use swagger-link-json for v2 and openapi-link for v3
165
+ OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader ( ) ;
166
+ bool isVersionThree = await openAPISpecReader . isJSONOpenAPISpecVersionThreeAsync ( api . openApiSpec ) ;
167
+ format = isVersionThree == false ? "swagger-link-json" : "openapi-link" ;
168
+ }
169
+ else
170
+ {
171
+ // open api spec is remote yaml file
172
+ format = "openapi-link" ;
173
+ }
174
+ } else
175
+ {
176
+ value = fileContents ;
177
+ if ( isJSON == true )
178
+ {
179
+ // open api spec is local json file, use swagger-json for v2 and openapi+json for v3
180
+ OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader ( ) ;
181
+ bool isVersionThree = await openAPISpecReader . isJSONOpenAPISpecVersionThreeAsync ( api . openApiSpec ) ;
182
+ format = isVersionThree == false ? "swagger-json" : "openapi+json" ;
183
+ } else
184
+ {
185
+ // open api spec is local yaml file
186
+ format = "openapi" ;
187
+ }
188
+ }
189
+ apiTemplateResource . properties . format = format ;
190
+ apiTemplateResource . properties . value = value ;
159
191
apiTemplateResource . properties . path = api . suffix ;
160
192
}
161
193
return apiTemplateResource ;
0 commit comments