private IEnumerable <MethodDeclarationSyntax> GenerateExtensions( ILocatedOpenApiElement <OpenApiResponses> responseSet) { var nameFormatter = _context.NameFormatterSelector.GetFormatter(NameKind.Method); TypeSyntax interfaceTypeName = _context.TypeGeneratorRegistry.Get(responseSet).TypeInfo.Name; foreach (var response in responseSet.GetResponses()) { string responseCode = Enum.TryParse <HttpStatusCode>(response.Key, out var statusCode) ? _httpResponseCodeNameProvider.GetName(statusCode) : response.Key; TypeSyntax typeName = _context.TypeGeneratorRegistry.Get(response).TypeInfo.Name; yield return(MethodDeclaration(typeName, nameFormatter.Format("As-" + responseCode)) .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) .AddParameterListParameters( Parameter(Identifier("response")) .WithType(interfaceTypeName) .AddModifiers(Token(SyntaxKind.ThisKeyword))) .WithExpressionBody(ArrowExpressionClause( BinaryExpression(SyntaxKind.CoalesceExpression, BinaryExpression(SyntaxKind.AsExpression, IdentifierName("response"), typeName), ThrowExpression(ObjectCreationExpression(_responsesNamespace.StatusCodeMismatchException) .AddArgumentListArguments( Argument(IdentifierName("response")), Argument(TypeOfExpression(typeName)))))))); } }
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); } }