public async void ShouldConvertOpenApiSpecToDocument()
        {
            // arrange
            OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
            string            fileLocation      = "https://petstore.swagger.io/v2/swagger.json";

            // act
            OpenApiDocument doc = await openAPISpecReader.ConvertOpenAPISpecToDoc(fileLocation);

            // assert
            // document is only used to pull protocols, ensure servers is not null (contains protocols)
            Assert.NotNull(doc.Servers);
        }
Exemplo n.º 2
0
        public async void ShouldDetermineCorrectOpenAPISpecVersion()
        {
            // arrangeW
            OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();

            // act
            bool shouldOutputVersionTwo = await openAPISpecReader.IsJSONOpenAPISpecVersionThreeAsync(string.Concat(this.openAPISpecFolder, "swaggerPetstore.json"));

            bool shouldOutputVersionThree = await openAPISpecReader.IsJSONOpenAPISpecVersionThreeAsync(string.Concat(this.openAPISpecFolder, "swaggerPetstorev3.json"));

            // assert
            Assert.False(shouldOutputVersionTwo);
            Assert.True(shouldOutputVersionThree);
        }
        public async Task <APITemplateResource> CreateAPITemplateResourceAsync(APIConfig api, bool isSplit, bool isInitial)
        {
            // create api resource
            APITemplateResource apiTemplateResource = new APITemplateResource()
            {
                Name       = MakeResourceName(api),
                Type       = ResourceTypeConstants.API,
                ApiVersion = GlobalConstants.ApiVersion,
                Properties = new APITemplateProperties(),
                DependsOn  = new string[] { }
            };

            // add properties depending on whether the template is the initial, subsequent, or unified
            if (!isSplit || !isInitial)
            {
                // add metadata properties for initial and unified templates
                apiTemplateResource.Properties.ApiVersion                    = api.apiVersion;
                apiTemplateResource.Properties.ServiceUrl                    = this.MakeServiceUrl(api);
                apiTemplateResource.Properties.Type                          = api.type;
                apiTemplateResource.Properties.ApiType                       = api.type;
                apiTemplateResource.Properties.Description                   = api.description;
                apiTemplateResource.Properties.SubscriptionRequired          = api.subscriptionRequired;
                apiTemplateResource.Properties.ApiRevision                   = api.apiRevision;
                apiTemplateResource.Properties.ApiRevisionDescription        = api.apiRevisionDescription;
                apiTemplateResource.Properties.ApiVersionDescription         = api.apiVersionDescription;
                apiTemplateResource.Properties.AuthenticationSettings        = api.authenticationSettings;
                apiTemplateResource.Properties.SubscriptionKeyParameterNames = api.subscriptionKeyParameterNames;
                apiTemplateResource.Properties.Path                          = api.suffix;
                apiTemplateResource.Properties.IsCurrent                     = api.isCurrent;
                apiTemplateResource.Properties.DisplayName                   = string.IsNullOrEmpty(api.displayName) ? api.name : api.displayName;
                apiTemplateResource.Properties.Protocols                     = this.CreateProtocols(api);
                // set the version set id
                if (api.apiVersionSetId != null)
                {
                    // point to the supplied version set if the apiVersionSetId is provided
                    apiTemplateResource.Properties.ApiVersionSetId = $"[resourceId('Microsoft.ApiManagement/service/apiVersionSets', parameters('{ParameterNames.ApimServiceName}'), '{api.apiVersionSetId}')]";
                }
                // set the authorization server id
                if (api.authenticationSettings != null && api.authenticationSettings.OAuth2 != null && api.authenticationSettings.OAuth2.AuthorizationServerId != null &&
                    apiTemplateResource.Properties.AuthenticationSettings != null && apiTemplateResource.Properties.AuthenticationSettings.OAuth2 != null && apiTemplateResource.Properties.AuthenticationSettings.OAuth2.AuthorizationServerId != null)
                {
                    apiTemplateResource.Properties.AuthenticationSettings.OAuth2.AuthorizationServerId = api.authenticationSettings.OAuth2.AuthorizationServerId;
                }
                // set the subscriptionKey Parameter Names
                if (api.subscriptionKeyParameterNames != null)
                {
                    if (api.subscriptionKeyParameterNames.Header != null)
                    {
                        apiTemplateResource.Properties.SubscriptionKeyParameterNames.Header = api.subscriptionKeyParameterNames.Header;
                    }
                    if (api.subscriptionKeyParameterNames.Query != null)
                    {
                        apiTemplateResource.Properties.SubscriptionKeyParameterNames.Query = api.subscriptionKeyParameterNames.Query;
                    }
                }
            }
            if (!isSplit || isInitial)
            {
                // add open api spec properties for subsequent and unified templates
                string format;
                string value;

                // determine if the open api spec is remote or local, yaml or json
                bool isJSON = false;
                bool isUrl  = IsUri(api, out var _);

                string fileContents = null;
                if (!isUrl || api.openApiSpecFormat == OpenApiSpecFormat.Unspecified)
                {
                    fileContents = await this.fileReader.RetrieveFileContentsAsync(api.openApiSpec);
                }

                value = isUrl
                    ? api.openApiSpec
                    : fileContents
                ;

                bool isVersionThree = false;
                if (api.openApiSpecFormat == OpenApiSpecFormat.Unspecified)
                {
                    isJSON = this.fileReader.IsJSON(fileContents);

                    if (isJSON == true)
                    {
                        var openAPISpecReader = new OpenAPISpecReader();
                        isVersionThree = await openAPISpecReader.IsJSONOpenAPISpecVersionThreeAsync(api.openApiSpec);
                    }
                    format = GetOpenApiSpecFormat(isUrl, isJSON, isVersionThree);
                }

                else
                {
                    isJSON = IsOpenApiSpecJson(api.openApiSpecFormat);
                    format = GetOpenApiSpecFormat(isUrl, api.openApiSpecFormat);
                }

                // if the title needs to be modified
                // we need to embed the OpenAPI definition

                if (!string.IsNullOrEmpty(api.displayName))
                {
                    format = GetOpenApiSpecFormat(false, isJSON, isVersionThree);

                    // download definition

                    if (isUrl)
                    {
                        using (var client = new WebClient())
                            value = client.DownloadString(value);
                    }

                    // update title

                    value = new OpenApi(value, format)
                            .SetTitle(api.displayName)
                            .GetDefinition()
                    ;
                }

                // set the version set id
                if (api.apiVersionSetId != null)
                {
                    // point to the supplied version set if the apiVersionSetId is provided
                    apiTemplateResource.Properties.ApiVersionSetId = $"[resourceId('Microsoft.ApiManagement/service/apiVersionSets', parameters('{ParameterNames.ApimServiceName}'), '{api.apiVersionSetId}')]";
                }
                apiTemplateResource.Properties.Format = format;
                apiTemplateResource.Properties.Value  = value;

                // #562: deploying multiple versions of an API may fail because while trying to deploy the initial template
                // overwrite the initial template’s path property to a dummy value
                // this value will be restored when the subsequent template is deployed

                if (isSplit && isInitial)
                {
                    apiTemplateResource.Properties.Path = api.suffix + $"/{Guid.NewGuid():n}";
                }
                else
                {
                    apiTemplateResource.Properties.Path = api.suffix;
                }

                if (!string.IsNullOrEmpty(api.serviceUrl))
                {
                    apiTemplateResource.Properties.ServiceUrl = this.MakeServiceUrl(api);
                }
            }
            return(apiTemplateResource);
        }