Beispiel #1
0
        public async Task When_body_has_multiple_parameters_then_all_parameter_is_used_in_CSharp_ASPNetCore()
        {
            var yaml = @"openapi: 3.0.0
servers:
  - url: https://www.example.com/
info:
  version: '2.0.0'
  title: 'Test API'   
paths:
  /files:
    post:
      tags:
        - Files
      summary: 'Add File'
      operationId: addFile
      responses:
        '200':
          description: 'something'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FileToken'
      requestBody:
       content:
         multipart/form-data:
           schema:
             type: object
             required:
               - file
             properties:
               file:
                  type: string
                  format: binary
               id:
                 type: string
                 format: uuid
components:
  schemas:
    FileToken:
      type: object
      required:
        - fileId    
      properties:  
        fileId:
          type: string
          format: uuid";

            var document = await OpenApiYamlDocument.FromYamlAsync(yaml);

            //// Act
            CSharpControllerGeneratorSettings settings = new CSharpControllerGeneratorSettings();

            settings.ControllerTarget = CSharpControllerTarget.AspNetCore;
            var codeGenerator = new CSharpControllerGenerator(document, settings);
            var code          = codeGenerator.GenerateFile();

            //// Assert
            Assert.Contains("id", code);
            Assert.DoesNotContain("FromBody]", code);
        }
Beispiel #2
0
        public async Task When_yaml_with_custom_property_is_loaded_then_document_is_not_null()
        {
            //// Arrange
            var yaml = @"swagger: '2.0'
info:
  title: foo
  version: '1.0'
paths:
  /bar:
    x-swagger-router-controller: bar
    get:
      responses:
        '200':
          description: baz";

            //// Act
            var document = await OpenApiYamlDocument.FromYamlAsync(yaml);

            yaml = document.ToYaml();

            //// Assert
            Assert.NotNull(document);
            Assert.Equal("bar", document.Paths.First().Value.ExtensionData["x-swagger-router-controller"]);
            Assert.Contains("x-swagger-router-controller: bar", yaml);
        }
Beispiel #3
0
        /// <summary>
        /// Create the openapi document file, based on the input.
        /// </summary>
        /// <returns></returns>
        public static async Task <OpenApiDocument> GetOpenApiDoc()
        {
            var fileTxt = GetFileTxt();

            if (JsonOrYaml)
            {
                return(await OpenApiDocument.FromJsonAsync(fileTxt));
            }
            return(await OpenApiYamlDocument.FromYamlAsync(fileTxt));
        }
Beispiel #4
0
        public async Task When_body_is_binary_then_blob_is_used_as_parameter_in_TypeScript()
        {
            var yaml = @"openapi: 3.0.0
servers:
  - url: https://www.example.com/
info:
  version: '2.0.0'
  title: 'Test API'   
paths:
  /files:
    post:
      tags:
        - Files
      summary: 'Add File'
      operationId: addFile
      responses:
        '200':
          content:
            application/xml:
              schema:
                $ref: '#/components/schemas/FileToken'
      requestBody:
        content:
          image/png:
            schema:
              type: string
              format: binary
components:
  schemas:
    FileToken:
      type: object
      required:
        - fileId    
      properties:  
        fileId:
          type: string
          format: uuid";

            var document = await OpenApiYamlDocument.FromYamlAsync(yaml);

            // Act
            var codeGenerator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings());
            var code          = codeGenerator.GenerateFile();

            // Assert
            Assert.Contains("addFile(body: Blob | undefined): ", code);
            Assert.Contains("\"Content-Type\": \"image/png\"", code);
            Assert.Contains("\"Accept\": \"application/xml\"", code);
            Assert.Contains("const content_ = body;", code);
        }
