Skip to content

Commit

Permalink
add null check for swagger apiview (#4638)
Browse files Browse the repository at this point in the history
* add null check for swagger apiview

* Add type format for SwaggerApiViewParameters

* add null check

* update changelog and bump version

* reformat

* Change enum type to JsonElement support int,string, boolean type.

* update changelog

* remove wrong refChain operation

Co-authored-by: ruowan <ruowan@ruowandeMacBook-Pro.local>
  • Loading branch information
ruowan and ruowan committed Nov 9, 2022
1 parent 193081a commit b60e777
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History

## Version 1.0.2 (11-8-2022)
+ Add null check when cannot resolve reference from schema cache.
+ Change enum type to JsonElement to support different enum type like, string, boolean, int

## Version 1.0.1 (10-31-2022)

+ Fix missing first property for definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>swagger_api_parser</RootNamespace>
<LangVersion>Latest</LangVersion>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionPrefix>1.0.2</VersionPrefix>
<AssemblyName>Azure.Sdk.Tools.SwaggerApiParser</AssemblyName>
<SignAssembly>True</SignAssembly>
<InformationalVersion>$(OfficialBuildId)</InformationalVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ public List<string> GetKeywords()
return ret;
}

public String GetTypeFormat()
{
var ret = "";
if (this.type != null)
{
ret = this.type;
}
else if (this.schema != null)
{
ret = this.schema.GetTypeFormat();
}

if (this.format != null)
{
ret += $"/{this.format}";
}

return ret;
}


public CodeFileToken[] TokenSerialize(SerializeContext context)
{
Expand All @@ -39,7 +59,7 @@ public CodeFileToken[] TokenSerialize(SerializeContext context)
ret.Add(TokenSerializer.NavigableToken("ref", CodeFileTokenKind.Keyword, context.IteratorPath.CurrentNextPath("ref")));
ret.Add(TokenSerializer.Colon());
string navigationToId = context.IteratorPath.rootPath() + Utils.GetRefDefinitionIdPath(this.Ref);
ret.Add(new CodeFileToken(this.Ref, CodeFileTokenKind.Literal){NavigateToId = navigationToId});
ret.Add(new CodeFileToken(this.Ref, CodeFileTokenKind.Literal) {NavigateToId = navigationToId});
return ret.ToArray();
}

Expand Down Expand Up @@ -130,12 +150,8 @@ private CodeFileToken[] TokenSerializeTableRows(SerializeContext context)
foreach (var parameter in this)
{
ret.AddRange(TokenSerializer.TableCell(new[] {new CodeFileToken(parameter.name, CodeFileTokenKind.MemberName)}));
var parameterType = parameter.type;
var parameterType = parameter.GetTypeFormat();

if (parameter.format != null)
{
parameterType += "/" + parameter.format;
}

ret.AddRange(TokenSerializer.TableCell(new[] {new CodeFileToken(parameterType, CodeFileTokenKind.Keyword)}));
ret.AddRange(TokenSerializer.TableCell(new[] {new CodeFileToken(String.Join(",", parameter.GetKeywords()), CodeFileTokenKind.Literal)}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@ public BaseSchema GetResolvedSchema(BaseSchema root, string currentSwaggerFilePa

if (root.items != null && root.items.Ref != null && !refChain.Contains(root.items.Ref))
{
refChain.AddLast(root.items.Ref);
root.items = GetResolvedSchema(root.items, currentSwaggerFilePath, refChain);
}

if (root.properties != null)
{
foreach (var rootProperty in root.properties)
{
if (rootProperty.Value == null)
{
continue;
}
if (!refChain.Contains(rootProperty.Value.Ref) && !refChain.Contains(rootProperty.Value.Ref) && !refChain.Contains(rootProperty.Value.items?.Ref))
{
root.properties[rootProperty.Key] = this.GetResolvedSchema(rootProperty.Value, currentSwaggerFilePath, refChain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public object ResolveRefObj(string Ref)
if (Ref.Contains("parameters"))
{
var key = Ref.Split("/").Last();
if (this.parameters == null)
{
return null;
}
this.parameters.TryGetValue(key, out var ret);
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using APIView;

Expand Down Expand Up @@ -75,7 +76,7 @@ public class BaseSchema : ITokenSerializable

public List<string> required { get; set; }

[JsonPropertyName("enum")] public List<string> Enum { get; set; }
[JsonPropertyName("enum")] public List<JsonElement> Enum { get; set; }

public bool IsPropertyRequired(string propertyName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public CodeFileToken[] TokenSerialize(SerializeContext context)
ret.Add(TokenSerializer.FoldableContentStart());
foreach (var parameter in parameters)
{
ret.AddRange(parameter.TokenSerialize(context));
if (parameter != null)
{
ret.AddRange(parameter.TokenSerialize(context));
}
}

ret.Add(TokenSerializer.FoldableContentEnd());
Expand Down

0 comments on commit b60e777

Please sign in to comment.