Skip to content

Fixes for issue 233 #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ internal static partial class OpenApiV2Deserializer
private static readonly FixedFieldMap<OpenApiExternalDocs> _externalDocsFixedFields =
new FixedFieldMap<OpenApiExternalDocs>
{
// $ref
{
"description", (o, n) =>
OpenApiConstants.Description, (o, n) =>
{
o.Description = n.GetScalarValue();
}
},
{
"url", (o, n) =>
OpenApiConstants.Url, (o, n) =>
{
o.Url = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,15 @@ private static void LoadStyle(OpenApiParameter p, string v)
{
switch (v)
{
// TODO: Handle "csv" for query / form parameter. The style should be Form, not Simple.
case "csv":
p.Style = ParameterStyle.Simple;
if (p.In == ParameterLocation.Query)
Copy link
Contributor

@PerthCharern PerthCharern Apr 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cookie didn't exist in V2, so there is no need to try and translate csv for it,.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Totally forgot :)

{
p.Style = ParameterStyle.Form;
}
else
{
p.Style = ParameterStyle.Simple;
}
return;
case "ssv":
p.Style = ParameterStyle.SpaceDelimited;
Expand Down
14 changes: 5 additions & 9 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiResponseDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,17 @@ internal static partial class OpenApiV2Deserializer
private static void ProcessProduces(OpenApiResponse response, ParsingContext context)
{
var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces) ??
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces) ?? new List<string> {"application/json"};
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces) ?? new List<string>();

response.Content = new Dictionary<string, OpenApiMediaType>();
foreach (var produce in produces)
{
var responseSchema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema);
if (responseSchema != null)
var mediaType = new OpenApiMediaType
{
var mediaType = new OpenApiMediaType
{
Schema = responseSchema
};
Schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema)
};

response.Content.Add(produce, mediaType);
}
response.Content.Add(produce, mediaType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal static partial class OpenApiV2Deserializer
{
"default", (o, n) =>
{
o.Default = new OpenApiString(n.GetScalarValue());
o.Default = n.CreateAny();
}
},
{
Expand Down
45 changes: 32 additions & 13 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiTagDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;

Expand All @@ -12,27 +13,45 @@ namespace Microsoft.OpenApi.Readers.V2
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static readonly FixedFieldMap<OpenApiTag> _tagFixedFields = new FixedFieldMap<OpenApiTag>
{
{
OpenApiConstants.Name, (o, n) =>
{
o.Name = n.GetScalarValue();
}
},
{
OpenApiConstants.Description, (o, n) =>
{
o.Description = n.GetScalarValue();
}
},
{
OpenApiConstants.ExternalDocs, (o, n) =>
{
o.ExternalDocs = LoadExternalDocs(n);
}
}
};

private static readonly PatternFieldMap<OpenApiTag> _tagPatternFields = new PatternFieldMap<OpenApiTag>
{
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
};

public static OpenApiTag LoadTag(ParseNode n)
{
var mapNode = n.CheckMapNode("tag");

var obj = new OpenApiTag();
var domainObject = new OpenApiTag();

foreach (var node in mapNode)
foreach (var propertyNode in mapNode)
{
var key = node.Name;
switch (key)
{
case "description":
obj.Description = node.Value.GetScalarValue();
break;
case "name":
obj.Name = node.Value.GetScalarValue();
break;
}
propertyNode.ParseField(domainObject, _tagFixedFields, _tagPatternFields);
}

return obj;
return domainObject;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ internal static partial class OpenApiV3Deserializer
{
"default", (o, n) =>
{
o.Default = new OpenApiString(n.GetScalarValue());
o.Default = n.CreateAny();
}
},

Expand Down
45 changes: 32 additions & 13 deletions src/Microsoft.OpenApi.Readers/V3/OpenApiTagDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;

Expand All @@ -12,27 +13,45 @@ namespace Microsoft.OpenApi.Readers.V3
/// </summary>
internal static partial class OpenApiV3Deserializer
{
private static readonly FixedFieldMap<OpenApiTag> _tagFixedFields = new FixedFieldMap<OpenApiTag>
{
{
OpenApiConstants.Name, (o, n) =>
{
o.Name = n.GetScalarValue();
}
},
{
OpenApiConstants.Description, (o, n) =>
{
o.Description = n.GetScalarValue();
}
},
{
OpenApiConstants.ExternalDocs, (o, n) =>
{
o.ExternalDocs = LoadExternalDocs(n);
}
}
};

private static readonly PatternFieldMap<OpenApiTag> _tagPatternFields = new PatternFieldMap<OpenApiTag>
{
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
};

public static OpenApiTag LoadTag(ParseNode n)
{
var mapNode = n.CheckMapNode("tag");

var obj = new OpenApiTag();
var domainObject = new OpenApiTag();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domainObject [](start = 16, length = 12)

Why is this called domainObject?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that when I cut and paste the code from one load to the next, I don't have to rename it :-)


foreach (var node in mapNode)
foreach (var propertyNode in mapNode)
{
var key = node.Name;
switch (key)
{
case "description":
obj.Description = node.Value.GetScalarValue();
break;
case "name":
obj.Name = node.Value.GetScalarValue();
break;
}
propertyNode.ParseField(domainObject, _tagFixedFields, _tagPatternFields);
}

return obj;
return domainObject;
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);

// schema
writer.WriteOptionalObject(OpenApiConstants.Schema, Schema, (w, s) => s.SerializeAsV2(w));
Schema?.WriteAsItemsProperties(writer);

// example
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, s) => w.WriteAny(s));
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,14 @@ public void SerializeAsV2(IOpenApiWriter writer)
}
else
{
var content = RequestBody.Content.Values.FirstOrDefault();
var bodyParameter = new OpenApiBodyParameter
{
Description = RequestBody.Description,
// V2 spec actually allows the body to have custom name.
// Our library does not support this at the moment.
Name = "body",
Schema = RequestBody.Content.First().Value.Schema,
Schema = content?.Schema,
Required = RequestBody.Required
};

Expand Down
17 changes: 17 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,25 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)

// allowEmptyValue
writer.WriteProperty(OpenApiConstants.AllowEmptyValue, AllowEmptyValue, false);

if (this.In == ParameterLocation.Query )
{
if (this.Style == ParameterStyle.Form && this.Explode == true)
{
writer.WriteProperty("collectionFormat", "multi");
}
else if (this.Style == ParameterStyle.PipeDelimited)
{
writer.WriteProperty("collectionFormat", "pipes");
}
else if (this.Style == ParameterStyle.PipeDelimited)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PipeDelimited [](start = 58, length = 13)

SpaceDelimited

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

{
writer.WriteProperty("collectionFormat", "ssv");
}
}
}


// extensions
writer.WriteExtensions(Extensions);

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,17 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
// authorizationUrl
// tokenUrl
// scopes
writer.WriteProperty(OpenApiConstants.Type, Type.ToString());
writer.WriteProperty(OpenApiConstants.Type, Type.GetDisplayName());
WriteOAuthFlowForV2(writer, Flows);
break;

case SecuritySchemeType.ApiKey:
// These properties apply to apiKey type only.
// name
// in
writer.WriteProperty(OpenApiConstants.Type, Type.ToString());
writer.WriteProperty(OpenApiConstants.Type, Type.GetDisplayName());
writer.WriteProperty(OpenApiConstants.Name, Name);
writer.WriteProperty(OpenApiConstants.In, In.ToString());
writer.WriteProperty(OpenApiConstants.In, In.GetDisplayName());
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void SerializeAsV2(IOpenApiWriter writer)

if (Reference != null)
{
Reference.SerializeAsV3(writer);
Reference.SerializeAsV2(writer);
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Models/SecuritySchemeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public enum SecuritySchemeType
/// Use OAuth2 with OpenId Connect URL to discover OAuth2 configuration value.
/// </summary>
[Display("openIdConnect")] OpenIdConnect

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit remove empty line

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public void LoadResponseReference()
{
Type = ReferenceType.Response,
Id = "NotFound"
},
Content = new Dictionary<string,OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType()
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public class OpenApiOperationTests
{
["200"] = new OpenApiResponse
{
Description = "Pet updated."
Description = "Pet updated.",
Content = new Dictionary<string,OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType(),
["application/xml"] = new OpenApiMediaType()
}
}
}
};
Expand Down Expand Up @@ -122,11 +127,22 @@ public class OpenApiOperationTests
{
["200"] = new OpenApiResponse
{
Description = "Pet updated."
Description = "Pet updated.",
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType(),
["application/xml"] = new OpenApiMediaType()
}

},
["405"] = new OpenApiResponse
{
Description = "Invalid input"
Description = "Invalid input",
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType(),
["application/xml"] = new OpenApiMediaType()
}
}
}
};
Expand Down Expand Up @@ -169,11 +185,22 @@ public class OpenApiOperationTests
{
["200"] = new OpenApiResponse
{
Description = "Pet updated."
Description = "Pet updated.",
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType(),
["application/xml"] = new OpenApiMediaType()
}
},
["405"] = new OpenApiResponse
{
Description = "Invalid input"
Description = "Invalid input",
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new OpenApiMediaType(),
["application/xml"] = new OpenApiMediaType()
}

}
},
};
Expand Down
Loading