From 9c329ea461e31214bee54c6667997b2f5f86473b Mon Sep 17 00:00:00 2001 From: "Mario.RuizSanchez" Date: Thu, 21 Mar 2019 15:25:12 +0000 Subject: [PATCH] added data example removing read_only data --- lib/open_api_import.rb | 26 +++++++++++++++++++++++--- open_api_import.gemspec | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/open_api_import.rb b/lib/open_api_import.rb index 4238093..66bdf9b 100644 --- a/lib/open_api_import.rb +++ b/lib/open_api_import.rb @@ -153,7 +153,6 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses method_name = cont[:operationId] end end - path_txt = path.path.dup.to_s if [:path, :path_file, :tags, :tags_file].include?(name_for_module) old_module_requests = module_requests @@ -457,6 +456,27 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses output << "}," end end + unless data_read_only.empty? or !data_required.empty? + reqdata = [] + #remove read only fields from :data + data_examples[0].each do |edata| + read_only = false + data_read_only.each do |rdata| + if edata.scan(/^#{rdata}:/).size>0 + read_only = true + break + elsif edata.scan(/:/).size==0 + break + end + end + reqdata << edata unless read_only + end + unless reqdata.empty? + output << "data: {" + output << reqdata.join(", \n") + output << "}," + end + end output << "data_examples: [" data_examples.each do |data| @@ -558,12 +578,12 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses class << self # Retrieve the examples from the properties hash - private def get_examples(properties, type=:key_value, remove_read_only=false) + private def get_examples(properties, type=:key_value, remove_readonly=false) #todo: consider using this method also to get data examples example = [] example << "{" unless properties.empty? or type==:only_value properties.each do |prop, val| - unless remove_read_only and val.key?(:readOnly) and val[:readOnly]==true + unless remove_readonly and val.key?(:readOnly) and val[:readOnly]==true if val.key?(:properties) and !val.key?(:example) and !val.key?(:type) val[:type]='object' end diff --git a/open_api_import.gemspec b/open_api_import.gemspec index 15c0263..51b47e1 100644 --- a/open_api_import.gemspec +++ b/open_api_import.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'open_api_import' - s.version = '0.7.1' + s.version = '0.7.2' s.summary = "OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses with all the examples. The file can be in JSON or YAML" s.description = "OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses with all the examples. The file can be in JSON or YAML" s.authors = ["Mario Ruiz"]