コード例 #1
0
ファイル: SwaggerOptions.cs プロジェクト: batesiiic/Ahoy
        public SwaggerOptions()
        {
            SwaggerGeneratorOptions = new SwaggerGeneratorOptions();
            SwaggerGeneratorOptions.OperationFilters.Add(new ApplySwaggerOperationAttributes());
            SwaggerGeneratorOptions.OperationFilters.Add(new ApplySwaggerResponseAttributes());

            SchemaGeneratorOptions = new SchemaGeneratorOptions();
        }
コード例 #2
0
        private SchemaGenerator Subject(
            Action <SchemaGeneratorOptions> configureGenerator = null,
            Action <JsonSerializerOptions> configureSerializer = null)
        {
            var generatorOptions = new SchemaGeneratorOptions();

            configureGenerator?.Invoke(generatorOptions);

            var serializerOptions = new JsonSerializerOptions();

            configureSerializer?.Invoke(serializerOptions);

            return(new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions)));
        }
コード例 #3
0
        private SchemaGenerator Subject(
            Action <SchemaGeneratorOptions> configureGenerator  = null,
            Action <JsonSerializerSettings> configureSerializer = null)
        {
            var generatorOptions = new SchemaGeneratorOptions();

            configureGenerator?.Invoke(generatorOptions);

            var serializerSettings = new JsonSerializerSettings();

            configureSerializer?.Invoke(serializerSettings);

            return(new SchemaGenerator(generatorOptions, new NewtonsoftMetadataResolver(generatorOptions, serializerSettings)));
        }
コード例 #4
0
        private ISchemaGenerator Subject(
            Action <SchemaGeneratorOptions> configureOptions   = null,
            Action <JsonSerializerOptions> configureSerializer = null)
        {
            var jsonSerializerOptions = new JsonSerializerOptions();

            configureSerializer?.Invoke(jsonSerializerOptions);

            var schemaGeneratorOptions = new SchemaGeneratorOptions();

            configureOptions?.Invoke(schemaGeneratorOptions);

            return(new SchemaGenerator(new JsonApiModelResolver(jsonSerializerOptions), schemaGeneratorOptions));
        }
コード例 #5
0
        private ISchemaGenerator Subject(
            Action <SchemaGeneratorOptions> configureOptions    = null,
            Action <JsonSerializerSettings> configureSerializer = null)
        {
            var options = new SchemaGeneratorOptions();

            configureOptions?.Invoke(options);

            var serializerSettings = new JsonSerializerSettings();

            configureSerializer?.Invoke(serializerSettings);

            return(new SchemaGenerator(options, serializerSettings));
        }
コード例 #6
0
        public SchemaGenerator SchemaGenerator(
            Action <SchemaGeneratorOptions> configureGenerator = null,
            Action <JsonSerializerOptions> configureSerializer = null)
        {
            var generatorOptions = new SchemaGeneratorOptions();

            configureGenerator?.Invoke(generatorOptions);

            var serializerOptions = new JsonSerializerOptions();

            configureSerializer?.Invoke(serializerOptions);

            return(new SchemaGenerator(generatorOptions, new JsonSerializerMetadataResolver(serializerOptions)));
        }
        private SchemaGenerator Subject(
            Action <SchemaGeneratorOptions> configureGenerator = null,
            Action <JsonSerializerOptions> configureSerializer = null)
        {
            var generatorOptions = new SchemaGeneratorOptions();

            configureGenerator?.Invoke(generatorOptions);

            var serializerOptions = new JsonSerializerOptions();

            configureSerializer?.Invoke(serializerOptions);

            return(new SchemaGenerator(generatorOptions, new SystemTextJsonBehavior(serializerOptions)));
        }
コード例 #8
0
        public ServiceProviderExamplesOperationFilterTests()
        {
            schemaGeneratorOptions = new SchemaGeneratorOptions();

            var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlAndNewtonsoftJsonFormatters, new FakeLoggerFactory());

            var requestExample  = new RequestExample(mvcOutputFormatter, Options.Create(swaggerOptions));
            var responseExample = new ResponseExample(mvcOutputFormatter);

            serviceProvider = Substitute.For <IServiceProvider>();
            serviceProvider.GetService(typeof(IExamplesProvider <PersonResponse>)).Returns(new PersonResponseAutoExample());

            sut = new ServiceProviderExamplesOperationFilter(serviceProvider, requestExample, responseExample);
        }
