Skip to content

Commit b1a73f2

Browse files
committed
assets independancy
Fixed dependancy of api assets from the parsed open api model
1 parent b7f895f commit b1a73f2

File tree

14 files changed

+202
-181
lines changed

14 files changed

+202
-181
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ crashlytics-build.properties
5252

5353
# Custom
5454
[Aa]ssets/Apis
55-
[Aa]ssets/Apis.meta
55+
[Aa]ssets/Apis.meta
56+
[Aa]ssets/Experiments
57+
[Aa]ssets/Experiments.meta

Assets/UnityOpenApi/Editor/OpenApiParser.cs

Lines changed: 191 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,218 @@ private MemoryStream CreateStream(string text)
6464
return stream;
6565
}
6666

67-
public void GenerateAssets(OpenApiDocument openApiDocument)
67+
public void GenerateAssets(OpenApiDocument doc)
6868
{
6969
string assetsPath = EditorUtility.OpenFolderPanel("Select assets folder", _lastAssetPath, "");
7070
_lastAssetPath = assetsPath;
7171
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
7475
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+
7590

7691
apiAsset.pathItemAssets = new List<PathItemAsset>();
7792

78-
foreach (var p in openApiDocument.Paths)
93+
#endregion
94+
95+
foreach (var p in doc.Paths)
7996
{
8097
string fileName = p.Key.Replace('/', '_');
8198
PathItemAsset a = AssetsHelper.GetOrCreateScriptableObject<PathItemAsset>(assetsPath, fileName);
8299
a.ApiAsset = apiAsset;
83100

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
85113

86114
apiAsset.pathItemAssets.Add(a);
87115
}
88116

89-
117+
90118

91119
AssetDatabase.SaveAssets();
92120
}
93121

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+
94279
}
95280

96281
[CustomEditor(typeof(OpenApiParser))]

Assets/UnityOpenApi/OA Models/OAContact.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ public class OAContact
1010
public string Name;
1111
public string Url;
1212
public string Email;
13-
public bool Present = true;
14-
15-
public OAContact (OpenApiContact openApiContact)
16-
{
17-
if(openApiContact == null)
18-
{
19-
Present = false;
20-
return;
21-
}
22-
Url = openApiContact.Url.ToString();
23-
Name = openApiContact.Name;
24-
Email = openApiContact.Email;
25-
}
13+
public bool Present;
2614
}
2715
}

Assets/UnityOpenApi/OA Models/OAExternalDocs.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,5 @@ public class OAExternalDocs
88
{
99
public string Description;
1010
public string Url;
11-
12-
public OAExternalDocs(OpenApiExternalDocs openApiExternalDocs)
13-
{
14-
if (openApiExternalDocs == null) return;
15-
Description = openApiExternalDocs.Description;
16-
Url = openApiExternalDocs.Url.ToString();
17-
}
1811
}
1912
}

Assets/UnityOpenApi/OA Models/OAInfo.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44
namespace UnityOpenApi
55
{
66
[Serializable]
7-
internal class OAInfo
7+
public class OAInfo
88
{
99
public string Title;
1010
public string Description;
1111
public string Version;
1212
public string TermsOfService;
1313
public OAContact Contact;
1414
public OALicense License;
15-
16-
public OAInfo (OpenApiInfo openApiInfo)
17-
{
18-
Title = openApiInfo.Title;
19-
Description = openApiInfo.Description;
20-
Version = openApiInfo.Version;
21-
TermsOfService = openApiInfo.TermsOfService == null ? "" : openApiInfo.TermsOfService.ToString();
22-
Contact = new OAContact(openApiInfo.Contact);
23-
License = new OALicense(openApiInfo.License);
24-
}
2515
}
2616
}

Assets/UnityOpenApi/OA Models/OALicense.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ public class OALicense
88
{
99
public string Name;
1010
public string Url;
11-
public bool Present = true;
12-
13-
public OALicense(OpenApiLicense openApiLicense)
14-
{
15-
if(openApiLicense==null)
16-
{
17-
Present = false;
18-
return;
19-
}
20-
Name = openApiLicense.Name;
21-
Url = openApiLicense.Url.ToString();
22-
}
11+
public bool Present;
2312
}
2413
}

Assets/UnityOpenApi/OA Models/OAOperation.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,6 @@ public int OperationCurrentHash
4242
}
4343
}
4444

45-
public OAOperation(OperationType operationType, OpenApiOperation op, PathItemAsset pathItemAsset)
46-
{
47-
pathAsset = pathItemAsset;
48-
OperationId = op.OperationId;
49-
OperationType = (AOOperationType)operationType;
50-
Summary = op.Summary;
51-
Description = op.Description;
52-
Deprecated = op.Deprecated;
53-
54-
if (op.Parameters.Count > 0)
55-
{
56-
Parameters = op.Parameters.Select(p => new OAParameter(p)).ToList();
57-
}
58-
else
59-
{
60-
Parameters = pathItemAsset.Parameters;
61-
}
62-
63-
ParametersValues = Parameters.Select(p => new ParameterValue { parameter = p }).ToList();
64-
65-
Servers = op.Servers.Select(s => new OAServer(s)).ToList();
66-
67-
Tags = op.Tags.Select(t => new OATag(t)).ToList();
68-
69-
RequestBody = new OARequestBody(op.RequestBody);
70-
71-
ExternalDocs = new OAExternalDocs(op.ExternalDocs);
72-
}
73-
7445
public void SetParameterValue(string parameterName, string val)
7546
{
7647
var parVal = ParametersValues.FirstOrDefault(p => p.parameter.Name == parameterName);

Assets/UnityOpenApi/OA Models/OAParameter.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,5 @@ public class OAParameter
1818
public OAReference Reference;
1919
public bool UnresolvedReference;
2020
//public IDictionary<string, OpenApiMediaType> Content;
21-
22-
public OAParameter (OpenApiParameter openApiParameter)
23-
{
24-
Name = openApiParameter.Name;
25-
Required = openApiParameter.Required;
26-
AllowReserved = openApiParameter.AllowReserved;
27-
Explode = openApiParameter.Explode;
28-
AllowEmptyValue = openApiParameter.AllowEmptyValue;
29-
Deprecated = openApiParameter.Deprecated;
30-
Description = openApiParameter.Description;
31-
UnresolvedReference = openApiParameter.UnresolvedReference;
32-
33-
In = (OAParameterLocation)openApiParameter.In;
34-
35-
Reference = new OAReference(openApiParameter.Reference);
36-
}
3721
}
3822
}

Assets/UnityOpenApi/OA Models/OAReference.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,5 @@ public class OAReference
1313
public bool IsExternal;
1414
public bool IsLocal;
1515
public string Reference;
16-
17-
public OAReference(OpenApiReference openApiReference)
18-
{
19-
if (openApiReference == null) return;
20-
21-
IsPresent = true;
22-
ExternalResource = openApiReference.ExternalResource;
23-
Type = (OAReferenceType)openApiReference.Type;
24-
Id = openApiReference.Id;
25-
IsExternal = openApiReference.IsExternal;
26-
IsLocal = openApiReference.IsLocal;
27-
Reference = string.IsNullOrEmpty(openApiReference.ReferenceV2)
28-
? openApiReference.ReferenceV3 : openApiReference.ReferenceV2;
29-
}
3016
}
3117
}

0 commit comments

Comments
 (0)