예제 #1
0
        public JsonSchemaProperty?Visit(IArrayField field, Args args)
        {
            if (args.Level > MaxDepth)
            {
                return(null);
            }

            var itemSchema = SchemaBuilder.Object();

            var nestedArgs = args.Increment();

            foreach (var nestedField in field.Fields.ForApi(args.WithHiddenFields))
            {
                var nestedProperty = nestedField.Accept(this, nestedArgs);

                if (nestedProperty != null)
                {
                    nestedProperty.Description = nestedField.RawProperties.Hints;
                    nestedProperty.SetRequired(nestedField.RawProperties.IsRequired);

                    itemSchema.Properties.Add(nestedField.Name, nestedProperty);
                }
            }

            return(SchemaBuilder.ArrayProperty(itemSchema));
        }
예제 #2
0
        public JsonSchemaProperty?Visit(IField <GeolocationFieldProperties> field)
        {
            var geolocationSchema = SchemaBuilder.Object();

            geolocationSchema.Properties.Add("latitude", new JsonSchemaProperty
            {
                Type          = JsonObjectType.Number,
                IsNullableRaw = false,
                IsRequired    = true,
                Maximum       = 90,
                Minimum       = -90
            });

            geolocationSchema.Properties.Add("longitude", new JsonSchemaProperty
            {
                Type          = JsonObjectType.Number,
                IsNullableRaw = false,
                IsRequired    = true,
                Maximum       = 180,
                Minimum       = -180
            });

            var reference = schemaResolver("GeolocationDto", geolocationSchema);

            return(SchemaBuilder.ObjectProperty(reference));
        }
예제 #3
0
        public JsonSchemaProperty?Visit(IField <GeolocationFieldProperties> field, Args args)
        {
            var reference = args.SchemaResolver("GeolocationDto", () =>
            {
                var geolocationSchema = SchemaBuilder.Object();

                geolocationSchema.Format = GeoJson.Format;

                geolocationSchema.Properties.Add("latitude", new JsonSchemaProperty
                {
                    Type    = JsonObjectType.Number,
                    Maximum = 90,
                    Minimum = -90
                }.SetRequired(true));

                geolocationSchema.Properties.Add("longitude", new JsonSchemaProperty
                {
                    Type    = JsonObjectType.Number,
                    Maximum = 180,
                    Minimum = -180
                }.SetRequired(true));

                return(geolocationSchema);
            });

            return(SchemaBuilder.ObjectProperty(reference));
        }
예제 #4
0
        private void BuildComponent(JsonSchema jsonSchema, IField field, ImmutableList <DomainId>?schemaIds, Args args)
        {
            jsonSchema.Properties.Add(Component.Discriminator, SchemaBuilder.StringProperty(isRequired: true));

            if (args.SchemaResolver != null)
            {
                var schemas = schemaIds?.Select(x => field.GetResolvedSchema(x)).NotNull() ?? Enumerable.Empty <Schema>();

                var discriminator = new OpenApiDiscriminator
                {
                    PropertyName = Component.Discriminator
                };

                foreach (var schema in schemas)
                {
                    var schemaName = $"{schema.TypeName()}ComponentDto";

                    var componentSchema = args.SchemaResolver(schemaName, () =>
                    {
                        var nestedArgs = args.Increment();

                        var componentSchema = SchemaBuilder.Object();

                        foreach (var sharedField in schema.Fields.ForApi(nestedArgs.WithHiddenFields))
                        {
                            var sharedProperty = sharedField.Accept(this, nestedArgs);

                            if (sharedProperty != null)
                            {
                                sharedProperty.Description = sharedField.RawProperties.Hints;
                                sharedProperty.SetRequired(sharedField.RawProperties.IsRequired);

                                componentSchema.Properties.Add(sharedField.Name, sharedProperty);
                            }
                        }

                        componentSchema.Properties.Add(Component.Discriminator, SchemaBuilder.StringProperty(isRequired: true));

                        return(componentSchema);
                    });

                    jsonSchema.OneOf.Add(componentSchema);

                    discriminator.Mapping[schemaName] = componentSchema;
                }

                jsonSchema.DiscriminatorObject = discriminator;
            }
            else
            {
                jsonSchema.AllowAdditionalProperties = true;
            }
        }
예제 #5
0
        public JsonSchemaProperty?Visit(IField <ComponentsFieldProperties> field, Args args)
        {
            if (args.Level > MaxDepth)
            {
                return(null);
            }

            var itemSchema = SchemaBuilder.Object();

            BuildComponent(itemSchema, field, field.Properties.SchemaIds, args);

            return(SchemaBuilder.ArrayProperty(itemSchema));
        }
