public static IEnumerable <ILocatedOpenApiElement <OpenApiSchema> > GetAllSchemas( this ILocatedOpenApiElement <OpenApiOperation> operation) { var requestBody = operation.GetRequestBody(); if (requestBody is not null && !requestBody.IsReference()) { var requestSchemas = requestBody .GetMediaTypes() .Select(p => p.GetSchema()) .Where(p => p is not null && !p.IsReference()) .SelectMany(p => p !.GetAllSchemas()); foreach (var schema in requestSchemas) { yield return(schema); } } foreach (var responseSchema in operation .GetResponseSet() .GetResponses() .Where(p => !p.IsReference()) .GetAllSchemas()) { yield return(responseSchema); } }
protected virtual ExpressionSyntax GenerateResponse( ILocatedOpenApiElement <OpenApiOperation> operation, ExpressionSyntax responseMessage) => SwitchExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, responseMessage, IdentifierName("StatusCode")), SeparatedList(operation .GetResponseSet() .GetResponses() .Select(p => SwitchExpressionArm( ConstantPattern(ParseStatusCode(p.Key)), ObjectCreationExpression( Context.TypeNameProvider.GetName(p)) .AddArgumentListArguments( Argument(IdentifierName("responseMessage")), Argument(IdentifierName(TagTypeGenerator.TypeSerializerRegistryFieldName))))))) .AddArms(SwitchExpressionArm(DiscardPattern(), ObjectCreationExpression( Context.TypeNameProvider.GetName(operation.GetResponseSet().GetUnknownResponse())) .AddArgumentListArguments( Argument(IdentifierName("responseMessage")), Argument(IdentifierName(TagTypeGenerator.TypeSerializerRegistryFieldName)))));
protected virtual IEnumerable <StatementSyntax> GenerateStatements( ILocatedOpenApiElement <OpenApiOperation> operation) { ILocatedOpenApiElement <OpenApiResponses> responseSet = operation.GetResponseSet(); ILocatedOpenApiElement <OpenApiResponse> primaryResponse = responseSet .GetResponses() .OrderBy(p => p.Key) .First(); ILocatedOpenApiElement <OpenApiMediaType>?mediaType = MediaTypeSelector.Select(primaryResponse); if (mediaType != null) { yield return(ExpressionStatement(InvocationExpression( SyntaxHelpers.MemberAccess(RequestMessageParameterName, "Headers", "Accept", "Add")) .AddArgumentListArguments( Argument(ObjectCreationExpression(WellKnownTypes.System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Name) .AddArgumentListArguments( Argument(SyntaxHelpers.StringLiteral(mediaType.Key))))))); } var propertyNameFormatter = NameFormatterSelector.GetFormatter(NameKind.Property); foreach (var headerParameter in operation.Element.Parameters.Where(p => p.In == ParameterLocation.Header)) { string propertyName = propertyNameFormatter.Format(headerParameter.Name); StatementSyntax statement = ExpressionStatement(InvocationExpression( SyntaxHelpers.MemberAccess(RequestMessageParameterName, "Headers", "Add")) .AddArgumentListArguments( Argument(SyntaxHelpers.StringLiteral(headerParameter.Name)), Argument(InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SerializationNamespace.HeaderSerializerInstance, IdentifierName("Serialize"))) .AddArgumentListArguments( Argument(IdentifierName(propertyName)), Argument(headerParameter.Explode ? LiteralExpression(SyntaxKind.TrueLiteralExpression) : LiteralExpression(SyntaxKind.FalseLiteralExpression)))))); if (!headerParameter.Required) { statement = MethodHelpers.IfNotNull( IdentifierName(propertyName), Block(statement)); } yield return(statement); } }
protected virtual IEnumerable <MethodDeclarationSyntax> GenerateOperationMethodHeader( ILocatedOpenApiElement <OpenApiOperation> operation) { TypeSyntax requestType = Context.TypeNameProvider.GetName(operation); TypeSyntax responseType = WellKnownTypes.System.Threading.Tasks.TaskT.Name( Context.TypeNameProvider.GetName(operation.GetResponseSet())); string methodName = Context.NameFormatterSelector.GetFormatter(NameKind.AsyncMethod) .Format(operation.Element.OperationId); var methodDeclaration = MethodDeclaration(responseType, methodName) .AddElementAnnotation(operation, Context.ElementRegistry) .AddParameterListParameters( Parameter(Identifier(OperationMethodGenerator.RequestParameterName)) .WithType(requestType), MethodHelpers.DefaultedCancellationTokenParameter()); yield return(methodDeclaration); }