@@ -64,33 +64,218 @@ private MemoryStream CreateStream(string text)
64
64
return stream ;
65
65
}
66
66
67
- public void GenerateAssets ( OpenApiDocument openApiDocument )
67
+ public void GenerateAssets ( OpenApiDocument doc )
68
68
{
69
69
string assetsPath = EditorUtility . OpenFolderPanel ( "Select assets folder" , _lastAssetPath , "" ) ;
70
70
_lastAssetPath = assetsPath ;
71
71
assetsPath = assetsPath . Substring ( assetsPath . IndexOf ( "Assets" ) ) ;
72
- ApiAsset apiAsset = AssetsHelper . GetOrCreateScriptableObject < ApiAsset > ( assetsPath , openApiDocument . Info . Title ) ;
73
- apiAsset . UpdateWithApiDocument ( openApiDocument ) ;
72
+ ApiAsset apiAsset = AssetsHelper . GetOrCreateScriptableObject < ApiAsset > ( assetsPath , doc . Info . Title ) ;
73
+
74
+ #region ApiAsset update
74
75
apiAsset . Http = http ;
76
+ apiAsset . info = new OAInfo ( )
77
+ {
78
+ Title = doc . Info . Title ,
79
+ Description = doc . Info . Description ,
80
+ Version = doc . Info . Version ,
81
+ TermsOfService = doc . Info . TermsOfService == null ? "" : doc . Info . TermsOfService . ToString ( ) ,
82
+ Contact = CreateContact ( doc . Info . Contact ) ,
83
+ License = CreateLicence ( doc . Info . License ) ,
84
+ } ;
85
+
86
+ apiAsset . externalDocs = CreateExternalDocs ( doc . ExternalDocs ) ;
87
+
88
+ apiAsset . servers = doc . Servers . Select ( s => CreateAOServer ( s ) ) . ToList ( ) ;
89
+
75
90
76
91
apiAsset . pathItemAssets = new List < PathItemAsset > ( ) ;
77
92
78
- foreach ( var p in openApiDocument . Paths )
93
+ #endregion
94
+
95
+ foreach ( var p in doc . Paths )
79
96
{
80
97
string fileName = p . Key . Replace ( '/' , '_' ) ;
81
98
PathItemAsset a = AssetsHelper . GetOrCreateScriptableObject < PathItemAsset > ( assetsPath , fileName ) ;
82
99
a . ApiAsset = apiAsset ;
83
100
84
- a . UpdateWithPathData ( p . Key , p . Value ) ;
101
+ #region path item update
102
+
103
+
104
+ a . Path = p . Key ;
105
+
106
+ a . Summary = p . Value . Summary ;
107
+ a . Description = p . Value . Description ;
108
+ a . Parameters = p . Value . Parameters . Select ( par => CreateAOParameter ( par ) ) . ToList ( ) ;
109
+ a . Operations = p . Value . Operations . Select ( o => CreateAOOperation ( o . Key , o . Value , a ) ) . ToList ( ) ;
110
+ a . Servers = p . Value . Servers . Select ( s => CreateAOServer ( s ) ) . ToList ( ) ;
111
+
112
+ #endregion
85
113
86
114
apiAsset . pathItemAssets . Add ( a ) ;
87
115
}
88
116
89
-
117
+
90
118
91
119
AssetDatabase . SaveAssets ( ) ;
92
120
}
93
121
122
+ private OAParameter CreateAOParameter ( OpenApiParameter openApiParameter )
123
+ {
124
+ return new OAParameter ( )
125
+ {
126
+ Name = openApiParameter . Name ,
127
+ Required = openApiParameter . Required ,
128
+ AllowReserved = openApiParameter . AllowReserved ,
129
+ Explode = openApiParameter . Explode ,
130
+ AllowEmptyValue = openApiParameter . AllowEmptyValue ,
131
+ Deprecated = openApiParameter . Deprecated ,
132
+ Description = openApiParameter . Description ,
133
+ UnresolvedReference = openApiParameter . UnresolvedReference ,
134
+
135
+ In = ( OAParameterLocation ) openApiParameter . In ,
136
+
137
+ Reference = CreateReference ( openApiParameter . Reference ) ,
138
+ } ;
139
+ }
140
+
141
+ private OAOperation CreateAOOperation ( OperationType operationType , OpenApiOperation op , PathItemAsset pathItemAsset )
142
+ {
143
+ var operation = new OAOperation ( )
144
+ {
145
+ pathAsset = pathItemAsset ,
146
+ OperationId = op . OperationId ,
147
+ OperationType = ( AOOperationType ) operationType ,
148
+ Summary = op . Summary ,
149
+ Description = op . Description ,
150
+ Deprecated = op . Deprecated ,
151
+
152
+ Parameters = op . Parameters . Count > 0 ?
153
+ op . Parameters . Select ( p => CreateAOParameter ( p ) ) . ToList ( ) :
154
+ pathItemAsset . Parameters ,
155
+
156
+
157
+ Servers = op . Servers . Select ( s => CreateAOServer ( s ) ) . ToList ( ) ,
158
+
159
+ Tags = op . Tags . Select ( t => CreateOATag ( t ) ) . ToList ( ) ,
160
+
161
+ RequestBody = CreateOARequestBody ( op . RequestBody ) ,
162
+
163
+ ExternalDocs = CreateExternalDocs ( op . ExternalDocs ) ,
164
+ } ;
165
+ operation . ParametersValues = operation . Parameters . Select ( p => new ParameterValue { parameter = p } ) . ToList ( ) ;
166
+
167
+ return operation ;
168
+ }
169
+
170
+ private OATag CreateOATag ( OpenApiTag openApiTag )
171
+ {
172
+ return new OATag ( )
173
+ {
174
+ Name = openApiTag . Name ,
175
+ Description = openApiTag . Description ,
176
+ ExternalDocs = CreateExternalDocs ( openApiTag . ExternalDocs ) ,
177
+ } ;
178
+ }
179
+
180
+ public OARequestBody CreateOARequestBody ( OpenApiRequestBody requestBody )
181
+ {
182
+ if ( requestBody == null ) return new OARequestBody ( ) ;
183
+
184
+ return new OARequestBody ( )
185
+ {
186
+ Description = requestBody . Description ,
187
+ Required = requestBody . Required ,
188
+ } ;
189
+ }
190
+
191
+ private OAReference CreateReference ( OpenApiReference openApiReference )
192
+ {
193
+ if ( openApiReference == null ) return new OAReference ( ) ;
194
+
195
+ return new OAReference ( )
196
+ {
197
+ IsPresent = true ,
198
+ ExternalResource = openApiReference . ExternalResource ,
199
+ Type = ( OAReferenceType ) openApiReference . Type ,
200
+ Id = openApiReference . Id ,
201
+ IsExternal = openApiReference . IsExternal ,
202
+ IsLocal = openApiReference . IsLocal ,
203
+ Reference = string . IsNullOrEmpty ( openApiReference . ReferenceV2 )
204
+ ? openApiReference . ReferenceV3 : openApiReference . ReferenceV2 ,
205
+ } ;
206
+ }
207
+
208
+ private OAContact CreateContact ( OpenApiContact openApiContact )
209
+ {
210
+ if ( openApiContact == null )
211
+ return new OAContact ( ) ;
212
+
213
+ return new OAContact ( )
214
+ {
215
+ Url = openApiContact . Url . ToString ( ) ,
216
+ Name = openApiContact . Name ,
217
+ Email = openApiContact . Email ,
218
+ Present = true ,
219
+ } ;
220
+ }
221
+
222
+ private OALicense CreateLicence ( OpenApiLicense openApiLicense )
223
+ {
224
+ if ( openApiLicense == null ) return new OALicense ( ) ;
225
+
226
+ return new OALicense ( )
227
+ {
228
+ Name = openApiLicense . Name ,
229
+ Url = openApiLicense . Url . ToString ( ) ,
230
+ Present = true ,
231
+ } ;
232
+ }
233
+
234
+ private OAExternalDocs CreateExternalDocs ( OpenApiExternalDocs ExternalDocs )
235
+ {
236
+ if ( ExternalDocs != null )
237
+ {
238
+ return new OAExternalDocs ( )
239
+ {
240
+ Description = ExternalDocs . Description ,
241
+ Url = ExternalDocs . Url . ToString ( ) ,
242
+ } ;
243
+ }
244
+ return new OAExternalDocs ( ) ;
245
+ }
246
+
247
+ private OAServer CreateAOServer ( OpenApiServer s )
248
+ {
249
+ var server = new OAServer ( )
250
+ {
251
+ Description = s . Description ,
252
+ Url = s . Url ,
253
+ Variables = s . Variables . ToDictionary ( v => v . Key , v => new OAServerVariable ( )
254
+ {
255
+ Name = v . Key ,
256
+ Description = v . Value . Description ,
257
+ Default = v . Value . Default ,
258
+ Enum = new List < string > ( v . Value . Enum ) ,
259
+ Current = v . Value . Enum . IndexOf ( v . Value . Default )
260
+ } ) . Values . ToList ( ) ,
261
+ } ;
262
+ server . Variables . ForEach ( v =>
263
+ {
264
+ v . Default = v . Default . Trim ( '/' ) ;
265
+ if ( v . Enum . Count == 0 )
266
+ {
267
+ v . Enum = new List < string > { v . Default } ;
268
+ }
269
+ else
270
+ {
271
+ // trim all slashes for variants
272
+ v . Enum = new List < string > ( v . Enum . Select ( e => e . Trim ( '/' ) ) ) ;
273
+ }
274
+ v . Current = 0 ;
275
+ } ) ;
276
+ return server ;
277
+ }
278
+
94
279
}
95
280
96
281
[ CustomEditor ( typeof ( OpenApiParser ) ) ]
0 commit comments