Skip to content

Commit 8a73b54

Browse files
committed
fix: adds support for all component types
1 parent a231748 commit 8a73b54

File tree

2 files changed

+80
-20
lines changed

2 files changed

+80
-20
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

+54-8
Original file line numberDiff line numberDiff line change
@@ -588,19 +588,65 @@ public static ReadResult Parse(string input,
588588
return OpenApiModelFactory.Parse(input, format, settings);
589589
}
590590
/// <summary>
591-
/// Adds a schema to the components object of the current document.
591+
/// Adds a component to the components object of the current document and registers it to the underlying workspace.
592592
/// </summary>
593-
/// <param name="openApiSchema">The schema to add</param>
593+
/// <param name="componentToRegister">The component to add</param>
594594
/// <param name="id">The id for the component</param>
595-
/// <returns>Whether the schema was added to the components.</returns>
596-
public bool AddComponentSchema(string id, OpenApiSchema openApiSchema)
595+
/// <typeparam name="T">The type of the component</typeparam>
596+
/// <returns>Whether the component was added to the components.</returns>
597+
/// <exception cref="ArgumentNullException">Thrown when the component is null.</exception>
598+
/// <exception cref="ArgumentException">Thrown when the id is null or empty.</exception>
599+
public bool AddComponent<T>(string id, T componentToRegister)
597600
{
598-
Utils.CheckArgumentNull(openApiSchema);
601+
Utils.CheckArgumentNull(componentToRegister);
599602
Utils.CheckArgumentNullOrEmpty(id);
600603
Components ??= new();
601-
Components.Schemas ??= new Dictionary<string, OpenApiSchema>();
602-
Components.Schemas.Add(id, openApiSchema);
603-
return Workspace?.RegisterSchemaForDocument(this, openApiSchema, id) ?? false;
604+
switch (componentToRegister)
605+
{
606+
case OpenApiSchema openApiSchema:
607+
Components.Schemas ??= new Dictionary<string, OpenApiSchema>();
608+
Components.Schemas.Add(id, openApiSchema);
609+
break;
610+
case OpenApiParameter openApiParameter:
611+
Components.Parameters ??= new Dictionary<string, OpenApiParameter>();
612+
Components.Parameters.Add(id, openApiParameter);
613+
break;
614+
case OpenApiResponse openApiResponse:
615+
Components.Responses ??= new Dictionary<string, OpenApiResponse>();
616+
Components.Responses.Add(id, openApiResponse);
617+
break;
618+
case OpenApiRequestBody openApiRequestBody:
619+
Components.RequestBodies ??= new Dictionary<string, OpenApiRequestBody>();
620+
Components.RequestBodies.Add(id, openApiRequestBody);
621+
break;
622+
case OpenApiLink openApiLink:
623+
Components.Links ??= new Dictionary<string, OpenApiLink>();
624+
Components.Links.Add(id, openApiLink);
625+
break;
626+
case OpenApiCallback openApiCallback:
627+
Components.Callbacks ??= new Dictionary<string, OpenApiCallback>();
628+
Components.Callbacks.Add(id, openApiCallback);
629+
break;
630+
case OpenApiPathItem openApiPathItem:
631+
Components.PathItems ??= new Dictionary<string, OpenApiPathItem>();
632+
Components.PathItems.Add(id, openApiPathItem);
633+
break;
634+
case OpenApiExample openApiExample:
635+
Components.Examples ??= new Dictionary<string, OpenApiExample>();
636+
Components.Examples.Add(id, openApiExample);
637+
break;
638+
case OpenApiHeader openApiHeader:
639+
Components.Headers ??= new Dictionary<string, OpenApiHeader>();
640+
Components.Headers.Add(id, openApiHeader);
641+
break;
642+
case OpenApiSecurityScheme openApiSecurityScheme:
643+
Components.SecuritySchemes ??= new Dictionary<string, OpenApiSecurityScheme>();
644+
Components.SecuritySchemes.Add(id, openApiSecurityScheme);
645+
break;
646+
default:
647+
throw new ArgumentException($"Component type {componentToRegister!.GetType().Name} is not supported.");
648+
}
649+
return Workspace?.RegisterComponentForDocument(this, componentToRegister, id) ?? false;
604650
}
605651
}
606652

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

