Skip to content

Commit 41fd508

Browse files
committed
fix: refactor to avoid adding duplicate entries
1 parent 9095321 commit 41fd508

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -665,47 +665,52 @@ public bool AddComponent<T>(string id, T componentToRegister)
665665
Utils.CheckArgumentNull(componentToRegister);
666666
Utils.CheckArgumentNullOrEmpty(id);
667667
Components ??= new();
668+
669+
static Dictionary<string, TValue> AddToDictionary<TValue>(Dictionary<string, TValue>? dict, string key, TValue value)
670+
{
671+
dict ??= new Dictionary<string, TValue>();
672+
#if NET5_0_OR_GREATER
673+
dict.TryAdd(key, value);
674+
#else
675+
if (!dict.ContainsKey(key))
676+
{
677+
dict.Add(key, value);
678+
}
679+
#endif
680+
return dict;
681+
}
682+
668683
switch (componentToRegister)
669684
{
670685
case IOpenApiSchema openApiSchema:
671-
Components.Schemas ??= [];
672-
Components.Schemas.Add(id, openApiSchema);
686+
Components.Schemas = AddToDictionary(Components.Schemas, id, openApiSchema);
673687
break;
674688
case IOpenApiParameter openApiParameter:
675-
Components.Parameters ??= [];
676-
Components.Parameters.Add(id, openApiParameter);
689+
Components.Parameters = AddToDictionary(Components.Parameters, id, openApiParameter);
677690
break;
678691
case IOpenApiResponse openApiResponse:
679-
Components.Responses ??= [];
680-
Components.Responses.Add(id, openApiResponse);
692+
Components.Responses = AddToDictionary(Components.Responses, id, openApiResponse);
681693
break;
682694
case IOpenApiRequestBody openApiRequestBody:
683-
Components.RequestBodies ??= [];
684-
Components.RequestBodies.Add(id, openApiRequestBody);
695+
Components.RequestBodies = AddToDictionary(Components.RequestBodies, id, openApiRequestBody);
685696
break;
686697
case IOpenApiLink openApiLink:
687-
Components.Links ??= [];
688-
Components.Links.Add(id, openApiLink);
698+
Components.Links = AddToDictionary(Components.Links, id, openApiLink);
689699
break;
690700
case IOpenApiCallback openApiCallback:
691-
Components.Callbacks ??= [];
692-
Components.Callbacks.Add(id, openApiCallback);
701+
Components.Callbacks = AddToDictionary(Components.Callbacks, id, openApiCallback);
693702
break;
694703
case IOpenApiPathItem openApiPathItem:
695-
Components.PathItems ??= [];
696-
Components.PathItems.Add(id, openApiPathItem);
704+
Components.PathItems = AddToDictionary(Components.PathItems, id, openApiPathItem);
697705
break;
698706
case IOpenApiExample openApiExample:
699-
Components.Examples ??= [];
700-
Components.Examples.Add(id, openApiExample);
707+
Components.Examples = AddToDictionary(Components.Examples, id, openApiExample);
701708
break;
702709
case IOpenApiHeader openApiHeader:
703-
Components.Headers ??= [];
704-
Components.Headers.Add(id, openApiHeader);
710+
Components.Headers = AddToDictionary(Components.Headers, id, openApiHeader);
705711
break;
706712
case IOpenApiSecurityScheme openApiSecurityScheme:
707-
Components.SecuritySchemes ??= [];
708-
Components.SecuritySchemes.Add(id, openApiSecurityScheme);
713+
Components.SecuritySchemes = AddToDictionary(Components.SecuritySchemes, id, openApiSecurityScheme);
709714
break;
710715
default:
711716
throw new ArgumentException($"Component type {componentToRegister!.GetType().Name} is not supported.");

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,7 @@ public async Task SerializesDoubleHopeReferences()
12701270
Description = "A reference to a pet"
12711271
};
12721272
document.AddComponent("PetReference", petSchemaReference);
1273+
document.AddComponent("Pet", petSchema);
12731274
document.Paths.Add("/pets", new OpenApiPathItem
12741275
{
12751276
Operations = new()

0 commit comments

Comments
 (0)