コード例 #9
0
        public NewtonsoftSchemaGenerator(SchemaGeneratorOptions generatorOptions, JsonSerializerSettings serializerSettings)
            : base(generatorOptions)
        {
            var contractResolver = serializerSettings.ContractResolver ?? new DefaultContractResolver();

            AddHandler(new FileTypeHandler());
            AddHandler(new PolymorphicTypeHandler(generatorOptions, this));
            AddHandler(new PrimitiveTypeHandler());
            AddHandler(new NewtonsoftLinqTypeHandler());
            AddHandler(new NewtonsoftEnumHandler(generatorOptions, contractResolver, serializerSettings));
            AddHandler(new NewtonsoftDictionaryHandler(contractResolver, this));
            AddHandler(new NewtonsoftArrayHandler(contractResolver, this));
            AddHandler(new NewtonsoftObjectHandler(generatorOptions, contractResolver, this));
        }
        protected OperationFilterContext FilterContextFor(Type controllerType, string actionName, IContractResolver contractResolver, List <ApiParameterDescription> parameterDescriptions = null, List <ApiResponseType> supportedResponseTypes = null)
        {
            var apiDescription = new ApiDescription
            {
                ActionDescriptor = new ControllerActionDescriptor
                {
                    ControllerTypeInfo = controllerType.GetTypeInfo(),
                    MethodInfo         = controllerType.GetMethod(actionName),
                }
            };

            if (parameterDescriptions != null)
            {
                apiDescription.With(api => api.ParameterDescriptions, parameterDescriptions);
            }

            if (supportedResponseTypes != null)
            {
                apiDescription.With(api => api.SupportedResponseTypes, supportedResponseTypes);
            }

            var jsonSerializerSettings = new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            };

            var schemaOptions = new SchemaGeneratorOptions();

            var schemaRepository = new SchemaRepository();

            var methodInfo = controllerType.GetMethod(actionName);

            foreach (var parameterInfo in methodInfo.GetParameters())
            {
                schemaRepository.GetOrAdd(parameterInfo.ParameterType, parameterInfo.ParameterType.SchemaDefinitionName(), () => new OpenApiSchema()
                {
                    Reference = new OpenApiReference {
                        Id = parameterInfo.Name
                    }
                });
            }

            return(new OperationFilterContext(
                       apiDescription,
                       new NewtonsoftSchemaGenerator(schemaOptions, jsonSerializerSettings),
                       schemaRepository,
                       (apiDescription.ActionDescriptor as ControllerActionDescriptor).MethodInfo));
        }
コード例 #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyContainerSchemaFilter"/> class.
        /// </summary>
        /// <param name="options">Options.</param>
        /// <param name="serializerOptions">JsonSerializerOptions.</param>
        /// <param name="generatorOptions">Swagger generator options.</param>
        public PropertyContainerSchemaFilter(
            PropertyContainerSchemaFilterOptions?options,
            IOptions <JsonSerializerOptions> serializerOptions,
            SwaggerGenOptions generatorOptions)
        {
            _options                = options?.Clone() ?? new PropertyContainerSchemaFilterOptions();
            _serializerOptions      = serializerOptions.Value;
            _schemaGeneratorOptions = generatorOptions.SchemaGeneratorOptions;

            Func <string, string>?resolvePropertyName = options?.ResolvePropertyName;

            resolvePropertyName ??= propertyName => _serializerOptions.PropertyNamingPolicy.ConvertName(propertyName);
            resolvePropertyName ??= propertyName => propertyName;

            _options.ResolvePropertyName = resolvePropertyName;
        }
        public ServiceProviderExamplesOperationFilterTests()
        {
            var mvcJsonOptions = Options.Create(new MvcJsonOptions());

            schemaGeneratorOptions = new SchemaGeneratorOptions();
            var serializerSettingsDuplicator = new SerializerSettingsDuplicator(mvcJsonOptions, Options.Create(schemaGeneratorOptions));

            var jsonFormatter = new JsonFormatter();

            var requestExample  = new RequestExample(jsonFormatter, serializerSettingsDuplicator);
            var responseExample = new ResponseExample(jsonFormatter, serializerSettingsDuplicator);

            serviceProvider = Substitute.For <IServiceProvider>();
            serviceProvider.GetService(typeof(IExamplesProvider <PersonResponse>)).Returns(new PersonResponseAutoExample());

            sut = new ServiceProviderExamplesOperationFilter(serviceProvider, requestExample, responseExample);
        }
