Skip to content

Commit 403fdbc

Browse files
committed
Updated referencable items to use GetEffective when inlining
1 parent d41cf71 commit 403fdbc

File tree

11 files changed

+321
-80
lines changed

11 files changed

+321
-80
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace Microsoft.OpenApi.Hidi
2828
{
2929
public class OpenApiService
3030
{
31-
public static async void ProcessOpenApiDocument(
31+
public static async Task ProcessOpenApiDocument(
3232
string openapi,
3333
string csdl,
3434
FileInfo output,
3535
OpenApiSpecVersion? version,
3636
OpenApiFormat? format,
3737
LogLevel loglevel,
38-
bool inline,
39-
bool resolveexternal,
38+
bool inlineLocal,
39+
bool inlineExternal,
4040
string filterbyoperationids,
4141
string filterbytags,
4242
string filterbycollection
@@ -104,8 +104,9 @@ string filterbycollection
104104
logger.LogTrace("Parsing OpenApi file");
105105
var result = new OpenApiStreamReader(new OpenApiReaderSettings
106106
{
107-
ReferenceResolution = resolveexternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
108-
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
107+
RuleSet = ValidationRuleSet.GetDefaultRuleSet(),
108+
LoadExternalRefs = inlineExternal,
109+
BaseUrl = openapi.StartsWith("http") ? new Uri(openapi) : new Uri("file:" + new FileInfo(openapi).DirectoryName + "\\")
109110
}
110111
).ReadAsync(stream).GetAwaiter().GetResult();
111112

@@ -249,7 +250,7 @@ private static async Task<Stream> GetStream(string input, ILogger logger)
249250
stopwatch.Start();
250251

251252
Stream stream;
252-
if (input.Scheme == "http" || input.Scheme == "https")
253+
if (input.StartsWith("http"))
253254
{
254255
try
255256
{
@@ -269,7 +270,7 @@ private static async Task<Stream> GetStream(string input, ILogger logger)
269270
return null;
270271
}
271272
}
272-
else if (input.Scheme == "file")
273+
else
273274
{
274275
try
275276
{
@@ -336,11 +337,10 @@ internal static async Task ValidateOpenApiDocument(string openapi, LogLevel logl
336337

337338
OpenApiDocument document;
338339
logger.LogTrace("Parsing the OpenApi file");
339-
document = new OpenApiStreamReader(new OpenApiReaderSettings
340+
var result = await new OpenApiStreamReader(new OpenApiReaderSettings
340341
{
341-
ReferenceResolution = resolveExternal == true ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
342342
RuleSet = ValidationRuleSet.GetDefaultRuleSet(),
343-
BaseUrl = new Uri(inputUrl.AbsoluteUri)
343+
BaseUrl = new Uri(openapi)
344344
}
345345
).ReadAsync(stream);
346346

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ static async Task<int> Main(string[] args)
4343
var filterByCollectionOption = new Option<string>("--filter-by-collection", "Filters OpenApiDocument by Postman collection provided");
4444
filterByCollectionOption.AddAlias("-c");
4545

46-
var inlineOption = new Option<bool>("--inline", "Inline $ref instances");
47-
inlineOption.AddAlias("-i");
46+
var inlineLocalOption = new Option<bool>("--inlineLocal", "Inline local $ref instances");
47+
inlineLocalOption.AddAlias("-il");
4848

49-
var resolveExternalOption = new Option<bool>("--resolve-external", "Resolve external $refs");
50-
resolveExternalOption.AddAlias("-ex");
49+
var inlineExternalOption = new Option<bool>("--inlineExternal", "Inline external $ref instances");
50+
inlineExternalOption.AddAlias("-ie");
5151

5252
var validateCommand = new Command("validate")
5353
{
@@ -68,12 +68,12 @@ static async Task<int> Main(string[] args)
6868
filterByOperationIdsOption,
6969
filterByTagsOption,
7070
filterByCollectionOption,
71-
inlineOption,
72-
resolveExternalOption,
71+
inlineLocalOption,
72+
inlineExternalOption
7373
};
7474

7575
transformCommand.SetHandler<string, string, FileInfo, OpenApiSpecVersion?, OpenApiFormat?, LogLevel, bool, bool, string, string, string> (
76-
OpenApiService.ProcessOpenApiDocument, descriptionOption, csdlOption, outputOption, versionOption, formatOption, logLevelOption, inlineOption, resolveExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
76+
OpenApiService.ProcessOpenApiDocument, descriptionOption, csdlOption, outputOption, versionOption, formatOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
7777

7878
rootCommand.Add(transformCommand);
7979
rootCommand.Add(validateCommand);

src/Microsoft.OpenApi/Models/OpenApiCallback.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,41 @@ public void SerializeAsV3(IOpenApiWriter writer)
7070
throw Error.ArgumentNull(nameof(writer));
7171
}
7272

73-
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
73+
var target = this;
74+
75+
if (Reference != null)
7476
{
75-
Reference.SerializeAsV3(writer);
76-
return;
77+
if (!writer.GetSettings().ShouldInlineReference(Reference))
78+
{
79+
Reference.SerializeAsV3(writer);
80+
return;
81+
}
82+
else
83+
{
84+
target = GetEffective(Reference.HostDocument);
85+
}
7786
}
87+
target.SerializeAsV3WithoutReference(writer);
88+
}
7889

79-
SerializeAsV3WithoutReference(writer);
90+
/// <summary>
91+
/// Returns an effective OpenApiCallback object based on the presence of a $ref
92+
/// </summary>
93+
/// <param name="doc">The host OpenApiDocument that contains the reference.</param>
94+
/// <returns>OpenApiCallback</returns>
95+
public OpenApiCallback GetEffective(OpenApiDocument doc)
96+
{
97+
if (this.Reference != null)
98+
{
99+
return doc.ResolveReferenceTo<OpenApiCallback>(this.Reference);
100+
}
101+
else
102+
{
103+
return this;
104+
}
80105
}
81106

107+
82108
/// <summary>
83109
/// Serialize to OpenAPI V3 document without using reference.
84110
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,38 @@ public void SerializeAsV3(IOpenApiWriter writer)
6464
throw Error.ArgumentNull(nameof(writer));
6565
}
6666

67-
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
67+
var target = this;
68+
69+
if (Reference != null)
6870
{
69-
Reference.SerializeAsV3(writer);
70-
return;
71+
if (!writer.GetSettings().ShouldInlineReference(Reference))
72+
{
73+
Reference.SerializeAsV3(writer);
74+
return;
75+
}
76+
else
77+
{
78+
target = GetEffective(Reference.HostDocument);
79+
}
7180
}
81+
target.SerializeAsV3WithoutReference(writer);
82+
}
7283

73-
SerializeAsV3WithoutReference(writer);
84+
/// <summary>
85+
/// Returns an effective OpenApiExample object based on the presence of a $ref
86+
/// </summary>
87+
/// <param name="doc">The host OpenApiDocument that contains the reference.</param>
88+
/// <returns>OpenApiExample</returns>
89+
public OpenApiExample GetEffective(OpenApiDocument doc)
90+
{
91+
if (this.Reference != null)
92+
{
93+
return doc.ResolveReferenceTo<OpenApiExample>(this.Reference);
94+
}
95+
else
96+
{
97+
return this;
98+
}
7499
}
75100

76101
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,42 @@ public void SerializeAsV3(IOpenApiWriter writer)
9696
throw Error.ArgumentNull(nameof(writer));
9797
}
9898

99-
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
99+
var target = this;
100+
101+
if (Reference != null)
100102
{
101-
Reference.SerializeAsV3(writer);
102-
return;
103+
if (!writer.GetSettings().ShouldInlineReference(Reference))
104+
{
105+
Reference.SerializeAsV3(writer);
106+
return;
107+
}
108+
else
109+
{
110+
target = GetEffective(Reference.HostDocument);
111+
}
103112
}
113+
target.SerializeAsV3WithoutReference(writer);
104114

105-
SerializeAsV3WithoutReference(writer);
106115
}
107116

117+
/// <summary>
118+
/// Returns an effective OpenApiHeader object based on the presence of a $ref
119+
/// </summary>
120+
/// <param name="doc">The host OpenApiDocument that contains the reference.</param>
121+
/// <returns>OpenApiHeader</returns>
122+
public OpenApiHeader GetEffective(OpenApiDocument doc)
123+
{
124+
if (this.Reference != null)
125+
{
126+
return doc.ResolveReferenceTo<OpenApiHeader>(this.Reference);
127+
}
128+
else
129+
{
130+
return this;
131+
}
132+
}
133+
134+
108135
/// <summary>
109136
/// Serialize to OpenAPI V3 document without using reference.
110137
/// </summary>
@@ -161,13 +188,21 @@ public void SerializeAsV2(IOpenApiWriter writer)
161188
throw Error.ArgumentNull(nameof(writer));
162189
}
163190

164-
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
191+
var target = this;
192+
193+
if (Reference != null)
165194
{
166-
Reference.SerializeAsV2(writer);
167-
return;
195+
if (!writer.GetSettings().ShouldInlineReference(Reference))
196+
{
197+
Reference.SerializeAsV2(writer);
198+
return;
199+
}
200+
else
201+
{
202+
target = GetEffective(Reference.HostDocument);
203+
}
168204
}
169-
170-
SerializeAsV2WithoutReference(writer);
205+
target.SerializeAsV2WithoutReference(writer);
171206
}
172207

173208
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiLink.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,42 @@ public void SerializeAsV3(IOpenApiWriter writer)
7171
throw Error.ArgumentNull(nameof(writer));
7272
}
7373