+26-12
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,40 @@ private string getBaseUri(OpenApiDocument openApiDocument)
145145
}
146146

147147
/// <summary>
148-
/// Registers a schema for a document in the workspace
148+
/// Registers a component for a document in the workspace
149149
/// </summary>
150-
/// <param name="openApiDocument">The document to register the schema for.</param>
151-
/// <param name="openApiSchema">The schema to register.</param>
152-
/// <param name="id">The id of the schema.</param>
153-
/// <returns>true if the schema is successfully registered; otherwise false.</returns>
150+
/// <param name="openApiDocument">The document to register the component for.</param>
151+
/// <param name="componentToRegister">The component to register.</param>
152+
/// <param name="id">The id of the component.</param>
153+
/// <typeparam name="T">The type of the component to register.</typeparam>
154+
/// <returns>true if the component is successfully registered; otherwise false.</returns>
154155
/// <exception cref="ArgumentNullException">openApiDocument is null</exception>
155-
/// <exception cref="ArgumentNullException">openApiSchema is null</exception>
156+
/// <exception cref="ArgumentNullException">componentToRegister is null</exception>
156157
/// <exception cref="ArgumentNullException">id is null or empty</exception>
157-
public bool RegisterSchemaForDocument(OpenApiDocument openApiDocument, OpenApiSchema openApiSchema, string id)
158+
public bool RegisterComponentForDocument<T>(OpenApiDocument openApiDocument, T componentToRegister, string id)
158159
{
159160
Utils.CheckArgumentNull(openApiDocument);
160-
Utils.CheckArgumentNull(openApiSchema);
161+
Utils.CheckArgumentNull(componentToRegister);
161162
Utils.CheckArgumentNullOrEmpty(id);
162163

163164
var baseUri = getBaseUri(openApiDocument);
164165

165-
var location = baseUri + ReferenceType.Schema.GetDisplayName() + ComponentSegmentSeparator + id;
166-
167-
return RegisterComponent(location, openApiSchema);
166+
var location = componentToRegister switch
167+
{
168+
OpenApiSchema => baseUri + ReferenceType.Schema.GetDisplayName() + ComponentSegmentSeparator + id,
169+
OpenApiParameter => baseUri + ReferenceType.Parameter.GetDisplayName() + ComponentSegmentSeparator + id,
170+
OpenApiResponse => baseUri + ReferenceType.Response.GetDisplayName() + ComponentSegmentSeparator + id,
171+
OpenApiRequestBody => baseUri + ReferenceType.RequestBody.GetDisplayName() + ComponentSegmentSeparator + id,
172+
OpenApiLink => baseUri + ReferenceType.Link.GetDisplayName() + ComponentSegmentSeparator + id,
173+
OpenApiCallback => baseUri + ReferenceType.Callback.GetDisplayName() + ComponentSegmentSeparator + id,
174+
OpenApiPathItem => baseUri + ReferenceType.PathItem.GetDisplayName() + ComponentSegmentSeparator + id,
175+
OpenApiExample => baseUri + ReferenceType.Example.GetDisplayName() + ComponentSegmentSeparator + id,
176+
OpenApiHeader => baseUri + ReferenceType.Header.GetDisplayName() + ComponentSegmentSeparator + id,
177+
OpenApiSecurityScheme => baseUri + ReferenceType.SecurityScheme.GetDisplayName() + ComponentSegmentSeparator + id,
178+
_ => throw new ArgumentException($"Invalid component type {componentToRegister.GetType().Name}"),
179+
};
180+
181+
return RegisterComponent(location, componentToRegister);
168182
}
169183

170184
/// <summary>
@@ -173,7 +187,7 @@ public bool RegisterSchemaForDocument(OpenApiDocument openApiDocument, OpenApiSc
173187
/// <param name="location"></param>
174188
/// <param name="component"></param>
175189
/// <returns>true if the component is successfully registered; otherwise false.</returns>
176-
public bool RegisterComponent<T>(string location, T component)
190+
internal bool RegisterComponent<T>(string location, T component)
177191
{
178192
var uri = ToLocationUrl(location);
179193
if (component is IOpenApiReferenceable referenceable)

0 commit comments

Comments
 (0)