Ejemplo n.º 1
0
        private string GetClassName()
        {
            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(NameKind.Class);

            if (Element.IsRoot())
            {
                // We're in the components section

                return(formatter.Format(Response.Reference.Id + "Response"));
            }
            else
            {
                // We're in an operation

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

                string responseCode = Enum.TryParse <HttpStatusCode>(Element.Key, out var statusCode)
                    ? HttpResponseCodeNameProvider.GetName(statusCode)
                    : Element.Key;

                return(formatter.Format($"{operation.OperationId}{responseCode}Response"));
            }
        }
Ejemplo n.º 2
0
        protected virtual EnumMemberDeclarationSyntax?CreateEnumMember(
            ILocatedOpenApiElement <OpenApiSchema> schemaElement,
            IOpenApiAny value,
            INameFormatter nameFormatter,
            NamingContext namingContext)
        {
            if (value.AnyType != AnyType.Primitive)
            {
                return(null);
            }

            var primitive = (IOpenApiPrimitive)value;

            if (primitive.PrimitiveType != PrimitiveType.String)
            {
                return(null);
            }

            var stringPrimitive = (OpenApiPrimitive <string>)primitive;

            string memberName = namingContext.RegisterName(nameFormatter.Format(stringPrimitive.Value));

            return(SyntaxFactory.EnumMemberDeclaration(memberName)
                   .AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(
                                          CreateEnumMemberAttribute(stringPrimitive.Value))));
        }
Ejemplo n.º 3
0
        protected override YardarmTypeInfo GetTypeInfo()
        {
            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(NameKind.Class);
            NameSyntax     ns        = Context.NamespaceProvider.GetNamespace(Element);

            return(new YardarmTypeInfo(QualifiedName(ns,
                                                     IdentifierName(formatter.Format(Operation.OperationId + "Request")))));
        }
Ejemplo n.º 4
0
        public override QualifiedNameSyntax GetChildName <TChild>(ILocatedOpenApiElement <TChild> child, NameKind nameKind)
        {
            QualifiedNameSyntax?name = Parent?.GetChildName(Element, nameKind);

            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(nameKind);

            if (name == null)
            {
                // At the components root, use the schema namespace
                return(QualifiedName(
                           Context.NamespaceProvider.GetNamespace(Element),
                           IdentifierName(formatter.Format(Element.Key + "-Item"))));
            }

            return(name.WithRight(IdentifierName(
                                      formatter.Format(name.Right.Identifier.ValueText + "-Item"))));
        }
Ejemplo n.º 5
0
        private string GetClassName()
        {
            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(NameKind.Class);

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

            return(formatter.Format($"{operation.OperationId}UnknownResponse"));
        }
Ejemplo n.º 6
0
        protected override YardarmTypeInfo GetTypeInfo()
        {
            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(NameKind.Class);
            NameSyntax     ns        = Context.NamespaceProvider.GetNamespace(RequestTypeGenerator.Element);

            TypeSyntax name = QualifiedName(ns,
                                            IdentifierName(formatter.Format($"{RequestTypeGenerator.Element.Element.OperationId}-HttpContent-Request")));

            return(new YardarmTypeInfo(name));
        }
Ejemplo n.º 7
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"))));
            }
        }
Ejemplo n.º 8
0
        public override QualifiedNameSyntax GetChildName <TChild>(ILocatedOpenApiElement <TChild> child, NameKind nameKind)
        {
            QualifiedNameSyntax?name = Parent?.GetChildName(Element, nameKind);

            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(nameKind);

            if (name == null)
            {
                // At the components root, use the responses namespace
                return(QualifiedName(
                           GetNamespace(),
                           IdentifierName(formatter.Format(Element.Key))));
            }

            return(name);
        }
Ejemplo n.º 9
0
        public override QualifiedNameSyntax GetChildName <TChild>(ILocatedOpenApiElement <TChild> child, NameKind nameKind)
        {
            QualifiedNameSyntax?name = Parent?.GetChildName(Element, nameKind);

            INameFormatter formatter = Context.NameFormatterSelector.GetFormatter(nameKind);

            if (name == null)
            {
                // At the components root, use the parameters namespace. In this case, use the key on the components
                // dictionary and not the parameter name, since multiple component parameters may have the same name.
                return(QualifiedName(
                           GetNamespace(),
                           IdentifierName(formatter.Format(Element.Key))));
            }

            return(name);
        }
Ejemplo n.º 10
0
        protected override YardarmTypeInfo GetTypeInfo()
        {
            SerializerDescriptor?serializerDescriptor = SerializerSelector.Select(Element)?.Descriptor;

            if (serializerDescriptor == null)
            {
                throw new InvalidOperationException($"No serializer configured for {Element}.");
            }

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

            TypeSyntax name = QualifiedName(ns,
                                            IdentifierName(formatter.Format($"{RequestTypeGenerator.Element.Element.OperationId}-{serializerDescriptor.NameSegment}-Request")));

            return(new YardarmTypeInfo(name));
        }
        public string Format(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(value);
            }

            if (value.Length == 1)
            {
                return(value.ToUpper());
            }

            var camelCase  = _camelCase.Format(value);
            var pascalCase = camelCase[0].ToString().ToUpper() + camelCase.Substring(1);

            return(pascalCase);
        }
 public virtual string GetFieldName(string rosIdentifier)
 {
     return(_fieldNameFormatter.Format(rosIdentifier));
 }
 public virtual string GetConstantName(string rosIdentifier)
 {
     return(_constantNameFormatter.Format(rosIdentifier));
 }