コード例 #13
0
        private OperationFilterContext FilterContextFor(string fakeActionName)
        {
            var apiDescription = new ApiDescription
            {
                ActionDescriptor = new ControllerActionDescriptor
                {
                    ControllerTypeInfo = typeof(TestController).GetTypeInfo(),
                    MethodInfo         = typeof(TestController).GetMethod(fakeActionName)
                }
            };

            var generatorOptions = new SchemaGeneratorOptions();

            return(new OperationFilterContext(
                       apiDescription,
                       new SchemaGenerator(generatorOptions, new JsonSerializerMetadataResolver(new JsonSerializerOptions())),
                       new SchemaRepository(),
                       (apiDescription.ActionDescriptor as ControllerActionDescriptor).MethodInfo));
        }
        public ExamplesOperationFilterTests()
        {
            var mvcJsonOptions = Options.Create(new MvcJsonOptions());

            schemaGeneratorOptions = new SchemaGeneratorOptions();
            var serializerSettingsDuplicator = new SerializerSettingsDuplicator(mvcJsonOptions, Options.Create(schemaGeneratorOptions));

            var jsonFormatter      = new JsonFormatter();
            var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithoutFormatters, new FakeLoggerFactory());

            var serviceProvider = Substitute.For <IServiceProvider>();

            serviceProvider.GetService(typeof(PersonResponseExample)).Returns(new PersonResponseExample());
            serviceProvider.GetService(typeof(PersonRequestExample)).Returns(new PersonRequestExample());
            serviceProvider.GetService(typeof(DictionaryRequestExample)).Returns(new DictionaryRequestExample());

            var requestExample  = new RequestExample(jsonFormatter, serializerSettingsDuplicator, mvcOutputFormatter);
            var responseExample = new ResponseExample(jsonFormatter, serializerSettingsDuplicator, mvcOutputFormatter);

            sut = new ExamplesOperationFilter(serviceProvider, requestExample, responseExample);
        }
コード例 #15
0
        private SwaggerGenerator Subject(
            Action <FakeApiDescriptionGroupCollectionProvider> setupApis = null,
            Action <SwaggerGeneratorOptions> setupAction = null)
        {
            var apiDescriptionsProvider = new FakeApiDescriptionGroupCollectionProvider();

            setupApis?.Invoke(apiDescriptionsProvider);

            var options = new SwaggerGeneratorOptions();

            options.SwaggerDocs.Add("v1", new OpenApiInfo {
                Title = "API", Version = "v1"
            });

            setupAction?.Invoke(options);

            var schemaOptions = new SchemaGeneratorOptions();

            return(new SwaggerGenerator(
                       apiDescriptionsProvider,
                       new SchemaGenerator(new NewtonsoftApiModelResolver(new JsonSerializerSettings(), schemaOptions), schemaOptions),
                       options
                       ));
        }
コード例 #16
0
 public SerializerSettingsDuplicator(IOptions <MvcJsonOptions> mvcJsonOptions, IOptions <SchemaGeneratorOptions> schemaGeneratorOptions)
 {
     this.jsonSerializerSettings = mvcJsonOptions.Value.SerializerSettings;
     this.schemaGeneratorOptions = schemaGeneratorOptions.Value;
 }
コード例 #17
0
 public SCIMSchemaGenerator(ILogger <SCIMSchemaGenerator> logger, IServiceProvider serviceProvider, SchemaGeneratorOptions generatorOptions, IDataContractResolver dataContractResolver)
 {
     _logger                    = logger;
     _serviceProvider           = serviceProvider;
     _generatorOptions          = generatorOptions;
     _dataContractResolver      = dataContractResolver;
     _scimSchemaQueryRepository = (ISCIMSchemaQueryRepository)_serviceProvider.GetService(typeof(ISCIMSchemaQueryRepository));
 }
