コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: SwaggerDocument.cs プロジェクト: tuongntk/NSwag
        /// <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));
        }
コード例 #3
0
        /// <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));
        }
コード例 #4
0
        /// <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;
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
ファイル: SyncService.cs プロジェクト: slalFe/squidex-samples
        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);
                }
            }
        }