public void SetRequestExampleForOperation(
            OpenApiOperation operation,
            object example,
            IContractResolver contractResolver = null,
            JsonConverter jsonConverter        = null)
        {
            if (example == null)
            {
                return;
            }

            if (operation.RequestBody == null || operation.RequestBody.Content == null)
            {
                return;
            }

            var serializerSettings = serializerSettingsDuplicator.SerializerSettings(contractResolver, jsonConverter);

            var examplesConverter = new ExamplesConverter(jsonFormatter, mvcOutputFormatter, serializerSettings);

            var multiple = example as IEnumerable <ISwaggerExample <object> >;

            if (multiple == null)
            {
                SetSingleRequestExampleForOperation(operation, example, examplesConverter);
            }
            else
            {
                SetMultipleRequestExamplesForOperation(operation, multiple, examplesConverter);
            }
        }
Example #2
0
        /// <summary>
        /// Sets multiple examples on the operation for all of the operation's content types
        /// </summary>
        /// <returns>The first example so that it can be reused on the definition for V2</returns>
        private IOpenApiAny SetMultipleRequestExamplesForOperation(
            OpenApiOperation operation,
            IEnumerable <ISwaggerExample <object> > examples,
            ExamplesConverter examplesConverter)
        {
            var jsonExamples = new Lazy <IDictionary <string, OpenApiExample> >(() =>
                                                                                examplesConverter.ToOpenApiExamplesDictionaryJson(examples)
                                                                                );

            var xmlExamples = new Lazy <IDictionary <string, OpenApiExample> >(() =>
                                                                               examplesConverter.ToOpenApiExamplesDictionaryXml(examples)
                                                                               );

            foreach (var content in operation.RequestBody.Content)
            {
                if (content.Key.Contains("xml"))
                {
                    content.Value.Examples = xmlExamples.Value;
                }
                else
                {
                    content.Value.Examples = jsonExamples.Value;
                }
            }

            return(operation.RequestBody.Content.FirstOrDefault().Value?.Examples?.FirstOrDefault().Value?.Value);
        }
        private void SetMultipleResponseExampleForStatusCode(
            KeyValuePair <string, OpenApiResponse> response,
            IEnumerable <ISwaggerExample <object> > examples,
            ExamplesConverter examplesConverter)
        {
            var jsonExamples = new Lazy <IDictionary <string, OpenApiExample> >(() =>
                                                                                examplesConverter.ToOpenApiExamplesDictionaryJson(examples)
                                                                                );

            var xmlExamples = new Lazy <IDictionary <string, OpenApiExample> >(() =>
                                                                               examplesConverter.ToOpenApiExamplesDictionaryXml(examples)
                                                                               );

            foreach (var content in response.Value.Content)
            {
                if (content.Key.Contains("xml"))
                {
                    content.Value.Examples = xmlExamples.Value;
                }
                else
                {
                    content.Value.Examples = jsonExamples.Value;
                }
            }
        }
        public void SetResponseExampleForStatusCode(
            OpenApiOperation operation,
            int statusCode,
            object example,
            IContractResolver contractResolver = null,
            JsonConverter jsonConverter        = null)
        {
            if (example == null)
            {
                return;
            }

            var response = operation.Responses.FirstOrDefault(r => r.Key == statusCode.ToString());

            if (response.Equals(default(KeyValuePair <string, OpenApiResponse>)) != false || response.Value == null)
            {
                return;
            }

            var serializerSettings = serializerSettingsDuplicator.SerializerSettings(contractResolver, jsonConverter);

            var examplesConverter = new ExamplesConverter(jsonFormatter, mvcOutputFormatter, serializerSettings);

            var multiple = example as IEnumerable <ISwaggerExample <object> >;

            if (multiple == null)
            {
                SetSingleResponseExampleForStatusCode(response, example, examplesConverter);
            }
            else
            {
                SetMultipleResponseExampleForStatusCode(response, multiple, examplesConverter);
            }
        }
        public void SetResponseExampleForStatusCode(
            OpenApiOperation operation,
            int statusCode,
            object example)
        {
            if (example == null)
            {
                return;
            }

            var key      = statusCode == 0 ? "default" : statusCode.ToString();
            var response = operation.Responses.FirstOrDefault(r => r.Key == key);

            if (response.Equals(default(KeyValuePair <string, OpenApiResponse>)) || response.Value == null)
            {
                return;
            }

            var examplesConverter = new ExamplesConverter(mvcOutputFormatter);

            var multiple = example as IEnumerable <ISwaggerExample <object> >;

            if (multiple == null)
            {
                SetSingleResponseExampleForStatusCode(response, example, examplesConverter);
            }
            else
            {
                SetMultipleResponseExampleForStatusCode(response, multiple, examplesConverter);
            }
        }
Example #6
0
        public void SetRequestExampleForOperation(
            OpenApiOperation operation,
            SchemaRepository schemaRepository,
            Type requestType,
            object example,
            IContractResolver contractResolver = null,
            JsonConverter jsonConverter        = null)
        {
            if (example == null)
            {
                return;
            }

            if (operation.RequestBody == null || operation.RequestBody.Content == null)
            {
                return;
            }

            var serializerSettings = serializerSettingsDuplicator.SerializerSettings(contractResolver, jsonConverter);

            var examplesConverter = new ExamplesConverter(jsonFormatter, mvcOutputFormatter, serializerSettings);

            IOpenApiAny firstOpenApiExample;
            var         multiple = example as IEnumerable <ISwaggerExample <object> >;

            if (multiple == null)
            {
                firstOpenApiExample = SetSingleRequestExampleForOperation(operation, example, examplesConverter);
            }
            else
            {
                firstOpenApiExample = SetMultipleRequestExamplesForOperation(operation, multiple, examplesConverter);
            }

            if (swaggerOptions.SerializeAsV2)
            {
                // Swagger v2 doesn't have a request example on the path
                // Fallback to setting it on the object in the "definitions"

                string schemaDefinitionName = requestType.SchemaDefinitionName();
                if (schemaRepository.Schemas.ContainsKey(schemaDefinitionName))
                {
                    var schemaDefinition = schemaRepository.Schemas[schemaDefinitionName];
                    if (schemaDefinition.Example == null)
                    {
                        schemaDefinition.Example = firstOpenApiExample;
                    }
                }
            }
        }
        private void SetSingleRequestExampleForOperation(
            OpenApiOperation operation,
            object example,
            ExamplesConverter examplesConverter)
        {
            var jsonExample = new Lazy <IOpenApiAny>(() => examplesConverter.SerializeExampleJson(example));
            var xmlExample  = new Lazy <IOpenApiAny>(() => examplesConverter.SerializeExampleXml(example));

            foreach (var content in operation.RequestBody.Content)
            {
                if (content.Key.Contains("xml"))
                {
                    content.Value.Example = xmlExample.Value;
                }
                else
                {
                    content.Value.Example = jsonExample.Value;
                }
            }
        }
        private void SetSingleResponseExampleForStatusCode(
            KeyValuePair <string, OpenApiResponse> response,
            object example,
            ExamplesConverter examplesConverter)
        {
            var jsonExample = new Lazy <IOpenApiAny>(() => examplesConverter.SerializeExampleJson(example));
            var xmlExample  = new Lazy <IOpenApiAny>(() => examplesConverter.SerializeExampleXml(example));

            foreach (var content in response.Value.Content)
            {
                if (content.Key.Contains("xml"))
                {
                    content.Value.Example = xmlExample.Value;
                }
                else
                {
                    content.Value.Example = jsonExample.Value;
                }
            }
        }