From 682db2a230c6782cfd2236009cde0b67556d55d8 Mon Sep 17 00:00:00 2001 From: Yuri Sidorov <403994+newstler@users.noreply.github.com> Date: Mon, 10 Apr 2023 00:37:24 +0200 Subject: [PATCH] Fix: Send FactoryBot examples to JbuilderSchema for proper attributes and properties examples in schemas --- bullet_train-api/app/helpers/api/open_api_helper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bullet_train-api/app/helpers/api/open_api_helper.rb b/bullet_train-api/app/helpers/api/open_api_helper.rb index 80f50a1ef..3e0816535 100644 --- a/bullet_train-api/app/helpers/api/open_api_helper.rb +++ b/bullet_train-api/app/helpers/api/open_api_helper.rb @@ -74,7 +74,7 @@ def automatic_components_for(model, locals: {}) }.merge(locals)) schema_json = jbuilder.json( - model.new, + FactoryBot.example(model.name.underscore.to_sym) || model.new, title: I18n.t("#{model.name.underscore.pluralize}.label"), # TODO Improve this. We don't have a generic description for models we can use here. description: I18n.t("#{model.name.underscore.pluralize}.label"), @@ -106,7 +106,8 @@ def automatic_components_for(model, locals: {}) parameters_output = JSON.parse(schema_json) parameters_output["required"].select! { |key| strong_parameter_keys.include?(key.to_sym) } - parameters_output["properties"].select! { |key, value| strong_parameter_keys.include?(key.to_sym) } + parameters_output["properties"].select! { |key| strong_parameter_keys.include?(key.to_sym) } + parameters_output["example"]&.select! { |key, value| strong_parameter_keys.include?(key.to_sym) && value.present? } ( indent(attributes_output.to_yaml.gsub("---", "#{model.name.gsub("::", "")}Attributes:"), 3) +