74-
if (Reference != null && !writer.GetSettings().ShouldInlineReference(Reference))
74+
var target = this;
75+
76+
if (Reference != null)
7577
{
76-
Reference.SerializeAsV3(writer);
77-
return;
78+
if (!writer.GetSettings().ShouldInlineReference(Reference))
79+
{
80+
Reference.SerializeAsV3(writer);
81+
return;
82+
}
83+
else
84+
{
85+
target = GetEffective(Reference.HostDocument);
86+
}
7887
}
88+
target.SerializeAsV3WithoutReference(writer);
7989

80-
SerializeAsV3WithoutReference(writer);
8190
}
8291

92+
/// <summary>
93+
/// Returns an effective OpenApiLink object based on the presence of a $ref
94+
/// </summary>
95+
/// <param name="doc">The host OpenApiDocument that contains the reference.</param>
96+
/// <returns>OpenApiLink</returns>
97+
public OpenApiLink GetEffective(OpenApiDocument doc)
98+
{
99+
if (this.Reference != null)
100+
{
101+
return doc.ResolveReferenceTo<OpenApiLink>(this.Reference);
102+
}
103+
else
104+
{
105+
return this;
106+
}
107+
}
108+
109+
83110
/// <summary>
84111
/// Serialize to OpenAPI V3 document without using reference.
85112
/// </summary>

0 commit comments

Comments
 (0)