Exemple #1
0
        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)
            {
                var serializerSettings = serializerSettingsDuplicator.SerializerSettings(contractResolver, jsonConverter);
                var jsonExample        = new OpenApiString(jsonFormatter.FormatJson(example, serializerSettings));

                OpenApiString xmlExample = null;
                if (response.Value.Content.Keys.Any(k => k.Contains("xml")))
                {
                    xmlExample = new OpenApiString(example.XmlSerialize());
                }

                foreach (var content in response.Value.Content)
                {
                    content.Value.Example = content.Key.Contains("xml") ? xmlExample : jsonExample;
                }
            }
        }
        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 jsonExample        = new OpenApiString(jsonFormatter.FormatJson(example, serializerSettings));

            OpenApiString xmlExample = null;

            if (operation.RequestBody.Content.Keys.Any(k => k.Contains("xml")))
            {
                xmlExample = new OpenApiString(example.XmlSerialize());
            }

            foreach (var content in operation.RequestBody.Content)
            {
                content.Value.Example = content.Key.Contains("xml") ? xmlExample : jsonExample;
            }
        }
Exemple #3
0
        public void SetRequestExampleForType(
            Operation operation,
            ISchemaRegistry schemaRegistry,
            Type requestType,
            object example,
            IContractResolver contractResolver = null,
            JsonConverter jsonConverter        = null)
        {
            if (example == null)
            {
                return;
            }

            var schema = schemaRegistry.GetOrRegister(requestType);

            var bodyParameters = operation.Parameters.Where(p => p.In == "body").Cast <BodyParameter>();
            var bodyParameter  = bodyParameters.FirstOrDefault(p => p?.Schema.Ref == schema.Ref || p.Schema?.Items?.Ref == schema.Ref);

            if (bodyParameter == null)
            {
                bodyParameter = new BodyParameter()
                {
                    In          = "body",
                    Name        = "body",
                    Schema      = schema,
                    Description = "",
                };
                // return; // The type in their [SwaggerRequestExample(typeof(requestType), ...] is not passed to their controller action method
            }

            operation.Consumes.Add("application/json");
            operation.Parameters.Add(bodyParameter);

            var    serializerSettings = this.jsonSerializerSettings ?? serializerSettingsDuplicator.SerializerSettings(contractResolver, jsonConverter);
            var    formattedExample   = jsonFormatter.FormatJson(example, serializerSettings, includeMediaType: false);
            string name = SchemaDefinitionName(requestType, schema);

            // Console.Out.WriteLine($"   SchemaDefinitionName: {name}");
            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            // set the example on the object in the schema registry (this is what swagger-ui will display)
            if (schemaRegistry.Definitions.ContainsKey(name))
            {
                //Console.Out.WriteLine($"   Registry contains key");
                var definitionToUpdate = schemaRegistry.Definitions[name];
                if (definitionToUpdate.Example == null)
                {
                    definitionToUpdate.Example = formattedExample;
                }
            }
            else
            {
                //Console.Out.WriteLine($"   Registry does not contain key");
                bodyParameter.Schema.Example = formattedExample; // set example on the request paths/parameters/schema/example property
            }
        }
        public void SetRequestExampleForType(
            Operation operation,
            ISchemaRegistry schemaRegistry,
            Type requestType,
            object example,
            IContractResolver contractResolver = null,
            JsonConverter jsonConverter        = null)
        {
            if (example == null)
            {
                return;
            }

            var schema = schemaRegistry.GetOrRegister(requestType);

            var bodyParameters = operation.Parameters.Where(p => p.In == "body").Cast <BodyParameter>();
            var bodyParameter  = bodyParameters.FirstOrDefault(p => p?.Schema.Ref == schema.Ref || p.Schema?.Items?.Ref == schema.Ref);

            if (bodyParameter == null)
            {
                return; // The type in their [SwaggerRequestExample(typeof(requestType), ...] is not passed to their controller action method
            }

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

            var formattedExample = jsonFormatter.FormatJson(example, serializerSettings, includeMediaType: false);

            bodyParameter.Schema.Example = formattedExample; // set example on the paths/parameters/schema/example property

            string name = SchemaDefinitionName(requestType, schema);

            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            // now that we have the name, additionally set the example on the object in the schema registry (this is what swagger-ui will display)
            if (schemaRegistry.Definitions.ContainsKey(name))
            {
                var definitionToUpdate = schemaRegistry.Definitions[name];
                if (definitionToUpdate.Example == null)
                {
                    definitionToUpdate.Example = formattedExample;
                }
            }
        }
        public void SetResponseExampleForStatusCode(
            Operation 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, Response>)) == false && response.Value != null)
            {
                var serializerSettings = jsonSerializerSettings ?? serializerSettingsDuplicator.SerializerSettings(contractResolver, jsonConverter);
                response.Value.Examples = jsonFormatter.FormatJson(example, serializerSettings, includeMediaType: true);
            }
        }
 public IOpenApiAny SerializeExampleJson(object exampleValue)
 {
     return(new OpenApiRawString(jsonFormatter.FormatJson(exampleValue, serializerSettings)));
 }