예제 #1
0
        public static JsonSchema BuildJsonSchemaFlat(this Schema schema, PartitionResolver partitionResolver,
                                                     ResolvedComponents components,
                                                     JsonTypeFactory?factory = null,
                                                     bool withHidden         = false,
                                                     bool withComponents     = false)
        {
            Guard.NotNull(partitionResolver);
            Guard.NotNull(components);

            factory ??= DefaultFactory;

            var jsonSchema = JsonTypeBuilder.Object();

            foreach (var field in schema.Fields.ForApi(withHidden))
            {
                var property =
                    JsonTypeVisitor.BuildProperty(
                        field, components, schema,
                        factory,
                        withHidden,
                        withComponents);

                // Property is null for UI fields.
                if (property != null)
                {
                    property.SetRequired(false);
                    property.SetDescription(field);

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

            return(jsonSchema);
        }
예제 #2
0
        public static JsonSchema BuildJsonSchema(this Schema schema, PartitionResolver partitionResolver,
                                                 ResolvedComponents components,
                                                 JsonTypeFactory?factory = null,
                                                 bool withHidden         = false,
                                                 bool withComponents     = false)
        {
            Guard.NotNull(partitionResolver);
            Guard.NotNull(components);

            factory ??= DefaultFactory;

            var jsonSchema = JsonTypeBuilder.Object();

            foreach (var field in schema.Fields.ForApi(withHidden))
            {
                var typeName = $"{schema.TypeName()}{field.Name.ToPascalCase()}PropertyDto";

                // Create a reference to give it a nice name in code generation.
                var(reference, actual) = factory(typeName);

                if (actual != null)
                {
                    var partitioning = partitionResolver(field.Partitioning);

                    foreach (var partitionKey in partitioning.AllKeys)
                    {
                        var property =
                            JsonTypeVisitor.BuildProperty(
                                field, components, schema,
                                factory,
                                withHidden,
                                withComponents);

                        // Property is null for UI fields.
                        if (property != null)
                        {
                            var isOptional = partitioning.IsOptional(partitionKey);

                            var name = partitioning.GetName(partitionKey);

                            // Required if property is required and language/partitioning is not optional.
                            property.SetRequired(field.RawProperties.IsRequired && !isOptional);
                            property.SetDescription(name);

                            actual.Properties.Add(partitionKey, property);
                        }
                    }
                }

                var propertyReference =
                    JsonTypeBuilder.ReferenceProperty(reference)
                    .SetDescription(field)
                    .SetRequired(field.RawProperties.IsRequired);

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

            return(jsonSchema);
        }