lib/json_schema/faker/strategy/simple.rb
def compact_schema(schema, position:)
      return schema if schema.one_of.empty? && schema.any_of.empty? && schema.all_of.empty?

      if ::JsonSchema::Faker::Configuration.logger
        ::JsonSchema::Faker::Configuration.logger.info "start to compact at #{position}"
        ::JsonSchema::Faker::Configuration.logger.debug schema.inspect_schema
      end

      merged_schema = ::JsonSchema::Schema.new
      merged_schema.copy_from(schema)
      merged_schema.one_of = []
      merged_schema.any_of = []
      merged_schema.all_of = []

      unless schema.all_of.empty?
        ::JsonSchema::Faker::Configuration.logger.info "compact all_of" if ::JsonSchema::Faker::Configuration.logger

        all_of = schema.all_of.each.with_index.inject(nil) do |(a, _), (b, i)|
          if a
            take_logical_and_of_schema(a, b, a_position: "#{position}/all_of", b_position: "#{position}/all_of[#{i+1}]")
          else
            b
          end
        end

        unless schema.one_of.empty? && schema.any_of.empty?
          ::JsonSchema::Faker::Configuration.logger.info "find from one_of and any_of which satiffy all_of" if ::JsonSchema::Faker::Configuration.logger
          one_of_candidate = schema.one_of.find do |s|
            s2 = take_logical_and_of_schema(s, all_of)
            compare_schema(s, s2) # FIXME: check s2 > s
          end

          any_of_candidate = schema.any_of.find do |s|
            s2 = take_logical_and_of_schema(s, all_of)
            compare_schema(s, s2) # FIXME: check s2 > s
          end

          unless one_of_candidate || any_of_candidate
            ::JsonSchema::Faker::Configuration.logger.error "failed to find condition which satfisfy all_of in one_of and any_of" if ::JsonSchema::Faker::Configuration.logger
            merged_schema = take_logical_and_of_schema(merged_schema, all_of, a_position: position, b_position: "#{position}/all_of")
          else
            merged_schema = take_logical_and_of_schema(merged_schema, one_of_candidate, a_position: position, b_position: "#{position}/one_of") if one_of_candidate
            merged_schema = take_logical_and_of_schema(merged_schema, any_of_candidate, a_position: position, b_position: "#{position}/any_of") if any_of_candidate
          end
        else
          merged_schema = take_logical_and_of_schema(merged_schema, all_of, a_position: position, b_position: "#{position}/all_of")
        end
      else
        unless schema.one_of.empty?
          ::JsonSchema::Faker::Configuration.logger.info "compact one_of" if ::JsonSchema::Faker::Configuration.logger
          merged_schema = take_logical_and_of_schema(merged_schema, schema.one_of.first, a_position: position, b_position: "#{position}/one_of[0]")
        end

        unless schema.any_of.empty?
          ::JsonSchema::Faker::Configuration.logger.info "compact any_of" if ::JsonSchema::Faker::Configuration.logger
          merged_schema = take_logical_and_of_schema(merged_schema, schema.any_of.first, a_position: position, b_position: "#{position}/any_of[0]")
        end
      end

      # compact recursively
      merged_schema = compact_schema(merged_schema, position: position)

      ::JsonSchema::Faker::Configuration.logger.debug "compacted: #{merged_schema.inspect_schema}" if ::JsonSchema::Faker::Configuration.logger

      merged_schema
    end
def compact_schema(schema, position:)
      return schema if schema.one_of.empty? && schema.any_of.empty? && schema.all_of.empty?

      if ::JsonSchema::Faker::Configuration.logger
        ::JsonSchema::Faker::Configuration.logger.info "start to compact at #{position}"
        ::JsonSchema::Faker::Configuration.logger.debug schema.inspect_schema
      end

      merged_schema = ::JsonSchema::Schema.new
      merged_schema.copy_from(schema)
      merged_schema.one_of = []
      merged_schema.any_of = []
      merged_schema.all_of = []

      unless schema.all_of.empty?
        ::JsonSchema::Faker::Configuration.logger.info "compact all_of" if ::JsonSchema::Faker::Configuration.logger

        all_of = schema.all_of.each.with_index.inject(nil) do |(a, _), (b, i)|
          if a
            take_logical_and_of_schema(a, b, a_position: "#{position}/all_of", b_position: "#{position}/all_of[#{i+1}]")
          else
            b
          end
        end

        unless schema.one_of.empty? && schema.any_of.empty?
          ::JsonSchema::Faker::Configuration.logger.info "find from one_of and any_of which satiffy all_of" if ::JsonSchema::Faker::Configuration.logger
          one_of_candidate = schema.one_of.find do |s|
            s2 = take_logical_and_of_schema(s, all_of)
            compare_schema(s, s2) # FIXME: check s2 > s
          end

          any_of_candidate = schema.any_of.find do |s|
            s2 = take_logical_and_of_schema(s, all_of)
            compare_schema(s, s2) # FIXME: check s2 > s
          end

          unless one_of_candidate || any_of_candidate
            ::JsonSchema::Faker::Configuration.logger.error "failed to find condition which satfisfy all_of in one_of and any_of" if ::JsonSchema::Faker::Configuration.logger
            merged_schema = take_logical_and_of_schema(merged_schema, all_of, a_position: position, b_position: "#{position}/all_of")
          else
            merged_schema = take_logical_and_of_schema(merged_schema, one_of_candidate, a_position: position, b_position: "#{position}/one_of") if one_of_candidate
            merged_schema = take_logical_and_of_schema(merged_schema, any_of_candidate, a_position: position, b_position: "#{position}/any_of") if any_of_candidate
          end
        else
          merged_schema = take_logical_and_of_schema(merged_schema, all_of, a_position: position, b_position: "#{position}/all_of")
        end
      else
        unless schema.one_of.empty?
          ::JsonSchema::Faker::Configuration.logger.info "compact one_of" if ::JsonSchema::Faker::Configuration.logger
          merged_schema = take_logical_and_of_schema(merged_schema, schema.one_of.first, a_position: position, b_position: "#{position}/one_of[0]")
        end

        unless schema.any_of.empty?
          ::JsonSchema::Faker::Configuration.logger.info "compact any_of" if ::JsonSchema::Faker::Configuration.logger
          merged_schema = take_logical_and_of_schema(merged_schema, schema.any_of.first, a_position: position, b_position: "#{position}/any_of[0]")
        end
      end

      # compact recursively
      merged_schema = compact_schema(merged_schema, position: position)

      ::JsonSchema::Faker::Configuration.logger.debug "compacted: #{merged_schema.inspect_schema}" if ::JsonSchema::Faker::Configuration.logger

      merged_schema
    end
