private void AddRequestBodyParameters(bool useComplexTypeForBody = false) { var stringType = new CodeType { Name = "string", }; var requestConfigClass = parentClass.AddInnerClass(new CodeClass { Name = "RequestConfig", Kind = CodeClassKind.RequestConfiguration, }).First(); requestConfigClass.AddProperty(new() { Name = "h", Kind = CodePropertyKind.Headers, Type = stringType, }, new () { Name = "q", Kind = CodePropertyKind.QueryParameters, Type = stringType, }, new () { Name = "o", Kind = CodePropertyKind.Options, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "b", Kind = CodeParameterKind.RequestBody, Type = useComplexTypeForBody ? new CodeType { Name = "SomeComplexTypeForRequestBody", TypeDefinition = root.AddClass(new CodeClass { Name = "SomeComplexTypeForRequestBody", Kind = CodeClassKind.Model, }).First(), } : stringType, }); method.AddParameter(new CodeParameter { Name = "config", Kind = CodeParameterKind.RequestConfiguration, Type = new CodeType { Name = "RequestConfig", TypeDefinition = requestConfigClass, ActionOf = true, }, Optional = true, }); method.AddParameter(new CodeParameter { Name = "r", Kind = CodeParameterKind.ResponseHandler, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "c", Kind = CodeParameterKind.Cancellation, Type = stringType, }); }
private void AddRequestBodyParameters() { var stringType = new CodeType(method) { Name = "string", }; method.AddParameter(new CodeParameter(method) { Name = "h", ParameterKind = CodeParameterKind.Headers, Type = stringType, }); method.AddParameter(new CodeParameter(method) { Name = "q", ParameterKind = CodeParameterKind.QueryParameter, Type = stringType, }); method.AddParameter(new CodeParameter(method) { Name = "b", ParameterKind = CodeParameterKind.RequestBody, Type = stringType, }); method.AddParameter(new CodeParameter(method) { Name = "r", ParameterKind = CodeParameterKind.ResponseHandler, Type = stringType, }); }
public void WriteRequestBuilderWithParametersBody() { var codeMethod = new CodeMethod() { ReturnType = new CodeType() { Name = "MessageRequestBuilder", IsNullable = false }, Name = "messageById", Parent = parentClass, Kind = CodeMethodKind.RequestBuilderWithParameters }; codeMethod.AddParameter(new CodeParameter { Kind = CodeParameterKind.Path, Name = "id", Type = new CodeType { Name = "string" } }); _codeMethodWriter.WriteCodeElement(codeMethod, languageWriter); var result = stringWriter.ToString(); Assert.Contains("function messageById(string $id): MessageRequestBuilder {", result); Assert.Contains("return new MessageRequestBuilder($this->pathParameters, $this->requestAdapter, $id);", result); }
public void OrdersWithMethodWithinClass() { var root = CodeNamespace.InitRootNamespace(); var comparer = new CodeElementOrderComparer(); var codeClass = new CodeClass { Name = "Class" }; root.AddClass(codeClass); var method = new CodeMethod { Name = "Method" }; codeClass.AddMethod(method); method.AddParameter(new CodeParameter { Name = "param" }); var dataSet = new List <Tuple <CodeElement, CodeElement, int> > { new(null, null, 0), new(null, new CodeClass(), -1), new(new CodeClass(), null, 1), new(new CodeUsing(), new CodeProperty(), -1000), new(new CodeIndexer(), new CodeProperty(), 1000), new(method, new CodeProperty(), 1101), new(method, codeClass, -899) };
public void AddsParameter() { var method = new CodeMethod { Name = "method1" }; Assert.Throws <ArgumentNullException>(() => { method.AddParameter((CodeParameter)null); }); Assert.Throws <ArgumentNullException>(() => { method.AddParameter(null); }); Assert.Throws <ArgumentOutOfRangeException>(() => { method.AddParameter(Array.Empty <CodeParameter>()); }); }
public void AddsParameter() { var root = CodeNamespace.InitRootNamespace(); var method = new CodeMethod(root) { Name = "method1" }; Assert.Throws <ArgumentNullException>(() => { method.AddParameter((CodeParameter)null); }); Assert.Throws <ArgumentNullException>(() => { method.AddParameter(null); }); Assert.Throws <ArgumentOutOfRangeException>(() => { method.AddParameter(new CodeParameter[] { }); }); }
public void WritesMethodSyncDescription() { method.Description = MethodDescription; method.IsAsync = false; var parameter = new CodeParameter { Description = ParamDescription, Name = ParamName, Type = new CodeType { Name = "string" } }; method.AddParameter(parameter); writer.Write(method); var result = tw.ToString(); Assert.DoesNotContain("@return a CompletableFuture of", result); AssertExtensions.CurlyBracesAreClosed(result); }
public void ParametersExtensionsReturnsValue() { var method = new CodeMethod { Name = "method1" }; method.AddParameter(new CodeParameter { Name = "param1", Kind = CodeParameterKind.Custom, }); Assert.NotNull(method.Parameters.OfKind(CodeParameterKind.Custom)); Assert.Null(method.Parameters.OfKind(CodeParameterKind.RequestBody)); }
public void ClonesParameters() { var method = new CodeMethod { Name = "method1" }; method.AddParameter(new CodeParameter { Name = "param1" }); var clone = method.Clone() as CodeMethod; Assert.Equal(method.Name, clone.Name); Assert.Single(method.Parameters); Assert.Equal(method.Parameters.First().Name, clone.Parameters.First().Name); }
private void AddRequestBodyParameters() { var stringType = new CodeType { Name = "string", }; var requestConfigClass = parentClass.AddInnerClass(new CodeClass { Name = "RequestConfig", Kind = CodeClassKind.RequestConfiguration, }).First(); requestConfigClass.AddProperty(new() { Name = "h", Kind = CodePropertyKind.Headers, Type = stringType, }, new () { Name = "q", Kind = CodePropertyKind.QueryParameters, Type = stringType, }, new () { Name = "o", Kind = CodePropertyKind.Options, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "b", Kind = CodeParameterKind.RequestBody, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "config", Kind = CodeParameterKind.RequestConfiguration, Type = new CodeType { Name = "RequestConfig", TypeDefinition = requestConfigClass, ActionOf = true, }, Optional = true, }); method.AddParameter(new CodeParameter { Name = "r", Kind = CodeParameterKind.ResponseHandler, Type = stringType, }); }
public void ParametersExtensionsReturnsValue() { var root = CodeNamespace.InitRootNamespace(); var method = new CodeMethod(root) { Name = "method1" }; method.AddParameter(new CodeParameter(method) { Name = "param1", ParameterKind = CodeParameterKind.Custom, }); Assert.NotNull(method.Parameters.OfKind(CodeParameterKind.Custom)); Assert.Null(method.Parameters.OfKind(CodeParameterKind.RequestBody)); }
public void ClonesParameters() { var root = CodeNamespace.InitRootNamespace(); var method = new CodeMethod(root) { Name = "method1" }; method.AddParameter(new CodeParameter(method) { Name = "param1" }); var clone = method.Clone() as CodeMethod; Assert.Equal(method.Name, clone.Name); Assert.Single(method.Parameters); Assert.Equal(method.Parameters.First().Name, clone.Parameters.First().Name); }
public void WriteApiConstructor() { parentClass.AddProperty(new CodeProperty { Name = "requestAdapter", Kind = CodePropertyKind.RequestAdapter, Type = new CodeType { Name = "RequestAdapter" } }); var codeMethod = new CodeMethod() { ReturnType = new CodeType() { Name = "void", IsNullable = false }, Name = "construct", Parent = parentClass, Kind = CodeMethodKind.ClientConstructor }; codeMethod.AddParameter(new CodeParameter() { Kind = CodeParameterKind.RequestAdapter, Name = "requestAdapter", Type = new CodeType() { Name = "RequestAdapter" }, SerializationName = "rawUrl" }); codeMethod.DeserializerModules = new() { "Microsoft\\Kiota\\Serialization\\Deserializer" }; codeMethod.SerializerModules = new() { "Microsoft\\Kiota\\Serialization\\Serializer" }; parentClass.AddMethod(codeMethod); _refiner.Refine(parentClass.Parent as CodeNamespace); languageWriter.Write(codeMethod); var result = stringWriter.ToString(); Assert.Contains("$this->requestAdapter = $requestAdapter", result); Assert.Contains("public function __construct(RequestAdapter $requestAdapter)", result); } } }
public static CodeMethod FromIndexer(CodeIndexer originalIndexer, CodeClass indexerClass, string methodNameSuffix, bool parameterNullable) { if (originalIndexer == null) { throw new ArgumentNullException(nameof(originalIndexer)); } if (indexerClass == null) { throw new ArgumentNullException(nameof(indexerClass)); } var method = new CodeMethod { IsAsync = false, IsStatic = false, Access = AccessModifier.Public, Kind = CodeMethodKind.IndexerBackwardCompatibility, Name = originalIndexer.PathSegment + methodNameSuffix, Description = originalIndexer.Description, ReturnType = new CodeType { IsNullable = false, TypeDefinition = indexerClass, Name = indexerClass.Name, }, OriginalIndexer = originalIndexer, }; var parameter = new CodeParameter { Name = "id", Optional = false, Kind = CodeParameterKind.Custom, Description = "Unique identifier of the item", Type = new CodeType { Name = "String", IsNullable = parameterNullable, IsExternal = true, }, }; method.AddParameter(parameter); return(method); }
public void WriteSerializer(CodeProperty property, string expected) { var codeMethod = new CodeMethod() { Name = "serialize", Kind = CodeMethodKind.Serializer, ReturnType = new CodeType() { Name = "void", } }; codeMethod.AddParameter(new CodeParameter() { Name = "writer", Kind = CodeParameterKind.Serializer, Type = new CodeType() { Name = "SerializationWriter" } }); parentClass.AddMethod(codeMethod); parentClass.AddProperty(property); parentClass.Kind = CodeClassKind.Model; _codeMethodWriter.WriteCodeElement(codeMethod, languageWriter); var result = stringWriter.ToString(); Assert.Contains("public function serialize(SerializationWriter $writer)", result); if (property.ExistsInBaseType) { Assert.DoesNotContain(expected, result); } else { Assert.Contains(expected, stringWriter.ToString()); } }
public void WriteSetter() { var setter = new CodeMethod() { Name = "setEmailAddress", ReturnType = new CodeType() { Name = "void" }, Kind = CodeMethodKind.Setter, AccessedProperty = new CodeProperty() { Name = "emailAddress", Access = AccessModifier.Private, Type = new CodeType() { Name = "emailAddress" } }, Parent = parentClass }; setter.AddParameter(new CodeParameter() { Name = "value", Kind = CodeParameterKind.SetterValue, Type = new CodeType() { Name = "emailAddress" } }); _codeMethodWriter.WriteCodeElement(setter, languageWriter); var result = stringWriter.ToString(); Assert.Contains("public function setEmailAddress(EmailAddress $value)", result); Assert.Contains(": void {", result); Assert.Contains("$this->emailAddress = $value", result); }
private static void AddRequestBodyParameters(CodeMethod method) { var stringType = new CodeType { Name = "string", }; method.AddParameter(new CodeParameter { Name = "h", Kind = CodeParameterKind.Headers, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "q", Kind = CodeParameterKind.QueryParameter, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "b", Kind = CodeParameterKind.RequestBody, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "r", Kind = CodeParameterKind.ResponseHandler, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "o", Kind = CodeParameterKind.Options, Type = stringType, }); method.AddParameter(new CodeParameter { Name = "c", Kind = CodeParameterKind.Cancellation, Type = stringType, }); }
public void WriteRequestGenerator() { parentClass.Kind = CodeClassKind.RequestBuilder; parentClass.AddProperty( new CodeProperty() { Name = "urlTemplate", Access = AccessModifier.Protected, DefaultValue = "https://graph.microsoft.com/v1.0/", Description = "The URL template", Kind = CodePropertyKind.UrlTemplate, Type = new CodeType() { Name = "string" } }, new CodeProperty() { Name = "pathParameters", Access = AccessModifier.Protected, DefaultValue = "[]", Description = "The Path parameters.", Kind = CodePropertyKind.PathParameters, Type = new CodeType() { Name = "array" } }, new CodeProperty() { Name = "requestAdapter", Access = AccessModifier.Protected, Description = "The request Adapter", Kind = CodePropertyKind.RequestAdapter, Type = new CodeType() { IsNullable = false, Name = "RequestAdapter" } }); var codeMethod = new CodeMethod() { Name = "createPostRequestInformation", ReturnType = new CodeType() { Name = "RequestInformation", IsNullable = false }, Access = AccessModifier.Public, Description = "This method creates request information for POST request.", HttpMethod = HttpMethod.Post, BaseUrl = "https://graph.microsoft.com/v1.0/", Kind = CodeMethodKind.RequestGenerator, }; var stringType = new CodeType { Name = "string", IsNullable = false }; var requestConfigClass = parentClass.AddInnerClass(new CodeClass { Name = "RequestConfig", Kind = CodeClassKind.RequestConfiguration, }).First(); requestConfigClass.AddProperty(new() { Name = "h", Kind = CodePropertyKind.Headers, Type = stringType, }, new () { Name = "q", Kind = CodePropertyKind.QueryParameters, Type = stringType, }, new () { Name = "o", Kind = CodePropertyKind.Options, Type = stringType, }); codeMethod.AddParameter( new CodeParameter() { Name = "body", Kind = CodeParameterKind.RequestBody, Type = new CodeType() { Name = "Message", IsExternal = true, IsNullable = false } }, new CodeParameter { Name = "config", Kind = CodeParameterKind.RequestConfiguration, Type = new CodeType { Name = "RequestConfig", TypeDefinition = requestConfigClass, ActionOf = true, }, Optional = true, }); parentClass.AddMethod(codeMethod); _codeMethodWriter.WriteCodeElement(codeMethod, languageWriter); var result = stringWriter.ToString(); Assert.Contains( "public function createPostRequestInformation(Message $body, ?RequestConfig $requestConfiguration = null): RequestInformation", result); Assert.Contains("if ($requestConfiguration !== null", result); Assert.Contains("if ($requestConfiguration->h !== null)", result); Assert.Contains("$requestInfo->headers = array_merge($requestInfo->headers, $requestConfiguration->h);", result); Assert.Contains("$requestInfo->setQueryParameters($requestConfiguration->q);", result); Assert.Contains("$requestInfo->addRequestOptions(...$requestConfiguration->o);", result); Assert.Contains("return $requestInfo;", result); }
public void WriteIndexerBody() { parentClass.AddProperty( new CodeProperty() { Name = "pathParameters", Kind = CodePropertyKind.PathParameters, Type = new CodeType() { Name = "array" }, DefaultValue = "[]" }, new CodeProperty() { Name = "requestAdapter", Kind = CodePropertyKind.RequestAdapter, Type = new CodeType() { Name = "requestAdapter" } }, new CodeProperty { Name = "urlTemplate", Kind = CodePropertyKind.UrlTemplate, Type = new CodeType { Name = "string" } } ); var codeMethod = new CodeMethod() { Name = "messageById", Access = AccessModifier.Public, Kind = CodeMethodKind.IndexerBackwardCompatibility, Description = "Get messages by a specific ID.", OriginalIndexer = new CodeIndexer() { Name = "messageById", SerializationName = "message_id", IndexType = new CodeType() { Name = "MessageRequestBuilder" } }, OriginalMethod = new CodeMethod() { Name = "messageById", Access = AccessModifier.Public, Kind = CodeMethodKind.IndexerBackwardCompatibility, ReturnType = new CodeType() { Name = "MessageRequestBuilder" } }, ReturnType = new CodeType() { Name = "MessageRequestBuilder", IsNullable = false, TypeDefinition = new CodeClass() { Name = "MessageRequestBuilder", Kind = CodeClassKind.RequestBuilder, Parent = parentClass.Parent } } }; codeMethod.AddParameter(new CodeParameter() { Name = "id", Type = new CodeType { Name = "string", IsNullable = false }, Kind = CodeParameterKind.Path }); parentClass.AddMethod(codeMethod); _refiner.Refine(parentClass.Parent as CodeNamespace); languageWriter.Write(codeMethod); var result = stringWriter.ToString(); Assert.Contains("$urlTplParams['message_id'] = $id;", result); Assert.Contains("public function messageById(string $id): MessageRequestBuilder {", result); Assert.Contains("return new MessageRequestBuilder($urlTplParams, $this->requestAdapter);", result); }
public void WriteRequestExecutor() { CodeProperty[] properties = { new CodeProperty { Kind = CodePropertyKind.RequestAdapter, Name = "requestAdapter" }, new CodeProperty { Kind = CodePropertyKind.UrlTemplate, Name = "urlTemplate" }, new CodeProperty { Kind = CodePropertyKind.PathParameters, Name = "pathParameters" }, }; parentClass.AddProperty(properties); var codeMethod = new CodeMethod() { Name = "post", HttpMethod = HttpMethod.Post, ReturnType = new CodeType() { IsExternal = true, Name = "StreamInterface" }, Description = "This will send a POST request", Kind = CodeMethodKind.RequestExecutor }; codeMethod.AddParameter(new CodeParameter { Name = "ResponseHandler", Kind = CodeParameterKind.ResponseHandler, Optional = true, Type = new CodeType { Name = "ResponseHandler", IsNullable = true } }); var codeMethodRequestGenerator = new CodeMethod() { Kind = CodeMethodKind.RequestGenerator, HttpMethod = HttpMethod.Post, Name = "createPostRequestInformation", ReturnType = new CodeType() { Name = "RequestInformation" } }; parentClass.AddMethod(codeMethod); parentClass.AddMethod(codeMethodRequestGenerator); var error4XX = root.AddClass(new CodeClass { Name = "Error4XX", }).First(); var error5XX = root.AddClass(new CodeClass { Name = "Error5XX", }).First(); var error401 = root.AddClass(new CodeClass { Name = "Error401", }).First(); codeMethod.AddErrorMapping("4XX", new CodeType { Name = "Error4XX", TypeDefinition = error4XX }); codeMethod.AddErrorMapping("5XX", new CodeType { Name = "Error5XX", TypeDefinition = error5XX }); codeMethod.AddErrorMapping("403", new CodeType { Name = "Error403", TypeDefinition = error401 }); _codeMethodWriter.WriteCodeElement(codeMethod, languageWriter); var result = stringWriter.ToString(); Assert.Contains("Promise", result); Assert.Contains("$requestInfo = $this->createPostRequestInformation();", result); Assert.Contains("RejectedPromise", result); Assert.Contains("catch(Exception $ex)", result); Assert.Contains("'403' => array(Error403::class, 'createFromDiscriminatorValue')", result); Assert.Contains("return $this->requestAdapter->sendPrimitiveAsync($requestInfo, StreamInterface::class, $responseHandler, $errorMappings);", result); }
public void WriteRequestBuilderConstructor() { method.Kind = CodeMethodKind.Constructor; var defaultValue = "[]"; var propName = "propWithDefaultValue"; parentClass.Kind = CodeClassKind.RequestBuilder; parentClass.AddProperty(new CodeProperty { Name = propName, DefaultValue = defaultValue, Kind = CodePropertyKind.UrlTemplate, }); parentClass.AddProperty(new CodeProperty { Name = "requestAdapter", Kind = CodePropertyKind.RequestAdapter, }); parentClass.AddProperty(new CodeProperty { Name = "pathParameters", Kind = CodePropertyKind.PathParameters, Type = new CodeType { Name = "string", } }); parentClass.AddProperty(new CodeProperty { Name = "urlTemplate", Kind = CodePropertyKind.UrlTemplate, }); method.AddParameter(new CodeParameter() { Name = "requestAdapter", Kind = CodeParameterKind.RequestAdapter, Type = new CodeType() { Name = "RequestAdapter", IsExternal = true } }); method.AddParameter(new CodeParameter { Name = "pathParameters", Kind = CodeParameterKind.PathParameters, Type = new CodeType { Name = "array" } }); method.AddParameter(new CodeParameter() { Kind = CodeParameterKind.Path, Name = "username", Optional = true, Type = new CodeType { Name = "string", IsNullable = true } }); languageWriter.Write(method); var result = stringWriter.ToString(); Assert.Contains("__construct", result); Assert.Contains($"$this->{propName} = {defaultValue};", result); Assert.Contains("$this->pathParameters = array_merge($this->pathParameters, $urlTplParams);", result); }