<commit_before>﻿// Copyright (c) <NAME>, 2016. All rights reserved.
// Licensed under the MIT license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.Api.Swagger
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Newtonsoft.Json;
    using Swashbuckle.Swagger.Model;
    using Swashbuckle.SwaggerGen.Generator;

    /// <summary>
    /// A class representing an operation filter that adds the example to use for display in Swagger documentation. This class cannot be inherited.
    /// </summary>
    internal sealed class ExampleFilter : IOperationFilter
    {
        /// <summary>
        /// The <see cref="JsonSerializerSettings"/> to use for formatting example responses. This field is read-only.
        /// </summary>
        private readonly JsonSerializerSettings _settings;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExampleFilter"/> class.
        /// </summary>
        /// <param name="settings">The <see cref="JsonSerializerSettings"/> to use.</param>
        public ExampleFilter(JsonSerializerSettings settings)
        {
            _settings = settings;
        }

        /// <inheritdoc />
        public void Apply(Operation operation, OperationFilterContext context)
        {
            if (operation != null && context?.ApiDescription != null && context.SchemaRegistry != null)
            {
                var responseAttributes = context.ApiDescription.GetActionAttributes().OfType<SwaggerResponseExampleAttribute>();

                foreach (var attribute in responseAttributes)
                {
                    var schema = context.SchemaRegistry.GetOrRegister(attribute.ResponseType);

                    var response = operation.Responses
                        .Where((p) => p.Value.Schema.Type == schema.Type)
                        .Where((p) => p.Value.Schema.Ref == schema.Ref)
                        .First()
                        .Value;

                    if (response != null)
                    {
                        var provider = (IExampleProvider)Activator.CreateInstance(attribute.ExampleType);
                        response.Examples = FormatAsJson(provider);
                    }
                }
            }
        }
<commit_msg>Fix NullReferenceException if no schema<commit_after>﻿// Copyright (c) <NAME>, 2016. All rights reserved.
// Licensed under the MIT license. See the LICENSE file in the project root for full license information.

namespace MartinCostello.Api.Swagger
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Newtonsoft.Json;
    using Swashbuckle.Swagger.Model;
    using Swashbuckle.SwaggerGen.Generator;

    /// <summary>
    /// A class representing an operation filter that adds the example to use for display in Swagger documentation. This class cannot be inherited.
    /// </summary>
    internal sealed class ExampleFilter : IOperationFilter
    {
        /// <summary>
        /// The <see cref="JsonSerializerSettings"/> to use for formatting example responses. This field is read-only.
        /// </summary>
        private readonly JsonSerializerSettings _settings;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExampleFilter"/> class.
        /// </summary>
        /// <param name="settings">The <see cref="JsonSerializerSettings"/> to use.</param>
        public ExampleFilter(JsonSerializerSettings settings)
        {
            _settings = settings;
        }

        /// <inheritdoc />
        public void Apply(Operation operation, OperationFilterContext context)
        {
            if (operation != null && context?.ApiDescription != null && context.SchemaRegistry != null)
            {
                var responseAttributes = context.ApiDescription.GetActionAttributes().OfType<SwaggerResponseExampleAttribute>();

                foreach (var attribute in responseAttributes)
                {
                    var schema = context.SchemaRegistry.GetOrRegister(attribute.ResponseType);

                    var response = operation.Responses
                        .Where((p) => p.Value.Schema?.Type == schema.Type)
                        .Where((p) => p.Value.Schema?.Ref == schema.Ref)
                        .Select((p) => p.Value)
                        .FirstOrDefault();

                    if (response != null)
                    {
                        var provider = (IExampleProvider)Activator.CreateInstance(attribute.ExampleType);
                        response.Examples = FormatAsJson(provider);
                    }
                }
            }
        }

{'fraction_non_alphanumeric': 0.09643916913946587, 'fraction_numerical': 0.001695633743111488, 'mean_word_length': 1.8915441176470589, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '906488', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1047, 'n_words': 313}