コード例 #18
0
 public NewtonsoftDataContractResolver(SchemaGeneratorOptions generatorOptions, JsonSerializerSettings serializerSettings)
 {
     _generatorOptions   = generatorOptions;
     _serializerSettings = serializerSettings;
     _contractResolver   = serializerSettings.ContractResolver ?? new DefaultContractResolver();
 }
コード例 #19
0
        public static void Init(TestContext testContext)
        {
            var knownTypesOptions = Options.Create(new DotvvmApiOptions());

            knownTypesOptions.Value.AddKnownType(typeof(Company <string>));

            var options = new SwaggerGeneratorOptions
            {
                DocInclusionPredicate = (version, api) => true,
                OperationFilters      =
                {
                    new RemoveReadOnlyFromUriParametersOperationFilter(),
                    new RemoveBindNoneFromUriParametersOperationFilter(),
                    new AddAsObjectOperationFilter(knownTypesOptions)
                },
                DocumentFilters =
                {
                    new HandleKnownTypesDocumentFilter(knownTypesOptions)
                },
                SwaggerDocs =
                {
                    { "v1", new OpenApiInfo()
                        {
                            Title = "Test API", Version = "v1"
                        } }
                }
            };

            var serviceCollection = new ServiceCollection()
                                    .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
                                    .AddSingleton <DiagnosticSource>(p => new DiagnosticListener("test"))
                                    .AddLogging();

            serviceCollection.AddMvc(setup => setup.Conventions.Add(new ApiExplorerVisibilityEnabledConvention()))
            .AddApplicationPart(typeof(CompaniesController).Assembly);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var apiDescriptionGroupCollectionProvider = serviceProvider.GetRequiredService <IApiDescriptionGroupCollectionProvider>();

            string GetCustomSchemaId(Type modelType)
            {
                if (!modelType.IsConstructedGenericType)
                {
                    return(modelType.Name.Replace("[]", "Array"));
                }

                var generics = modelType.GetGenericArguments()
                               .Select(genericArg => GetCustomSchemaId(genericArg))
                               .Aggregate((previous, current) => previous + current);

                return($"{modelType.Name.Split('`').First()}[{generics}]");
            }

            var schemaGeneratorOptions = new SchemaGeneratorOptions()
            {
                SchemaFilters =
                {
                    new AddTypeToModelSchemaFilter()
                },
                SchemaIdSelector = type => GetCustomSchemaId(type)
            };

            var schemaGenerator  = new SchemaGenerator(schemaGeneratorOptions, new JsonSerializerDataContractResolver(new JsonSerializerOptions()));
            var swaggerGenerator = new SwaggerGenerator(options, apiDescriptionGroupCollectionProvider, schemaGenerator);

            document = swaggerGenerator.GetSwagger("v1");
        }
コード例 #20
0
 public NewtonsoftApiModelResolver(JsonSerializerSettings jsonSerializerSettings, SchemaGeneratorOptions options)
 {
     _jsonSerializerSettings = jsonSerializerSettings;
     _jsonContractResolver   = jsonSerializerSettings.ContractResolver ?? new DefaultContractResolver();
     _options = options;
 }
コード例 #21
0
 public StizeSchemaGenerator(SchemaGeneratorOptions generatorOptions, ISerializerDataContractResolver serializerDataContractResolver)
 {
     this.defaultSchemaGenerator = new SchemaGenerator(generatorOptions, serializerDataContractResolver);
 }
コード例 #22
0
        private void ConfigureGenerator(SchemaGeneratorOptions options, params IValidator[] validators)
        {
            IValidatorFactory validatorFactory = new CustomValidatorFactory(validators);

            options.SchemaFilters.Add(new FluentValidationRules(validatorFactory));
        }
コード例 #23
0
 public SchemaGeneratorWrapper(SchemaGeneratorOptions generatorOptions, ISerializerDataContractResolver defaultResolver, IEnumerable <IJsonRpcSerializer> serializers)
 {
     this.generatorOptions = generatorOptions;
     this.defaultResolver  = defaultResolver;
     this.serializers      = serializers;
 }
コード例 #24
0
        private SchemaGenerator Subject(Action<SchemaGeneratorOptions> configureOptions = null)
        {
            var options = new SchemaGeneratorOptions();
            if (configureOptions != null) configureOptions(options);

            return new SchemaGenerator(new JsonSerializerSettings(), options);
        }