예제 #6
0
        public JsonSchemaProperty?Visit(IArrayField field)
        {
            var itemSchema = SchemaBuilder.Object();

            foreach (var nestedField in field.Fields.ForApi(withHiddenFields))
            {
                var nestedProperty = nestedField.Accept(this);

                if (nestedProperty != null)
                {
                    nestedProperty.Description = nestedField.RawProperties.Hints;
                    nestedProperty.SetRequired(nestedField.RawProperties.IsRequired);

                    itemSchema.Properties.Add(nestedField.Name, nestedProperty);
                }
            }

            return(SchemaBuilder.ArrayProperty(itemSchema));
        }
예제 #7
0
        public static JsonSchema BuildJsonSchema(this Schema schema, PartitionResolver partitionResolver, SchemaResolver schemaResolver, bool withHidden = false)
        {
            Guard.NotNull(schemaResolver, nameof(schemaResolver));
            Guard.NotNull(partitionResolver, nameof(partitionResolver));

            var schemaName = schema.Name.ToPascalCase();

            var jsonTypeVisitor = new JsonTypeVisitor(schemaResolver, withHidden);
            var jsonSchema      = SchemaBuilder.Object();

            foreach (var field in schema.Fields.ForApi(withHidden))
            {
                var partitionObject = SchemaBuilder.Object();
                var partitioning    = partitionResolver(field.Partitioning);

                foreach (var partitionKey in partitioning.AllKeys)
                {
                    var partitionItemProperty = field.Accept(jsonTypeVisitor);

                    if (partitionItemProperty != null)
                    {
                        var isOptional = partitioning.IsOptional(partitionKey);

                        var name = partitioning.GetName(partitionKey);

                        partitionItemProperty.Description = name;
                        partitionItemProperty.SetRequired(field.RawProperties.IsRequired && !isOptional);

                        partitionObject.Properties.Add(partitionKey, partitionItemProperty);
                    }
                }

                if (partitionObject.Properties.Count > 0)
                {
                    var propertyReference = schemaResolver($"{schemaName}{field.Name.ToPascalCase()}Property", partitionObject);

                    jsonSchema.Properties.Add(field.Name, CreateProperty(field, propertyReference));
                }
            }

            return(jsonSchema);
        }
예제 #8
0
        public static JsonSchema BuildFlatJsonSchema(this Schema schema, SchemaResolver schemaResolver)
        {
            Guard.NotNull(schemaResolver, nameof(schemaResolver));

            var schemaName = schema.TypeName();

            var jsonSchema = SchemaBuilder.Object();

            foreach (var field in schema.Fields.ForApi())
            {
                var property = JsonTypeVisitor.BuildProperty(field, null, false);

                if (property != null)
                {
                    var propertyReference = schemaResolver($"{schemaName}{field.Name.ToPascalCase()}FlatPropertyDto", () => property);

                    jsonSchema.Properties.Add(field.Name, CreateProperty(field, propertyReference));
                }
            }

            return(jsonSchema);
        }
예제 #9
0
        public static JsonSchema BuildDynamicJsonSchema(this Schema schema, SchemaResolver schemaResolver, bool withHidden = false)
        {
            Guard.NotNull(schemaResolver, nameof(schemaResolver));

            var jsonSchema = SchemaBuilder.Object();

            foreach (var field in schema.Fields.ForApi(withHidden))
            {
                var propertyItem = JsonTypeVisitor.BuildProperty(field, null, withHidden);

                if (propertyItem != null)
                {
                    var property =
                        SchemaBuilder.ObjectProperty(propertyItem)
                        .SetDescription(field)
                        .SetRequired(field.RawProperties.IsRequired);

                    jsonSchema.Properties.Add(field.Name, property);
                }
            }

            return(jsonSchema);
        }
예제 #10
0
        public static JsonSchema BuildJsonSchema(this Schema schema, PartitionResolver partitionResolver, bool withHidden = false)
        {
            Guard.NotNull(partitionResolver, nameof(partitionResolver));

            var jsonSchema = SchemaBuilder.Object();

            foreach (var field in schema.Fields.ForApi(withHidden))
            {
                var propertyObject = SchemaBuilder.Object();

                var partitioning = partitionResolver(field.Partitioning);

                foreach (var partitionKey in partitioning.AllKeys)
                {
                    var propertyItem = JsonTypeVisitor.BuildProperty(field, null, withHidden);

                    if (propertyItem != null)
                    {
                        var isOptional = partitioning.IsOptional(partitionKey);

                        var name = partitioning.GetName(partitionKey);

                        propertyItem.SetDescription(name);
                        propertyItem.SetRequired(field.RawProperties.IsRequired && !isOptional);

                        propertyObject.Properties.Add(partitionKey, propertyItem);
                    }
                }

                if (propertyObject.Properties.Count > 0)
                {
                    jsonSchema.Properties.Add(field.Name, CreateProperty(field, propertyObject));
                }
            }

            return(jsonSchema);
        }