示例#1
0
        public void TestInitialise()
        {
            _typeResolver          = new Mock <IOpenApiUmbrellaTypeResolver>();
            _fieldStealer          = new Mock <IStealFieldsFromOpenApiObjectTypesService>();
            _objectsKeyStore       = new Mock <IObjectsProcessingKeyStore>();
            _moduleBuilderProvider = new ModuleBuilderProvider();

            _service = new OpenApiObjectToTypeService(_typeResolver.Object,
                                                      _fieldStealer.Object,
                                                      _moduleBuilderProvider);

            _prop1     = new OpenApiPrimitiveType();
            _prop2     = new OpenApiPrimitiveType();
            _inputType = new OpenApiObjectType()
            {
                Properties = new Dictionary <string, IOpenApiType>()
                {
                    { "FirstProperty", _prop1 },
                    { "SecondProperty", _prop2 }
                }
            };
            _inputDefinitions = new Dictionary <string, IOpenApiType>();
            _name             = Guid.NewGuid().ToString();

            _typeResolver.Setup(s => s.GetType(_service,
                                               _objectsKeyStore.Object,
                                               It.IsAny <OpenApiPrimitiveType>(),
                                               _inputDefinitions,
                                               It.IsAny <string>()))
            .Returns(typeof(int));
        }
示例#2
0
        public OpenApiObjectType Parse(TModel model)
        {
            var output = new OpenApiObjectType()
            {
                Properties = new Dictionary <string, IOpenApiType>()
            };

            if (model.Properties != null)
            {
                foreach (var prop in model.Properties)
                {
                    output.Properties[prop.Key] = _typeParser.Parse(this,
                                                                    prop.Value);
                }
            }

            if (model.AllOf != null && model.AllOf.Any())
            {
                var allOf = new List <IOpenApiType>();
                foreach (var extendedType in model.AllOf)
                {
                    allOf.Add(_typeParser.Parse(this,
                                                extendedType));
                }
                output.AllOf = allOf;
            }

            return(output);
        }
示例#3
0
        public void TestInitialise()
        {
            _lastTokenService = new Mock <ILastTokenInPathService>();
            _objectKeyStore   = new Mock <IObjectsProcessingKeyStore>();
            _objectService    = new Mock <IOpenApiObjectToTypeService>();
            _typeResolver     = new Mock <IOpenApiUmbrellaTypeResolver>();

            _service = new OpenApiReferenceToTypeService(_lastTokenService.Object);

            _inputType                              = new OpenApiReferencedType();
            _definedNotCreated                      = new OpenApiObjectType();
            _inputDefinitions                       = new Dictionary <string, IOpenApiType>();
            _inputDefinitions["NewType"]            = _definedNotCreated;
            _originalName                           = "path/ExistingType";
            _processedName                          = "ExistingType";
            CurrentTypeHolder.Types["ExistingType"] = typeof(ExistingType);

            _lastTokenService.Setup(s => s.GetLastToken(It.IsAny <string>()))
            .Returns(() => _processedName).Verifiable();
            _typeResolver.Setup(s => s.GetType(_objectService.Object,
                                               _objectKeyStore.Object,
                                               _definedNotCreated,
                                               _inputDefinitions,
                                               "NewType")).Returns(typeof(NewType));
        }
示例#4
0
        public void TestInitialise()
        {
            _primitiveService = new Mock <IOpenApiPrimitiveToTypeService>();
            _referenceService = new Mock <IOpenApiReferenceToTypeService>();
            _objectService    = new Mock <IOpenApiObjectToTypeService>();
            _keyStore         = new Mock <IObjectsProcessingKeyStore>();

            _service = new OpenApiUmbrellaTypeResolver(_primitiveService.Object,
                                                       _referenceService.Object);

            _definitions   = new Dictionary <string, IOpenApiType>();
            _suggestedName = Guid.NewGuid().ToString();
            _primitive     = new OpenApiPrimitiveType();
            _referenced    = new OpenApiReferencedType();
            _object        = new OpenApiObjectType();
            _array         = new OpenApiArrayType();

            _primitiveService.Setup(s => s.GetType(_primitive, It.IsAny <string>()))
            .Returns(typeof(int));
            _referenceService.Setup(s => s.GetType(_objectService.Object,
                                                   _service,
                                                   _keyStore.Object,
                                                   _referenced,
                                                   _definitions))
            .Returns(typeof(object));
            _objectService.Setup(s => s.GetType(_object,
                                                _definitions,
                                                It.IsAny <string>(),
                                                _keyStore.Object))
            .Returns(typeof(object));
        }
        public Type GetType(OpenApiObjectType inputObject,
                            IDictionary <string, IOpenApiType> definitions,
                            string name,
                            IObjectsProcessingKeyStore objectsKeyStore)
        {
            objectsKeyStore.ThrowIfPresent(name);

            var typeBuilder = _module.Builder.DefineType(name,
                                                         TypeAttributes.Public);

            objectsKeyStore.AddPresent(name);

            foreach (var property in inputObject.Properties)
            {
                var propName = property.Key;
                var propType = _typeResolver.GetType(this,
                                                     objectsKeyStore,
                                                     property.Value,
                                                     definitions,
                                                     $"{name}_{propName}");

                DefinePublicField(typeBuilder,
                                  propType,
                                  propName);
            }

            if (inputObject.AllOf != null && inputObject.AllOf.Any())
            {
                _fieldStealer.AddFields(this,
                                        objectsKeyStore,
                                        typeBuilder,
                                        inputObject.AllOf,
                                        definitions);
            }

            objectsKeyStore.RemovePresent(name);

            return(typeBuilder.CreateTypeInfo());
        }
