Skip to content
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

Add license SPDX identifier #1042

Merged
merged 2 commits into from
Oct 18, 2022
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 @@ -22,6 +22,12 @@ internal static partial class OpenApiV3Deserializer
o.Name = n.GetScalarValue();
}
},
{
"identifier", (o, n) =>
{
o.Identifier = n.GetScalarValue();
}
},
{
"url", (o, n) =>
{
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public static class OpenApiConstants
/// </summary>
public const string Name = "name";

/// <summary>
/// Field: Identifier
/// </summary>
public const string Identifier = "identifier";

/// <summary>
/// Field: Namespace
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiLicense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class OpenApiLicense : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public string Name { get; set; }

/// <summary>
/// An SPDX license expression for the API. The identifier field is mutually exclusive of the url field.
/// </summary>
public string Identifier { get; set; }

/// <summary>
/// The URL pointing to the contact information. MUST be in the format of a URL.
/// </summary>
Expand All @@ -40,6 +45,7 @@ public OpenApiLicense() {}
public OpenApiLicense(OpenApiLicense license)
{
Name = license?.Name ?? Name;
Identifier = license?.Identifier ?? Identifier;
Url = license?.Url != null ? new Uri(license.Url.OriginalString) : null;
Extensions = license?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(license.Extensions) : null;
}
Expand Down Expand Up @@ -72,6 +78,9 @@ private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion
// name
writer.WriteProperty(OpenApiConstants.Name, Name);

// identifier
writer.WriteProperty(OpenApiConstants.Identifier, Identifier);

// url
writer.WriteProperty(OpenApiConstants.Url, Url?.OriginalString);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
Expand Down Expand Up @@ -164,6 +164,9 @@
<EmbeddedResource Include="V3Tests\Samples\OpenApiInfo\basicInfo.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V3Tests\Samples\OpenApiLicense\licenseWithSpdxIdentifier.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V3Tests\Samples\OpenApiInfo\minimalInfo.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V3;
using SharpYaml.Serialization;
using System.IO;
using Xunit;
using System.Linq;
using FluentAssertions;

namespace Microsoft.OpenApi.Readers.Tests.V3Tests
{

public class OpenApiLicenseTests
{
private const string SampleFolderPath = "V3Tests/Samples/OpenApiLicense/";

[Fact]
public void ParseLicenseWithSpdxIdentifierShouldSucceed()
{
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "licenseWithSpdxIdentifier.yaml"));
var yamlStream = new YamlStream();
yamlStream.Load(new StreamReader(stream));
var yamlNode = yamlStream.Documents.First().RootNode;

var diagnostic = new OpenApiDiagnostic();
var context = new ParsingContext(diagnostic);

var node = new MapNode(context, (YamlMappingNode)yamlNode);

// Act
var license = OpenApiV3Deserializer.LoadLicense(node);

// Assert
license.Should().BeEquivalentTo(
new OpenApiLicense
{
Name = "Apache 2.0",
Identifier = "Apache-2.0"
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: Apache 2.0
identifier: Apache-2.0
37 changes: 37 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiLicenseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class OpenApiLicenseTests
}
};

public static OpenApiLicense LicenseWithIdentifier = new OpenApiLicense
{
Name = "Apache 2.0",
Identifier = "Apache-2.0"
};

[Theory]
[InlineData(OpenApiSpecVersion.OpenApi3_0)]
[InlineData(OpenApiSpecVersion.OpenApi2_0)]
Expand Down Expand Up @@ -123,5 +129,36 @@ public void ShouldCopyFromOriginalObjectWithoutMutating()
Assert.NotEqual(AdvanceLicense.Name, licenseCopy.Name);
Assert.NotEqual(AdvanceLicense.Url, licenseCopy.Url);
}

[Fact]
public void SerializeLicenseWithIdentifierAsJsonWorks()
{
// Arrange
var expected =
@"{
""name"": ""Apache 2.0"",
""identifier"": ""Apache-2.0""
}";

// Act
var actual = LicenseWithIdentifier.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

// Assert
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), actual.MakeLineBreaksEnvironmentNeutral());
}

[Fact]
public void SerializeLicenseWithIdentifierAsYamlWorks()
{
// Arrange
var expected = @"name: Apache 2.0
identifier: Apache-2.0";

// Act
var actual = LicenseWithIdentifier.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);

// Assert
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), actual.MakeLineBreaksEnvironmentNeutral());
}
}
}