Skip to content

Commit

Permalink
Merge pull request #6 from microsoft/bugfix/typo-in-properties
Browse files Browse the repository at this point in the history
bugfix/typo in properties
  • Loading branch information
darrelmiller authored Aug 17, 2023
2 parents bef885b + d5a5479 commit 96b16a2
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 64 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"dotnet.defaultSolution": "apimanifest.sln"
}
11 changes: 6 additions & 5 deletions src/lib/AccessRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

namespace Microsoft.OpenApi.ApiManifest;

public class AccessRequest {
public class AccessRequest
{

private const string TypeProperty = "type";
private const string ContentProperty = "content";

public string? Type {get;set;}
public JsonObject? Content {get;set;}
public string? Type { get; set; }
public JsonObject? Content { get; set; }

internal static AccessRequest Load(JsonElement content)
{
var accessRequest = new AccessRequest();
ParsingHelpers.ParseMap(content, accessRequest, handlers);
return accessRequest;

}
public void Write(Utf8JsonWriter writer)
{
Expand All @@ -30,7 +31,7 @@ public void Write(Utf8JsonWriter writer)
writer.WriteEndObject();
}

private static FixedFieldMap<AccessRequest> handlers = new()
private static readonly FixedFieldMap<AccessRequest> handlers = new()
{
{ TypeProperty, (o,v) => {o.Type = v.GetString(); } },
{ ContentProperty, (o,v) => {o.Content = JsonSerializer.Deserialize<JsonObject>(v); } },
Expand Down
31 changes: 19 additions & 12 deletions src/lib/ApiDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Microsoft.OpenApi.ApiManifest;
public class ApiDependency
{
public string? ApiDescripionUrl { get; set; }
public string? ApiDescripionVersion { get; set; }
public string? ApiDescriptionUrl { get; set; }
public string? ApiDescriptionVersion { get; set; }
public string? ApiDeploymentBaseUrl { get; set; }
public Auth? Auth { get; set; }
public List<Request> Requests { get; set; } = new List<Request>();
public Extensions? Extensions { get; set; }

private const string ApiDescriptionUrlProperty = "apiDescripionUrl";
private const string ApiDescriptionVersionProperty = "apiDescripionVersion";
private const string ApiDescriptionUrlProperty = "apiDescriptionUrl";
private const string ApiDescriptionVersionProperty = "apiDescriptionVersion";
private const string ApiDeploymentBaseUrlProperty = "apiDeploymentBaseUrl";
private const string AuthProperty = "auth";
private const string RequestsProperty = "requests";
private const string ExtensionsProperty = "extensions";
Expand All @@ -20,15 +22,18 @@ public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();

if (!String.IsNullOrWhiteSpace(ApiDescripionUrl)) writer.WriteString(ApiDescriptionUrlProperty, ApiDescripionUrl);
if (!String.IsNullOrWhiteSpace(ApiDescripionVersion)) writer.WriteString(ApiDescriptionVersionProperty, ApiDescripionVersion);
if (!string.IsNullOrWhiteSpace(ApiDescriptionUrl)) writer.WriteString(ApiDescriptionUrlProperty, ApiDescriptionUrl);
if (!string.IsNullOrWhiteSpace(ApiDescriptionVersion)) writer.WriteString(ApiDescriptionVersionProperty, ApiDescriptionVersion);
if (!string.IsNullOrWhiteSpace(ApiDeploymentBaseUrl)) writer.WriteString(ApiDeploymentBaseUrlProperty, ApiDeploymentBaseUrl);

if (Auth != null) {
if (Auth != null)
{
writer.WritePropertyName(AuthProperty);
Auth?.Write(writer);
}

if (Requests.Count > 0) {
if (Requests.Count > 0)
{
writer.WritePropertyName(RequestsProperty);
writer.WriteStartArray();
foreach (var request in Requests)
Expand All @@ -38,18 +43,20 @@ public void Write(Utf8JsonWriter writer)
writer.WriteEndArray();
}

if (Extensions != null) {
if (Extensions != null)
{
writer.WritePropertyName(ExtensionsProperty);
Extensions?.Write(writer);
}

writer.WriteEndObject();
}
// Fixed fieldmap for ApiDependency
private static FixedFieldMap<ApiDependency> handlers = new()
private static readonly FixedFieldMap<ApiDependency> handlers = new()
{
{ ApiDescriptionUrlProperty, (o,v) => {o.ApiDescripionUrl = v.GetString(); } },
{ ApiDescriptionVersionProperty, (o,v) => {o.ApiDescripionVersion = v.GetString(); } },
{ ApiDescriptionUrlProperty, (o,v) => {o.ApiDescriptionUrl = v.GetString(); } },
{ ApiDescriptionVersionProperty, (o,v) => {o.ApiDescriptionVersion = v.GetString(); } },
{ ApiDeploymentBaseUrlProperty, (o,v) => {o.ApiDeploymentBaseUrl = v.GetString(); } },
{ AuthProperty, (o,v) => {o.Auth = Auth.Load(v); } },
{ RequestsProperty, (o,v) => {o.Requests = ParsingHelpers.GetList(v, Request.Load); } },
{ ExtensionsProperty, (o,v) => {o.Extensions = Extensions.Load(v); } }
Expand Down
14 changes: 8 additions & 6 deletions src/lib/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ public class Extensions : Dictionary<string, JsonNode?>
public static Extensions Load(JsonElement value)
{
var extensions = new Extensions();
foreach(var property in value.EnumerateObject())
foreach (var property in value.EnumerateObject())
{
if (property.Value.ValueKind != JsonValueKind.Null) {
if (property.Value.ValueKind != JsonValueKind.Null)
{
var extensionValue = JsonSerializer.Deserialize<JsonObject>(property.Value.GetRawText());
extensions.Add(property.Name, extensionValue);
}
}
}
return extensions;
}

public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
foreach(var extension in this)
foreach (var extension in this)
{
writer.WritePropertyName(extension.Key);
writer.WriteRawValue(extension.Value.ToJsonString());
if (extension.Value is not null)
writer.WriteRawValue(extension.Value.ToJsonString());
}
writer.WriteEndObject();
}
}
}
35 changes: 20 additions & 15 deletions src/lib/OpenAI/BaseManifestAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public abstract class BaseManifestAuth
{
BaseManifestAuth? auth = null;

switch(value.GetProperty("type").GetString()) {
switch (value.GetProperty("type").GetString())
{
case "none":
auth = new ManifestNoAuth();
ParsingHelpers.ParseMap<ManifestNoAuth>(value, (ManifestNoAuth)auth, ManifestNoAuth.handlers);
Expand All @@ -30,13 +31,13 @@ public abstract class BaseManifestAuth
ParsingHelpers.ParseMap<ManifestOAuthAuth>(value, (ManifestOAuthAuth)auth, ManifestOAuthAuth.handlers);
break;
}

return auth;
}

// Create handlers FixedFieldMap for ManifestAuth

public virtual void Write(Utf8JsonWriter writer) {}
public virtual void Write(Utf8JsonWriter writer) { }

}

Expand All @@ -53,10 +54,11 @@ public ManifestNoAuth()
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
};

public override void Write(Utf8JsonWriter writer) {
public override void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteString("type", Type);
if(Instructions != null) writer.WriteString("instructions", Instructions);
if (Instructions != null) writer.WriteString("instructions", Instructions);
writer.WriteEndObject();
}
}
Expand All @@ -81,18 +83,19 @@ public ManifestOAuthAuth()
{ "scope", (o,v) => {o.Scope = v.GetString(); } },
{ "authorization_url", (o,v) => {o.AuthorizationUrl = v.GetString(); } },
{ "authorization_content_type", (o,v) => {o.AuthorizationContentType = v.GetString(); } },
{ "verification_tokens", (o,v) => { o.VerificationTokens = ParsingHelpers.GetMap<string>(v,(e) => e.GetString() ); } },
{ "verification_tokens", (o,v) => { o.VerificationTokens = ParsingHelpers.GetMap<string>(v,(e) => e.GetString() is string val && !string.IsNullOrEmpty(val) ? val : string.Empty ); } },
};

public override void Write(Utf8JsonWriter writer) {
public override void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteString("type", Type);
if(Instructions != null) writer.WriteString("instructions", Instructions);
if(ClientUrl != null) writer.WriteString("client_url", ClientUrl);
if(Scope != null) writer.WriteString("scope", Scope);
if(AuthorizationUrl != null) writer.WriteString("authorization_url", AuthorizationUrl);
if(AuthorizationContentType != null) writer.WriteString("authorization_content_type", AuthorizationContentType);

if (Instructions != null) writer.WriteString("instructions", Instructions);
if (ClientUrl != null) writer.WriteString("client_url", ClientUrl);
if (Scope != null) writer.WriteString("scope", Scope);
if (AuthorizationUrl != null) writer.WriteString("authorization_url", AuthorizationUrl);
if (AuthorizationContentType != null) writer.WriteString("authorization_content_type", AuthorizationContentType);
writer.WriteEndObject();
}
}
Expand All @@ -108,7 +111,8 @@ public ManifestUserHttpAuth()
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
};
public override void Write(Utf8JsonWriter writer) {
public override void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteString("type", Type);
writer.WriteString("instructions", Instructions);
Expand All @@ -127,7 +131,8 @@ public ManifestServiceHttpAuth()
{ "type", (o,v) => {o.Type = v.GetString(); } },
{ "instructions", (o,v) => {o.Instructions = v.GetString(); } },
};
public override void Write(Utf8JsonWriter writer) {
public override void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();
writer.WriteString("type", Type);
writer.WriteString("instructions", Instructions);
Expand Down
11 changes: 9 additions & 2 deletions src/lib/ParsingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public static void ParseMap<T>(JsonElement node, T permissionsDocument, FixedFie
{
foreach (var element in node.EnumerateObject())
{
handlers[element.Name](permissionsDocument, element.Value);
if (handlers.TryGetValue(element.Name, out var handler))
{
handler(permissionsDocument, element.Value);
}
//TODO we should log the unknown property or use an additional properties model
};
}

Expand Down Expand Up @@ -48,7 +52,7 @@ internal static List<string> GetListOfString(JsonElement v)
foreach (var item in v.EnumerateArray())
{
var value = item.GetString();
if (value != null)
if (value != null)
list.Add(value);
}
return list;
Expand Down Expand Up @@ -121,5 +125,8 @@ internal static IEnumerable<KeyValuePair<string, string>> ParseKey(string key)

public class FixedFieldMap<T> : Dictionary<string, Action<T, JsonElement>>
{
public FixedFieldMap() : base(StringComparer.OrdinalIgnoreCase)
{

}
}
16 changes: 8 additions & 8 deletions src/lib/Publisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ namespace Microsoft.OpenApi.ApiManifest;
public class Publisher
{
public string? Name { get; set; }
public string ContactEmail { get; set; }
public string? ContactEmail { get; set; }
private const string NameProperty = "name";
private const string ContactEmailProperty = "contactEmail";

public Publisher(string contactEmail)
{
if (String.IsNullOrWhiteSpace(contactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher.");
if (string.IsNullOrWhiteSpace(contactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher.");
ContactEmail = contactEmail;
}
}
private Publisher(JsonElement value)
{
ParsingHelpers.ParseMap(value, this, handlers);
// Validate that Name and ContactEmail are not null
if (String.IsNullOrWhiteSpace(this.Name)) throw new ArgumentNullException("Name is a required property of publisher.");
if (String.IsNullOrWhiteSpace(this.ContactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher.");
if (string.IsNullOrWhiteSpace(Name)) throw new ArgumentNullException("Name is a required property of publisher.");
if (string.IsNullOrWhiteSpace(ContactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher.");
}

// Write method
public void Write(Utf8JsonWriter writer)
{
writer.WriteStartObject();

if (!String.IsNullOrWhiteSpace(Name)) writer.WriteString(NameProperty, Name);
if (!String.IsNullOrWhiteSpace(ContactEmail)) writer.WriteString(ContactEmailProperty, ContactEmail);
if (!string.IsNullOrWhiteSpace(Name)) writer.WriteString(NameProperty, Name);
if (!string.IsNullOrWhiteSpace(ContactEmail)) writer.WriteString(ContactEmailProperty, ContactEmail);

writer.WriteEndObject();
}
Expand All @@ -38,7 +38,7 @@ internal static Publisher Load(JsonElement value)
return new Publisher(value);
}

private static FixedFieldMap<Publisher> handlers = new()
private static readonly FixedFieldMap<Publisher> handlers = new()
{
{ NameProperty, (o,v) => {o.Name = v.GetString(); } },
{ ContactEmailProperty, (o,v) => {o.ContactEmail = v.GetString(); } },
Expand Down
6 changes: 3 additions & 3 deletions src/lib/apimanifest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>Microsoft.OpenApi.ApiManifest</PackageId>
<VersionPrefix>0.5.0</VersionPrefix>
<VersionPrefix>0.5.1</VersionPrefix>
<VersionSuffix>preview</VersionSuffix>
<PackageIconUrl>http://go.microsoft.com/fwlink/?LinkID=288890</PackageIconUrl>
<PackageProjectUrl>https://github.com/Microsoft/OpenApi.ApiManifest</PackageProjectUrl>
Expand All @@ -12,7 +12,7 @@
<Authors>Microsoft</Authors>
<Company>Microsoft</Company>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
Expand All @@ -22,4 +22,4 @@
<AssemblyOriginatorKeyFile>sgKey.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

</Project>
</Project>
Loading

0 comments on commit 96b16a2

Please sign in to comment.