def compact_schema ( schema , position : ) return schema if schema . one_of . empty? && schema . any_of . empty? && schema . all_of . empty? if :: JsonSchema :: Faker :: Configuration . logger :: JsonSchema :: Faker :: Configuration . logger . info "start to compact at #{position}" :: JsonSchema :: Faker :: Configuration . logger . debug schema . inspect_schema end merged_schema = :: JsonSchema :: Schema . new merged_schema . copy_from ( schema ) merged_schema . one_of = [ ] merged_schema . any_of = [ ] merged_schema . all_of = [ ] unless schema . all_of . empty? :: JsonSchema :: Faker :: Configuration . logger . info "compact all_of" if :: JsonSchema :: Faker :: Configuration . logger all_of = schema . all_of . each . with_index . inject ( nil ) do | ( a , _ ) , ( b , i ) | if a take_logical_and_of_schema ( a , b , a_position : "#{position}/all_of" , b_position : "#{position}/all_of[#{i+1}]" ) else b end end unless schema . one_of . empty? && schema . any_of . empty? :: JsonSchema :: Faker :: Configuration . logger . info "find from one_of and any_of which satiffy all_of" if :: JsonSchema :: Faker :: Configuration . logger one_of_candidate = schema . one_of . find do | s | s2 = take_logical_and_of_schema ( s , all_of ) compare_schema ( s , s2 ) # FIXME: check s2 > s end any_of_candidate = schema . any_of . find do | s | s2 = take_logical_and_of_schema ( s , all_of ) compare_schema ( s , s2 ) # FIXME: check s2 > s end unless one_of_candidate || any_of_candidate :: JsonSchema :: Faker :: Configuration . logger . error "failed to find condition which satfisfy all_of in one_of and any_of" if :: JsonSchema :: Faker :: Configuration . logger merged_schema = take_logical_and_of_schema ( merged_schema , all_of , a_position : position , b_position : "#{position}/all_of" ) else merged_schema = take_logical_and_of_schema ( merged_schema , one_of_candidate , a_position : position , b_position : "#{position}/one_of" ) if one_of_candidate merged_schema = take_logical_and_of_schema ( merged_schema , any_of_candidate , a_position : position , b_position : "#{position}/any_of" ) if any_of_candidate end else merged_schema = take_logical_and_of_schema ( merged_schema , all_of , a_position : position , b_position : "#{position}/all_of" ) end else unless schema . one_of . empty? :: JsonSchema :: Faker :: Configuration . logger . info "compact one_of" if :: JsonSchema :: Faker :: Configuration . logger merged_schema = take_logical_and_of_schema ( merged_schema , schema . one_of . first , a_position : position , b_position : "#{position}/one_of[0]" ) end unless schema . any_of . empty? :: JsonSchema :: Faker :: Configuration . logger . info "compact any_of" if :: JsonSchema :: Faker :: Configuration . logger merged_schema = take_logical_and_of_schema ( merged_schema , schema . any_of . first , a_position : position , b_position : "#{position}/any_of[0]" ) end end # compact recursively merged_schema = compact_schema ( merged_schema , position : position ) :: JsonSchema :: Faker :: Configuration . logger . debug "compacted: #{merged_schema.inspect_schema}" if :: JsonSchema :: Faker :: Configuration . logger merged_schema end
TODO: compacting all_of and one_of/any_of at once will not work
TODO : compacting all_of and one_of / any_of at once will not work