From 0b125984da47fe7e0c970ac34b265daf62b1fe06 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 18 Feb 2016 16:15:15 +0800 Subject: [PATCH] add postProcessAllModels --- .../io/swagger/codegen/CodegenConfig.java | 2 ++ .../io/swagger/codegen/DefaultCodegen.java | 7 +++++ .../io/swagger/codegen/DefaultGenerator.java | 24 ++++++++++++++++ .../petstore/ruby/lib/petstore/api/pet_api.rb | 4 +-- .../ruby/lib/petstore/api/store_api.rb | 4 +-- .../ruby/lib/petstore/configuration.rb | 28 +++++++++++++++++++ 6 files changed, 65 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java index 9bb5b89089a..84f7fc50fb8 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java @@ -119,6 +119,8 @@ public interface CodegenConfig { void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map> operations); + Map postProcessAllModels(Map objs); + Map postProcessModels(Map objs); Map postProcessOperations(Map objs); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 6cb4c93e466..07ee0279c3e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -109,12 +109,19 @@ public class DefaultCodegen { } } + // override with any special post-processing for all models + @SuppressWarnings("static-method") + public Map postProcessAllModels(Map objs) { + return objs; + } + // override with any special post-processing @SuppressWarnings("static-method") public Map postProcessModels(Map objs) { return objs; } + // override with any special post-processing @SuppressWarnings("static-method") public Map postProcessOperations(Map objs) { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 96f0f2146b9..f817b244b3c 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -183,6 +183,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { sortedModelKeys = updatedKeys; } + // store all processed models + Map allProcessedModels = new HashMap(); + + // process models only for (String name : sortedModelKeys) { try { //don't generate models that have an import mapping @@ -195,6 +199,26 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { modelMap.put(name, model); Map models = processModels(config, modelMap, definitions); models.putAll(config.additionalProperties()); + + allProcessedModels.put(name, models); + + } catch (Exception e) { + throw new RuntimeException("Could not process model '" + name + "'", e); + } + } + + // post process all processed models + allProcessedModels = config.postProcessAllModels(allProcessedModels); + + // generate files based on processed models + for (String name: allProcessedModels.keySet()) { + Map models = (Map)allProcessedModels.get(name); + + try { + //don't generate models that have an import mapping + if(config.importMapping().containsKey(name)) { + continue; + } allModels.add(((List) models.get("models")).get(0)); diff --git a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb index f6d3a52a760..7efcd21c74d 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb @@ -298,7 +298,7 @@ module Petstore # http body (model) post_body = nil - auth_names = ['api_key'] + auth_names = ['api_key', 'petstore_auth'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, :query_params => query_params, @@ -550,7 +550,7 @@ module Petstore # http body (model) post_body = nil - auth_names = ['api_key'] + auth_names = ['api_key', 'petstore_auth'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, :query_params => query_params, diff --git a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb index 40c27f23f8e..5b669bca101 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb @@ -122,7 +122,7 @@ module Petstore # http body (model) post_body = @api_client.object_to_http_body(opts[:'body']) - auth_names = [] + auth_names = ['test_api_client_id', 'test_api_client_secret'] data, status_code, headers = @api_client.call_api(:POST, path, :header_params => header_params, :query_params => query_params, @@ -182,7 +182,7 @@ module Petstore # http body (model) post_body = nil - auth_names = [] + auth_names = ['test_api_key_header', 'test_api_key_query'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, :query_params => query_params, diff --git a/samples/client/petstore/ruby/lib/petstore/configuration.rb b/samples/client/petstore/ruby/lib/petstore/configuration.rb index 05973753287..e5f39898027 100644 --- a/samples/client/petstore/ruby/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby/lib/petstore/configuration.rb @@ -157,6 +157,13 @@ module Petstore # Returns Auth Settings hash for api client. def auth_settings { + 'test_api_key_header' => + { + type: 'api_key', + in: 'header', + key: 'test_api_key_header', + value: api_key_with_prefix('test_api_key_header') + }, 'api_key' => { type: 'api_key', @@ -164,6 +171,27 @@ module Petstore key: 'api_key', value: api_key_with_prefix('api_key') }, + 'test_api_client_secret' => + { + type: 'api_key', + in: 'header', + key: 'x-test_api_client_secret', + value: api_key_with_prefix('x-test_api_client_secret') + }, + 'test_api_client_id' => + { + type: 'api_key', + in: 'header', + key: 'x-test_api_client_id', + value: api_key_with_prefix('x-test_api_client_id') + }, + 'test_api_key_query' => + { + type: 'api_key', + in: 'query', + key: 'test_api_key_query', + value: api_key_with_prefix('test_api_key_query') + }, 'petstore_auth' => { type: 'oauth2',