示例#6
0
        public void TestInitialise()
        {
            _typeParser   = new Mock <IOpenApiSpecUmbrellaTypeParser <YamlCatchAllTypeModel> >();
            _objectParser = new Mock <IOpenApiSpecObjectParser <YamlCatchAllTypeModel> >();
            _enumService  = new Mock <IEnumFromStringService>();

            _service = new OpenApiYamlEndpointsParser(_typeParser.Object,
                                                      _objectParser.Object,
                                                      _enumService.Object);

            _path1GetResponseContent        = new YamlCatchAllTypeModel();
            _path1PostRequestContent        = new YamlCatchAllTypeModel();
            _path2GetResponseContent        = new YamlCatchAllTypeModel();
            _path1GetFailureResponseContent = new YamlCatchAllTypeModel();

            _parameterOpenApiType   = new OpenApiPrimitiveType();
            _path1GetParsedResponse = new OpenApiObjectType();
            _path2GetParsedResponse = new OpenApiObjectType();
            _path1PostParsedRequest = new OpenApiObjectType();

            _paths = new Dictionary <string, YamlPathModel>()
            {
                {
                    "/api/path1",
                    new YamlPathModel()
                    {
                        //path scope parameters should be handled correctly
                        Parameters = new List <YamlParameterModel>()
                        {
                            new YamlParameterModel()
                            {
                                In     = ParameterIn.query,
                                Name   = "pathScopeQueryParam",
                                Schema = new YamlCatchAllTypeModel()
                                {
                                    Type = "string"
                                }
                            }
                        },
                        Get = new YamlEndpointModel()
                        {
                            Responses = new Dictionary <string, YamlRequestResponseModel>()
                            {
                                {
                                    "200",
                                    new YamlRequestResponseModel()
                                    {
                                        Content = new Dictionary <string, YamlContentModel>()
                                        {
                                            {
                                                "application/json",
                                                new YamlContentModel()
                                                {
                                                    Schema = _path1GetResponseContent
                                                }
                                            }
                                        }
                                    }
                                },
                                // test failure responses ignored
                                {
                                    "400",
                                    new YamlRequestResponseModel()
                                    {
                                        Content = new Dictionary <string, YamlContentModel>()
                                        {
                                            {
                                                "application/json",
                                                new YamlContentModel()
                                                {
                                                    Schema = _path1GetFailureResponseContent
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        Post = new YamlEndpointModel()
                        {
                            RequestBody = new YamlRequestResponseModel()
                            {
                                Content = new Dictionary <string, YamlContentModel>()
                                {
                                    {
                                        "application/json",
                                        new YamlContentModel()
                                        {
                                            Schema = _path1PostRequestContent
                                        }
                                    }
                                }
                            },
                            Responses = new Dictionary <string, YamlRequestResponseModel>()
                            {
                                // test empty responses are parsed correctly
                                {
                                    "200",
                                    new YamlRequestResponseModel()
                                }
                            }
                        }
                    }
                },
                {
                    "/api/path2",
                    new YamlPathModel()
                    {
                        Get = new YamlEndpointModel()
                        {
                            Responses = new Dictionary <string, YamlRequestResponseModel>()
                            {
                                {
                                    //test all success statuses recognised
                                    "204",
                                    new YamlRequestResponseModel()
                                    {
                                        Content = new Dictionary <string, YamlContentModel>()
                                        {
                                            {
                                                "application/json",
                                                new YamlContentModel()
                                                {
                                                    Schema = _path2GetResponseContent
                                                }
                                            }
                                        }
                                    }
                                }
                            },

                            // endpoint scope parameters should be handled correctly
                            Parameters = new List <YamlParameterModel>()
                            {
                                new YamlParameterModel()
                                {
                                    Name   = "path2EndpointScopeHeaderParam",
                                    Schema = new YamlCatchAllTypeModel()
                                    {
                                        Type = "string"
                                    },
                                    In = ParameterIn.header
                                }
                            }
                        }
                    }
                }
            };

            _typeParser.Setup(s => s.Parse(_objectParser.Object,
                                           It.Is <YamlCatchAllTypeModel>(y => y.Type == "string")))
            .Returns(_parameterOpenApiType);
            _typeParser.Setup(s => s.Parse(_objectParser.Object,
                                           _path1GetResponseContent))
            .Returns(_path1GetParsedResponse);
            _typeParser.Setup(s => s.Parse(_objectParser.Object,
                                           _path2GetResponseContent))
            .Returns(_path2GetParsedResponse);
            _typeParser.Setup(s => s.Parse(_objectParser.Object,
                                           _path1PostRequestContent))
            .Returns(_path1PostParsedRequest);
            _enumService.Setup(s => s.ConvertStringTo <Method>("GET"))
            .Returns(Method.GET);
            _enumService.Setup(s => s.ConvertStringTo <Method>("POST"))
            .Returns(Method.POST);
        }