in src/JetBrains.Space.Generator/CodeGeneration/CSharp/CodeGenerationContextEnricher.cs [11:50]
public static void AddRequestBodyTypesToDtos(CodeGenerationContext context) =>
AddRequestBodyTypesToDtos(context, context.ApiModel.Resources, string.Empty);
private static void AddRequestBodyTypesToDtos(CodeGenerationContext context, IEnumerable<ApiResource> resources, string parentResourcePath)
{
foreach (var apiResource in resources)
{
var resourcePath = (parentResourcePath.Length > 0
? parentResourcePath + "/" + apiResource.Path.Segments.ToPath()
: apiResource.Path.Segments.ToPath()).TrimEnd('/');
foreach (var apiEndpoint in apiResource.Endpoints)
{
if (apiEndpoint.RequestBody is ApiFieldType.Object { Kind: ApiFieldType.Object.ObjectKind.REQUEST_BODY } requestBody)
{
// Endpoint path
var endpointPath = (resourcePath + "/" + apiEndpoint.Path.Segments.ToPath()).TrimEnd('/');
// Expected class name
var typeNameForRequestBody = apiEndpoint.ToCSharpRequestBodyClassName(endpointPath)!;
var classIdForRequestBody = typeNameForRequestBody.ToLowerInvariant();
// See if we have seen the anonymous class before (and if not, add it)
if (!context.TryGetDto(classIdForRequestBody, out var requestBodyClass))
{
requestBodyClass = new ApiDto
{
Id = classIdForRequestBody,
Name = typeNameForRequestBody,
Fields = requestBody.Fields.Select(it => new ApiDtoField { Field = it }).ToList()
};
context.AddDto(classIdForRequestBody, requestBodyClass, isRequestBodyDto: true);
}
}
}
AddRequestBodyTypesToDtos(context, apiResource.NestedResources, resourcePath);
}
}