Skip to content

Commit

Permalink
Collect action request body parameter so it will be added to the gene…
Browse files Browse the repository at this point in the history
…rated controller base class for reflection targets
  • Loading branch information
C0nquistadore committed May 25, 2024
1 parent efe35bb commit da0d247
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ protected ActionTargetDefinitionResolver(ISchemaRegistry schemaRegistry, ILogger
#endregion

#region Abstract Methods
public abstract bool TryResolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> readOnlyDictionary, ICollection<string> bodyParameters, out T actionTargetDefinition) where T : ActionTargetDefinition, new();
public abstract bool TryResolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> readOnlyDictionary, ICollection<string> bodyParameters, ActionRequestBody requestBody, out T actionTargetDefinition) where T : ActionTargetDefinition, new();
#endregion

#region Protected Methods
protected T CreateActionTargetDefinition<T>(ActionTarget actionTarget, IReadOnlyDictionary<string, PathParameter> pathParameters) where T : ActionTargetDefinition, new()
protected T CreateActionTargetDefinition<T>(ActionTarget actionTarget, IReadOnlyDictionary<string, PathParameter> pathParameters, ActionRequestBody requestBody) where T : ActionTargetDefinition, new()
{
T actionTargetDefinition = new T();
actionTargetDefinition.Target = actionTarget;
actionTargetDefinition.PathParameters.AddRange(pathParameters);
if (requestBody?.Contract != null)
actionTargetDefinition.Parameters.Add(new ActionParameter("body", "body", requestBody.Contract, ActionParameterLocation.Body, isRequired: true, isOutput: false, defaultValue: null, sourceLocation: requestBody.Contract.Location, source: null));

return actionTargetDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ string productName
};
}

