コード例 #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"));
            }
        }
コード例 #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))));
        }
コード例 #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")))));
        }
コード例 #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"))));
        }
コード例 #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"));
        }
コード例 #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));
        }
コード例 #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"))));
            }
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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));
        }
コード例 #11
0
        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);
        }
コード例 #12
0
 public virtual string GetFieldName(string rosIdentifier)
 {
     return(_fieldNameFormatter.Format(rosIdentifier));
 }
コード例 #13
0
 public virtual string GetConstantName(string rosIdentifier)
 {
     return(_constantNameFormatter.Format(rosIdentifier));
 }