Пример #1
0
        public MethodDeclarationSyntax?Generate(ILocatedOpenApiElement <OpenApiResponse> response)
        {
            if (response.Element.Content == null)
            {
                return(null);
            }

            ILocatedOpenApiElement <OpenApiMediaType>?mediaType = MediaTypeSelector.Select(response);
            ILocatedOpenApiElement <OpenApiSchema>?   schema    = mediaType?.GetSchema();

            if (schema == null)
            {
                return(null);
            }

            ITypeGenerator schemaGenerator = Context.SchemaGeneratorRegistry.Get(schema);

            TypeSyntax returnType = schemaGenerator.TypeName;

            return(MethodDeclaration(
                       WellKnownTypes.System.Threading.Tasks.ValueTaskT.Name(returnType),
                       GetBodyMethodName)
                   .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.AsyncKeyword))
                   .WithBody(Block(GenerateStatements(response, returnType))));
        }
Пример #2
0
        public IEnumerable <BaseMethodDeclarationSyntax> Generate(ILocatedOpenApiElement <OpenApiResponse> response, string className)
        {
            if (!response.IsRoot() && response.Element.Reference != null)
            {
                // Do not generator for responses within operations that are references to components, these will inherit
                // their get body method from the component base class
                yield break;
            }

            if (response.Element.Content == null)
            {
                yield break;
            }

            ILocatedOpenApiElement <OpenApiMediaType>?mediaType = MediaTypeSelector.Select(response);
            ILocatedOpenApiElement <OpenApiSchema>?   schema    = mediaType?.GetSchema();

            if (schema == null)
            {
                yield break;
            }

            ITypeGenerator schemaGenerator = Context.TypeGeneratorRegistry.Get(schema);

            TypeSyntax returnType = schemaGenerator.TypeInfo.Name;

            yield return(MethodDeclaration(
                             WellKnownTypes.System.Threading.Tasks.ValueTaskT.Name(returnType),
                             GetBodyMethodName)
                         .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.AsyncKeyword))
                         .WithBody(Block(GenerateStatements(response, returnType))));
        }
Пример #3
0
        private ITypeGenerator?GetSchemaGenerator()
        {
            ILocatedOpenApiElement <OpenApiMediaType>?mediaType = MediaTypeSelector.Select(RequestBodyElement);

            ILocatedOpenApiElement <OpenApiSchema>?schemaElement = mediaType?.GetSchema();

            if (schemaElement == null || schemaElement.Element.Type != "object" ||
                schemaElement.Element.Reference != null)
            {
                return(null);
            }

            return(Context.SchemaGeneratorRegistry.Get(schemaElement));
        }
Пример #4
0
        private (ITypeGenerator?schemaGenerator, bool isReference) GetSchemaGenerator()
        {
            ILocatedOpenApiElement <OpenApiMediaType>?mediaType = MediaTypeSelector.Select(Element);

            if (mediaType == null)
            {
                return(null, false);
            }

            ILocatedOpenApiElement <OpenApiSchema>?schemaElement = mediaType.GetSchema();

            if (schemaElement == null)
            {
                return(null, false);
            }

            return(Context.SchemaGeneratorRegistry.Get(schemaElement), schemaElement.Element.Reference != null);
        }
Пример #5
0
        protected virtual TypeSyntax GetTypeName()
        {
            ILocatedOpenApiElement <OpenApiMediaType>?mediaType = MediaTypeSelector.Select(RequestBodyElement);

            if (mediaType == null)
            {
                throw new InvalidOperationException("No valid media type for this request");
            }

            ILocatedOpenApiElement <OpenApiSchema>?schemaElement = mediaType.GetSchema();

            if (schemaElement != null &&
                (schemaElement.Element.Type != "object" || schemaElement.Element.Reference != null))
            {
                return(Context.SchemaGeneratorRegistry.Get(schemaElement).TypeName);
            }

            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(NameKind.Class);
            NameSyntax     ns        = Context.NamespaceProvider.GetNamespace(RequestBodyElement);

            if (RequestBody.Reference != null)
            {
                // We're in the components section

                return(SyntaxFactory.QualifiedName(ns,
                                                   SyntaxFactory.IdentifierName(formatter.Format(RequestBody.Reference.Id + "RequestBody"))));
            }
            else
            {
                // We're in an operation

                var operation = RequestBodyElement.Parents().OfType <LocatedOpenApiElement <OpenApiOperation> >().First().Element;

                return(SyntaxFactory.QualifiedName(ns,
                                                   SyntaxFactory.IdentifierName(formatter.Format(operation.OperationId + "RequestBody"))));
            }
        }
 public static ILocatedOpenApiElement <OpenApiSchema> GetSchemaOrDefault(
     this ILocatedOpenApiElement <OpenApiParameter> parameter) =>
 parameter.GetSchema() ?? parameter.CreateChild(_defaultSchema, "schema");
 public static ILocatedOpenApiElement <OpenApiSchema> GetSchemaOrDefault(
     this ILocatedOpenApiElement <OpenApiHeader> header) =>
 header.GetSchema() ?? header.CreateChild(_defaultSchema, "schema");