Beispiel #5
0
        public async Task When_body_is_binary_then_stream_is_used_as_parameter_in_CSharp()
        {
            var yaml = @"openapi: 3.0.0
servers:
  - url: https://www.example.com/
info:
  version: '2.0.0'
  title: 'Test API'   
paths:
  /files:
    post:
      tags:
        - Files
      summary: 'Add File'
      operationId: addFile
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FileToken'
      requestBody:
        content:
          image/png:
            schema:
              type: string
              format: binary
components:
  schemas:
    FileToken:
      type: object
      required:
        - fileId    
      properties:  
        fileId:
          type: string
          format: uuid";

            var document = await OpenApiYamlDocument.FromYamlAsync(yaml);

            //// Act
            var codeGenerator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings());
            var code          = codeGenerator.GenerateFile();

            //// Assert
            Assert.Contains("public async System.Threading.Tasks.Task<FileToken> AddFileAsync(System.IO.Stream body, System.Threading.CancellationToken cancellationToken)", code);
            Assert.Contains("var content_ = new System.Net.Http.StreamContent(body);", code);
        }
Beispiel #6
0
 protected async Task <OpenApiDocument> ReadSwaggerDocumentAsync(string input)
 {
     if (!IsJson(input) && !IsYaml(input))
     {
         if (input.StartsWith("http://") || input.StartsWith("https://"))
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
         }
         else
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromFileAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromFileAsync(input).ConfigureAwait(false));
             }
         }
     }
     else
     {
         if (IsYaml(input))
         {
             return(await OpenApiYamlDocument.FromYamlAsync(input).ConfigureAwait(false));
         }
         else
         {
             return(await OpenApiDocument.FromJsonAsync(input).ConfigureAwait(false));
         }
     }
 }
Beispiel #7
0
        protected override async Task OnInstallPackagesAsync(
            AsyncPackage package,
            Project project,
            EnterOpenApiSpecDialogResult dialogResult)
        {
            var url = dialogResult.Url;
            const StringComparison comparisonType = StringComparison.OrdinalIgnoreCase;
            var document = url.EndsWith("yaml", comparisonType) || url.EndsWith("yml", comparisonType)
                ? await OpenApiYamlDocument.FromYamlAsync(dialogResult.OpenApiSpecification)
                : await OpenApiDocument.FromJsonAsync(dialogResult.OpenApiSpecification);

            var codeGenerator = GetSupportedCodeGenerator(document);
            await project.InstallMissingPackagesAsync(package, codeGenerator);

            if (codeGenerator == SupportedCodeGenerator.AutoRestV3)
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                project.Save();

                await project.UpdatePropertyGroupsAsync(AutoRestConstants.PropertyGroups);
            }
        }
Beispiel #8
0
        public async Task When_yaml_with_description_is_loaded_then_document_is_not_null()
        {
            //// Arrange
            var yaml = @"info:
  title: Foo
  version: 1.0.0
paths:
  /something:
    description: foo
    get:
      responses:
        200:
          description: get description";

            //// Act
            var document = await OpenApiYamlDocument.FromYamlAsync(yaml);

            yaml = document.ToYaml();

            //// Assert
            Assert.NotNull(document);
            Assert.Equal("foo", document.Paths.First().Value.Description);
            Assert.Contains("description: foo", yaml);
        }
Beispiel #9
0
 internal static async Task <OpenApiDocument> ReadSwaggerDocumentAsync(this string input)
 {
     if (!input.IsJson() && !input.IsYaml())
     {
         if (input.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || input.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromUrlAsync(input).ConfigureAwait(false));
             }
         }
         else
         {
             if (input.EndsWith(".yaml", StringComparison.OrdinalIgnoreCase) ||
                 input.EndsWith(".yml", StringComparison.OrdinalIgnoreCase))
             {
                 return(await OpenApiYamlDocument.FromFileAsync(input).ConfigureAwait(false));
             }
             else
             {
                 return(await OpenApiDocument.FromFileAsync(input).ConfigureAwait(false));
             }
         }
     }
     else
     {
         return(input.IsYaml()
             ? await OpenApiYamlDocument.FromYamlAsync(input).ConfigureAwait(false)
             : await OpenApiDocument.FromJsonAsync(input).ConfigureAwait(false));
     }
 }