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 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); } }
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; } }
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 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; } } } }
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); } }