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

Commit 18af6e2

Browse files
Fix Extractor API Content Type Json array (#553)
Co-authored-by: RupengLiu <rliu1211@terpmail.umd.edu>
1 parent 1b3222b commit 18af6e2

File tree

1 file changed

+34
-18
lines changed
  • src/APIM_ARMTemplate/apimtemplate/Extractor/EntityExtractors

1 file changed

+34
-18
lines changed

src/APIM_ARMTemplate/apimtemplate/Extractor/EntityExtractors/APIExtractor.cs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public async Task<List<TemplateResource>> GenerateSingleAPIResourceAsync(string
246246
{
247247
apiResource.properties.serviceUrl = $"[parameters('{ParameterNames.ServiceUrl}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}]";
248248
}
249-
249+
250250
if (apiResource.properties.apiVersionSetId != null)
251251
{
252252
apiResource.dependsOn = new string[] { };
@@ -334,7 +334,7 @@ public async Task<List<TemplateResource>> GenerateCurrentRevisionAPIResourceAsyn
334334
{
335335
apiResource.properties.serviceUrl = $"[parameters('{ParameterNames.ServiceUrl}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}]";
336336
}
337-
337+
338338
if (apiResource.properties.apiVersionSetId != null)
339339
{
340340
apiResource.dependsOn = new string[] { };
@@ -410,12 +410,21 @@ public async Task<Template> GenerateAPIsARMTemplateAsync(string singleApiName, L
410410

411411
private static void ArmEscapeSampleValueIfNecessary(OperationTemplateRepresentation operationTemplateRepresentation)
412412
{
413-
if (!string.IsNullOrWhiteSpace(operationTemplateRepresentation.sample) && operationTemplateRepresentation.contentType?.Contains("application/json", StringComparison.OrdinalIgnoreCase) == true && operationTemplateRepresentation.sample.TryParseJson(out JToken sampleAsJToken) && sampleAsJToken.Type == JTokenType.Array)
413+
414+
if (!string.IsNullOrWhiteSpace(operationTemplateRepresentation.sample) &&
415+
ContentTypes().Contains(operationTemplateRepresentation.contentType?.ToLower()) &&
416+
operationTemplateRepresentation.sample.TryParseJson(out JToken sampleAsJToken) &&
417+
sampleAsJToken.Type == JTokenType.Array)
414418
{
415419
operationTemplateRepresentation.sample = "[" + operationTemplateRepresentation.sample;
416420
}
417421
}
418422

423+
private static string[] ContentTypes()
424+
{
425+
return new string[] { "application/json", "text/json", "application/*+json" };
426+
}
427+
419428
private static void AddSchemaDependencyToOperationIfNecessary(string apiName, List<string> operationDependsOn, OperationTemplateRepresentation operationTemplateRepresentation)
420429
{
421430
if (operationTemplateRepresentation.schemaId != null)
@@ -528,23 +537,25 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
528537
int numBatches = 0;
529538

530539
// create empty array for the batch operation owners
531-
List<string> batchOwners = new List<string>();
540+
List<string> batchOwners = new List<string>();
532541

533542
// if a batch size is specified
534-
if(exc.operationBatchSize > 0) {
543+
if (exc.operationBatchSize > 0)
544+
{
535545
// store the number of batches required based on exc.operationBatchSize
536-
numBatches = (int)Math.Ceiling((double)operationNames.Length/ (double)exc.operationBatchSize);
546+
numBatches = (int)Math.Ceiling((double)operationNames.Length / (double)exc.operationBatchSize);
537547
//Console.WriteLine ("Number of batches: {0}", numBatches);
538548
}
539-
549+
540550

541551
foreach (string operationName in operationNames)
542552
{
543553
int opIndex = Array.IndexOf(operationNames, operationName);
544554

545555
//add batch owners into array
546556
// ensure each owner is linked to the one before
547-
if(exc.operationBatchSize > 0 && opIndex < numBatches) {
557+
if (exc.operationBatchSize > 0 && opIndex < numBatches)
558+
{
548559
batchOwners.Add(operationName);
549560
//Console.WriteLine("Adding operation {0} to owner list", operationName);
550561
}
@@ -580,18 +591,22 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
580591
// add to batch if flagged
581592
string batchdependsOn;
582593

583-
if(exc.operationBatchSize > 0 && opIndex > 0) {
584-
if(opIndex >= 1 && opIndex <= numBatches - 1) {
594+
if (exc.operationBatchSize > 0 && opIndex > 0)
595+
{
596+
if (opIndex >= 1 && opIndex <= numBatches - 1)
597+
{
585598
// chain the owners to each other
586599
batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[opIndex - 1]}')]";
587600
//Console.WriteLine("Owner chaining: this request {0} to previous {1}", operationName, batchOwners[opIndex-1]);
588-
} else {
601+
}
602+
else
603+
{
589604
// chain the operation to respective owner
590-
int ownerIndex = (int)Math.Floor((opIndex - numBatches)/((double)exc.operationBatchSize-1));
605+
int ownerIndex = (int)Math.Floor((opIndex - numBatches) / ((double)exc.operationBatchSize - 1));
591606
batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[ownerIndex]}')]";
592607
//Console.WriteLine("Operation {0} chained to owner {1}", operationName, batchOwners[ownerIndex]);
593608
}
594-
609+
595610
operationDependsOn.Add(batchdependsOn);
596611
}
597612

@@ -693,7 +708,7 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
693708
catch (Exception) { }
694709
#endregion
695710

696-
#region API Tags
711+
#region API Tags
697712
// add tags associated with the api to template
698713
try
699714
{
@@ -716,9 +731,9 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
716731
}
717732
}
718733
catch (Exception) { }
719-
#endregion
734+
#endregion
735+
720736

721-
722737
// add product api associations to template
723738
#region API Products
724739
try
@@ -764,8 +779,9 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
764779
diagnosticResource.scale = null;
765780
diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
766781

767-
if (exc.paramApiLoggerId) {
768-
782+
if (exc.paramApiLoggerId)
783+
{
784+
769785
diagnosticResource.properties.loggerId = $"[parameters('{ParameterNames.ApiLoggerId}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}.{ExtractorUtils.GenValidParamName(diagnosticName, ParameterPrefix.Diagnostic)}]";
770786
}
771787

0 commit comments

Comments
 (0)