Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 4636be8

Browse files
f-alizadaFarhad Alizada
andauthored
Remove overriding document's value property (#770)
Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent 42c94e7 commit 4636be8

File tree

4 files changed

+166
-32
lines changed

4 files changed

+166
-32
lines changed

src/ArmTemplates/Extractor/EntityExtractors/ApiSchemaExtractor.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
88
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
9-
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
109
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.ApiSchemas;
1110
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors.Abstractions;
1211
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
@@ -36,7 +35,6 @@ public async Task<ApiSchemaTemplateResources> GenerateApiSchemaResourcesAsync(st
3635
{
3736
var apiSchemaOriginalName = apiSchema.Name;
3837

39-
apiSchema.Properties.Document.Value = this.GetSchemaValueBasedOnContentType(apiSchema.Properties);
4038
apiSchema.Name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{apiSchemaOriginalName}')]";
4139
apiSchema.Type = ResourceTypeConstants.APISchema;
4240
apiSchema.ApiVersion = GlobalConstants.ApiVersion;
@@ -47,35 +45,5 @@ public async Task<ApiSchemaTemplateResources> GenerateApiSchemaResourcesAsync(st
4745

4846
return apiSchemaResources;
4947
}
50-
51-
string GetSchemaValueBasedOnContentType(ApiSchemaProperties schemaTemplateProperties)
52-
{
53-
if (schemaTemplateProperties is null)
54-
{
55-
return string.Empty;
56-
}
57-
58-
var contentType = schemaTemplateProperties.ContentType.ToLowerInvariant();
59-
if (contentType.Equals("application/vnd.oai.openapi.components+json"))
60-
{
61-
// for OpenAPI "value" is not used, but "components" which is resolved during json deserialization
62-
return null;
63-
}
64-
65-
var schemaValue = contentType switch
66-
{
67-
"application/vnd.ms-azure-apim.swagger.definitions+json" => schemaTemplateProperties?.Document?.Definitions?.Serialize(),
68-
"application/vnd.ms-azure-apim.xsd+xml" => schemaTemplateProperties?.Document?.Definitions?.Serialize(),
69-
"application/vnd.ms-azure-apim.graphql.schema" => schemaTemplateProperties?.Document?.Value?.ToString(),
70-
_ => string.Empty
71-
};
72-
73-
if (string.IsNullOrEmpty(schemaValue) && schemaTemplateProperties.Document is not null)
74-
{
75-
return schemaTemplateProperties.Document.Serialize();
76-
}
77-
78-
return schemaValue;
79-
}
8048
}
8149
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// --------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License.
4+
// --------------------------------------------------------------------------
5+
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using FluentAssertions;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
11+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
12+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Extractor.Abstractions;
14+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Moqs.ApiClients;
15+
using Xunit;
16+
17+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Extractor.Scenarios
18+
{
19+
[Trait("Category", "Api Schema Extraction")]
20+
public class ApiSchemaExtractorTests : ExtractorMockerWithOutputTestsBase
21+
{
22+
public ApiSchemaExtractorTests() : base("api-schema-tests")
23+
{
24+
}
25+
26+
[Fact]
27+
public async Task GenerateApiTemplates_ProperlyParsesTheInformation()
28+
{
29+
// arrange
30+
var responseFileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListApiSchemas_success_response.json");
31+
32+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration();
33+
var extractorParameters = new ExtractorParameters(extractorConfig);
34+
35+
var mockedApiSchemaClient = await MockApiSchemaClient.GetMockedHttpApiSchemaClient(responseFileLocation);
36+
var mockedApiSchemaExtractor = new ApiSchemaExtractor(this.GetTestLogger<ApiSchemaExtractor>(), mockedApiSchemaClient);
37+
38+
// act
39+
var apiSchemas = await mockedApiSchemaExtractor.GenerateApiSchemaResourcesAsync("apiName", extractorParameters);
40+
41+
// assert
42+
apiSchemas.ApiSchemas.Count().Should().Be(2);
43+
apiSchemas.ApiSchemas.All(x => x.Type == ResourceTypeConstants.APISchema).Should().BeTrue();
44+
apiSchemas.ApiSchemas.All(x => x.Properties is not null).Should().BeTrue();
45+
46+
var jsonSchema = apiSchemas.ApiSchemas.First(x=>x.Name.Contains("schemaName1"));
47+
jsonSchema.Should().NotBeNull();
48+
jsonSchema.Properties.Document.Value.Should().BeNull();
49+
50+
var xmlSchema = apiSchemas.ApiSchemas.First(x => x.Name.Contains("schemaName2"));
51+
xmlSchema.Should().NotBeNull();
52+
xmlSchema.Properties.Document.Value.Should().NotBeNull();
53+
xmlSchema.Properties.Document.Definitions.Should().BeNull();
54+
xmlSchema.Properties.Document.Components.Should().BeNull();
55+
}
56+
}
57+
}

tests/ArmTemplates.Tests/Moqs/ApiClients/MockApiSchemaClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
using System.IO;
88
using System.Threading.Tasks;
99
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.ApiSchemas;
1011
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.FileHandlers;
1112
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.ApiSchemas;
1213
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
14+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Utilities;
1315
using Moq;
16+
using Moq.Protected;
1417

1518
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Moqs.ApiClients
1619
{
@@ -76,5 +79,14 @@ public static IApiSchemaClient GetMockedApiClientWithGraphQLSchemaValues()
7679

7780
return mockApiSchemaClient.Object;
7881
}
82+
83+
public static async Task<IApiSchemaClient> GetMockedHttpApiSchemaClient(string responseFileLocation)
84+
{
85+
var mockedClient= new Mock<ApiSchemaClient>(MockBehavior.Strict, await MockClientUtils.GenerateMockedIHttpClientFactoryWithResponse(responseFileLocation));
86+
mockedClient.Protected()
87+
.Setup<AzureCliAuthenticator>("Auth").Returns(MockClientUtils.GetMockedAzureClient());
88+
89+
return mockedClient.Object;
90+
}
7991
}
8092
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"value": [
3+
{
4+
"id": "/subscriptions/subid/resourceGroups/rg/providers/Microsoft.ApiManagement/service/service-name/apis/apiId/schemas/schemaId1",
5+
"type": "Microsoft.ApiManagement/service/apis/schemas",
6+
"name": "schemaName1",
7+
"properties": {
8+
"contentType": "application/vnd.ms-azure-apim.swagger.definitions+json",
9+
"document": {
10+
"definitions": {
11+
"Pet": {
12+
"type": "object",
13+
"allOf": [
14+
{
15+
"$ref": "#/definitions/NewPet"
16+
},
17+
{
18+
"required": [
19+
"id"
20+
],
21+
"properties": {
22+
"id": {
23+
"type": "integer",
24+
"format": "int64"
25+
}
26+
}
27+
}
28+
]
29+
},
30+
"NewPet": {
31+
"type": "object",
32+
"required": [
33+
"name"
34+
],
35+
"properties": {
36+
"name": {
37+
"type": "string"
38+
},
39+
"tag": {
40+
"type": "string"
41+
}
42+
}
43+
},
44+
"Error": {
45+
"type": "object",
46+
"required": [
47+
"code",
48+
"message"
49+
],
50+
"properties": {
51+
"code": {
52+
"type": "integer",
53+
"format": "int32"
54+
},
55+
"message": {
56+
"type": "string"
57+
}
58+
}
59+
},
60+
"Tags": {
61+
"items": {
62+
"type": "string"
63+
},
64+
"x-apim-inline": true
65+
},
66+
"Limit": {
67+
"format": "int32",
68+
"x-apim-inline": true
69+
},
70+
"PetArray": {
71+
"type": "array",
72+
"items": {
73+
"$ref": "#/definitions/Pet"
74+
}
75+
},
76+
"Id": {
77+
"format": "int64",
78+
"x-apim-inline": true
79+
}
80+
}
81+
}
82+
}
83+
},
84+
{
85+
"id": "/subscriptions/subid/resourceGroups/rg/providers/Microsoft.ApiManagement/service/service-name/apis/apiId/schemas/schemaId2",
86+
"type": "Microsoft.ApiManagement/service/apis/schemas",
87+
"name": "schemaName2",
88+
"properties": {
89+
"contentType": "application/vnd.ms-azure-apim.xsd+xml",
90+
"document": {
91+
"value": "<s:schema elementFormDefault=\"qualified\" targetNamespace=\"http://ws.cdyne.com/WeatherWS/\" xmlns:tns=\"http://ws.cdyne.com/WeatherWS/\" xmlns:s=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tm=\"http://microsoft.com/wsdl/mime/textMatching/\" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:apim-wsdltns=\"http://ws.cdyne.com/WeatherWS/\">\r\n <s:element name=\"GetWeatherInformation\">\r\n <s:complexType />\r\n </s:element>\r\n <s:element name=\"GetWeatherInformationResponse\">\r\n <s:complexType>\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"GetWeatherInformationResult\" type=\"tns:ArrayOfWeatherDescription\" />\r\n </s:sequence>\r\n </s:complexType>\r\n </s:element>\r\n <s:complexType name=\"ArrayOfWeatherDescription\">\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"WeatherDescription\" type=\"tns:WeatherDescription\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:complexType name=\"WeatherDescription\">\r\n <s:sequence>\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"WeatherID\" type=\"s:short\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Description\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"PictureURL\" type=\"s:string\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:element name=\"GetCityForecastByZIP\">\r\n <s:complexType>\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ZIP\" type=\"s:string\" />\r\n </s:sequence>\r\n </s:complexType>\r\n </s:element>\r\n <s:element name=\"GetCityForecastByZIPResponse\">\r\n <s:complexType>\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"GetCityForecastByZIPResult\" type=\"tns:ForecastReturn\" />\r\n </s:sequence>\r\n </s:complexType>\r\n </s:element>\r\n <s:complexType name=\"ForecastReturn\">\r\n <s:sequence>\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Success\" type=\"s:boolean\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ResponseText\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"State\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"City\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"WeatherStationCity\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ForecastResult\" type=\"tns:ArrayOfForecast\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:complexType name=\"ArrayOfForecast\">\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"Forecast\" nillable=\"true\" type=\"tns:Forecast\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:complexType name=\"Forecast\">\r\n <s:sequence>\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Date\" type=\"s:dateTime\" />\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"WeatherID\" type=\"s:short\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Desciption\" type=\"s:string\" />\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Temperatures\" type=\"tns:temp\" />\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"ProbabilityOfPrecipiation\" type=\"tns:POP\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:complexType name=\"temp\">\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"MorningLow\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"DaytimeHigh\" type=\"s:string\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:complexType name=\"POP\">\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Nighttime\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Daytime\" type=\"s:string\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:element name=\"GetCityWeatherByZIP\">\r\n <s:complexType>\r\n <s:sequence>\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ZIP\" type=\"s:string\" />\r\n </s:sequence>\r\n </s:complexType>\r\n </s:element>\r\n <s:element name=\"GetCityWeatherByZIPResponse\">\r\n <s:complexType>\r\n <s:sequence>\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"GetCityWeatherByZIPResult\" type=\"tns:WeatherReturn\" />\r\n </s:sequence>\r\n </s:complexType>\r\n </s:element>\r\n <s:complexType name=\"WeatherReturn\">\r\n <s:sequence>\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"Success\" type=\"s:boolean\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"ResponseText\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"State\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"City\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"WeatherStationCity\" type=\"s:string\" />\r\n <s:element minOccurs=\"1\" maxOccurs=\"1\" name=\"WeatherID\" type=\"s:short\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Description\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Temperature\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"RelativeHumidity\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Wind\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Pressure\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Visibility\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"WindChill\" type=\"s:string\" />\r\n <s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"Remarks\" type=\"s:string\" />\r\n </s:sequence>\r\n </s:complexType>\r\n <s:element name=\"ArrayOfWeatherDescription\" nillable=\"true\" type=\"tns:ArrayOfWeatherDescription\" />\r\n <s:element name=\"ForecastReturn\" nillable=\"true\" type=\"tns:ForecastReturn\" />\r\n <s:element name=\"WeatherReturn\" type=\"tns:WeatherReturn\" />\r\n</s:schema>"
92+
}
93+
}
94+
}
95+
],
96+
"count": 2
97+
}

0 commit comments

Comments
 (0)