public T Resolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters) where T : ActionTargetDefinition, new()
public T Resolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, ActionRequestBody requestBody) where T : ActionTargetDefinition, new()
{
foreach (ActionTargetDefinitionResolver resolver in this._resolvers)
{
if (resolver.TryResolve(targetName, sourceLocation, explicitParameters, pathParameters, bodyParameters, out T definition))
if (resolver.TryResolve(targetName, sourceLocation, explicitParameters, pathParameters, bodyParameters, requestBody, out T definition))
return definition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ExternalReflectionActionTargetDefinitionResolver(ISchemaRegistry schemaRe
this._lockEntryManager = lockEntryManager;
}

public override bool TryResolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, out T actionTargetDefinition)
public override bool TryResolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, ActionRequestBody requestBody, out T actionTargetDefinition)
{
string[] parts = targetName.Split(',');
if (parts.Length != 2)
Expand Down Expand Up @@ -57,7 +57,7 @@ public override bool TryResolve<T>(string targetName, SourceLocation sourceLocat
*/

ActionTarget actionTarget = new ReflectionActionTarget(assemblyName, typeName, methodName, isAsync: false, hasRefParameters: false, sourceLocation);
actionTargetDefinition = CreateActionTargetDefinition<T>(actionTarget, pathParameters);
actionTargetDefinition = CreateActionTargetDefinition<T>(actionTarget, pathParameters, requestBody);
ActionParameterRegistry parameterRegistry = new ActionParameterRegistry(actionTargetDefinition, pathParameters);
foreach (ExplicitParameter parameter in explicitParameters.Values)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Dibix.Sdk.CodeGeneration
{
internal interface IActionTargetDefinitionResolverFacade
{
T Resolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters) where T : ActionTargetDefinition, new();
T Resolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, ActionRequestBody requestBody) where T : ActionTargetDefinition, new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ string productName
#endregion

#region Overrides
public override bool TryResolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, out T actionTargetDefinition)
public override bool TryResolve<T>(string targetName, SourceLocation sourceLocation, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, ActionRequestBody requestBody, out T actionTargetDefinition)
{
if (!TryGetStatementDefinitionByProbing(targetName, out SqlStatementDefinition statementDefinition))
{
Expand All @@ -47,7 +47,7 @@ public override bool TryResolve<T>(string targetName, SourceLocation sourceLocat
bool isAsync = statementDefinition.Async;
bool hasRefParameters = statementDefinition.Parameters.Any(x => x.IsOutput);
ActionTarget actionTarget = new LocalActionTarget(statementDefinition, localAccessorFullName, externalAccessorFullName, definitionName, isAsync, hasRefParameters, sourceLocation);
actionTargetDefinition = CreateActionTargetDefinition<T>(actionTarget, pathParameters);
actionTargetDefinition = CreateActionTargetDefinition<T>(actionTarget, pathParameters, requestBody);
ActionParameterRegistry parameterRegistry = new ActionParameterRegistry(actionTargetDefinition, pathParameters);
foreach (SqlQueryParameter parameter in statementDefinition.Parameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void AddMethods(CSharpClass @class, CodeGenerationContext context
CSharpMethod method;
if (isImplementation)
{
ICollection<string> parameterNames = parameters.Select(x => x.InternalParameterName).ToList();
IList<string> parameterNames = parameters.Select(x => x.InternalParameterName).ToList();
parameterNames.Add("cancellationToken");
string body = $"return {methodImplementationName}({String.Join(", ", parameterNames)});";
method = @class.AddMethod(name: methodName, returnType: returnTypeName, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void ReadControllerAction(ControllerDefinition controller, JObject actio
IReadOnlyDictionary<string, ExplicitParameter> explicitParameters = CollectExplicitParameters(action, requestBody, pathParameters);

// Resolve action target, parameters and create action definition
ActionDefinition actionDefinition = CreateActionDefinition<ActionDefinition>(action, explicitParameters, pathParameters, bodyParameters);
ActionDefinition actionDefinition = CreateActionDefinition<ActionDefinition>(action, explicitParameters, pathParameters, bodyParameters, requestBody);
if (actionDefinition == null)
return;

Expand Down Expand Up @@ -531,7 +531,7 @@ private void CollectAuthorization(JProperty templateProperty, JObject authorizat

IReadOnlyDictionary<string, ExplicitParameter> explicitParameters = CollectExplicitParameters(authorization, requestBody: null, pathParameters);
ICollection<string> bodyParameters = new Collection<string>();
actionDefinition.Authorization = CreateActionDefinition<AuthorizationBehavior>(authorization, explicitParameters, pathParameters, bodyParameters);
actionDefinition.Authorization = CreateActionDefinition<AuthorizationBehavior>(authorization, explicitParameters, pathParameters, bodyParameters, requestBody: null);
}

private JObject ApplyAuthorizationTemplate(JProperty templateNameProperty, JObject authorizationTemplateReference)
Expand Down Expand Up @@ -564,11 +564,11 @@ private JObject ApplyAuthorizationTemplate(JProperty templateNameProperty, JObje
return mergedAuthorization;
}

private T CreateActionDefinition<T>(JObject action, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters) where T : ActionTargetDefinition, new()
private T CreateActionDefinition<T>(JObject action, IReadOnlyDictionary<string, ExplicitParameter> explicitParameters, IReadOnlyDictionary<string, PathParameter> pathParameters, ICollection<string> bodyParameters, ActionRequestBody requestBody) where T : ActionTargetDefinition, new()
{
JValue targetValue = (JValue)action.Property("target").Value;
SourceLocation sourceInfo = targetValue.GetSourceInfo();
T actionDefinition = _actionTargetResolver.Resolve<T>(targetName: (string)targetValue, sourceInfo, explicitParameters, pathParameters, bodyParameters);
T actionDefinition = _actionTargetResolver.Resolve<T>(targetName: (string)targetValue, sourceInfo, explicitParameters, pathParameters, bodyParameters, requestBody);
return actionDefinition;
}
#endregion
Expand Down

0 comments on commit da0d247

Please sign in to comment.