public void When_schema_is_serialized_for_Swagger_then_discriminator_is_string() { //// Arrange var childSchema = new JsonSchema4 { Type = JsonObjectType.Object, }; var schema = new JsonSchema4(); schema.Definitions["Foo"] = childSchema; schema.DiscriminatorObject = new OpenApiDiscriminator { PropertyName = "discr", Mapping = { { "Bar", new JsonSchema4 { Reference = childSchema } } } }; //// Act var json = JsonSchemaSerialization.ToJson(schema, SchemaType.Swagger2, new DefaultContractResolver(), Formatting.Indented); //// Assert Assert.Contains(@"""discriminator"": ""discr""", json); Assert.DoesNotContain(@"""Bar"": ""#/definitions/Foo""", json); }
/// <summary>Converts the description object to JSON.</summary> /// <param name="schemaType">The schema type.</param> /// <returns>The JSON string.</returns> public string ToJson(SchemaType schemaType) { GenerateOperationIds(); var contractResolver = CreateJsonSerializerContractResolver(schemaType); return(JsonSchemaSerialization.ToJson(this, schemaType, contractResolver)); }
/// <summary>Converts the description object to JSON.</summary> /// <param name="schemaType">The schema type.</param> /// <param name="formatting">The formatting.</param> /// <returns>The JSON string.</returns> public string ToJson(SchemaType schemaType, Formatting formatting) { GenerateOperationIds(); var contractResolver = GetJsonSerializerContractResolver(schemaType); return(JsonSchemaSerialization.ToJson(this, schemaType, contractResolver, formatting)); }
/// <summary>Serializes the <see cref="JsonSchema4" /> to a JSON string.</summary> /// <param name="formatting">The formatting.</param> /// <returns>The JSON string.</returns> public string ToJson(Formatting formatting) { var oldSchema = SchemaVersion; SchemaVersion = "http://json-schema.org/draft-04/schema#"; var json = JsonSchemaSerialization.ToJson(this, SerializationSchemaType, ContractResolver.Value, formatting); SchemaVersion = oldSchema; return json; }
public void When_AllowAdditionalProperties_is_true_then_JSON_is_correct(SchemaType schemaType, bool allowAdditionalProperties, string expectedJson) { //// Act var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); var json = JsonSchemaSerialization.ToJson(new JsonSchema { AllowAdditionalProperties = allowAdditionalProperties }, schemaType, new DefaultContractResolver(), Formatting.None); //// Assert Assert.Equal(expectedJson, json); }
public void When_default_schema_is_serialized_then_AllowAdditionalProperties_is_correct(SchemaType schemaType, string expectedJson) { // default schema (new JsonSchema) has always AllowAdditionalProperties = true //// Act var factory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new DefaultTypeNameGenerator()); var json = JsonSchemaSerialization.ToJson(new JsonSchema(), schemaType, new DefaultContractResolver(), Formatting.None); //// Assert Assert.Equal(expectedJson, json); }
/// <summary>Serializes the <see cref="JsonSchema4" /> to a JSON string.</summary> /// <returns>The JSON string.</returns> public string ToJson() { var oldSchema = SchemaVersion; SchemaVersion = "http://json-schema.org/draft-04/schema#"; var schemaType = SchemaType.JsonSchema; var contractResolver = CreateJsonSerializerContractResolver(schemaType); var json = JsonSchemaSerialization.ToJson(this, schemaType, contractResolver); SchemaVersion = oldSchema; return(json); }
public async Task WriteJsonSchemaAsync <T>(IFile file) { await using (var stream = file.OpenWrite()) { await using (var textWriter = new StreamWriter(stream)) { var jsonSchema = GetSchema <T>(); var jsonSchemaType = jsonSchemaGeneratorSettings.SchemaType; var json = JsonSchemaSerialization.ToJson( jsonSchema, jsonSchemaType, JsonSchema.CreateJsonSerializerContractResolver(jsonSchemaType), Formatting.Indented); await textWriter.WriteAsync(json); } } }