diff --git a/src/com/amazonaws/service/apigateway/importer/impl/SchemaTransformer.java b/src/com/amazonaws/service/apigateway/importer/impl/SchemaTransformer.java index f6a8962..b7815e8 100644 --- a/src/com/amazonaws/service/apigateway/importer/impl/SchemaTransformer.java +++ b/src/com/amazonaws/service/apigateway/importer/impl/SchemaTransformer.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; @@ -39,15 +40,15 @@ public class SchemaTransformer { /** * Get a schema schema in "flattened" form whereby all dependent references are resolved - * and included as inline schema definitions + * and included as external schema definitions * * @return the json-schema string in flattened form */ - public String flatten(String model, String models) { - return getFlattened(deserialize(model), deserialize(models)); + public String flatten(String apiId, String model, String models) { + return getFlattened(apiId, deserialize(model), deserialize(models)); } - private void buildSchemaReferenceMap(JsonNode model, JsonNode models, Map modelMap) { + private void buildSchemaReferenceMap(String apiId, JsonNode model, JsonNode models, Map modelMap) { Map refs = new HashMap<>(); findReferences(model, refs); @@ -58,10 +59,10 @@ private void buildSchemaReferenceMap(JsonNode model, JsonNode models, Map schemaMap = new HashMap<>(); - buildSchemaReferenceMap(model, models, schemaMap); - - replaceRefs(model, schemaMap); + buildSchemaReferenceMap(apiId, model, models, schemaMap); if (LOG.isTraceEnabled()) { try { @@ -108,26 +107,12 @@ private void validate(JsonNode rootNode) { } } - /* - * Add schema references as inline definitions to the root schema - */ - private void replaceRefs(JsonNode root, HashMap schemaMap) { - - ObjectNode definitionsNode = new ObjectNode(JsonNodeFactory.instance); - - for (Map.Entry entry : schemaMap.entrySet()) { - JsonNode schemaNode = deserialize(entry.getValue()); - definitionsNode.set(entry.getKey(), schemaNode); - } - - ((ObjectNode)root).set("definitions", definitionsNode); - } - /* * Replace a reference node with an inline reference */ - private void replaceRef(ObjectNode parent, String schemaName) { - parent.set("$ref", new TextNode("#/definitions/" + schemaName)); + private void replaceRef(String apiId, ObjectNode parent, String schemaName) { +// parent.set("$ref", new TextNode("#/definitions/" + schemaName)); + parent.set("$ref", new TextNode("https://apigateway.amazonaws.com/restapis/" + apiId + "/models/" + schemaName)); } /* @@ -163,7 +148,7 @@ JsonNode deserialize(String schemaText) { */ private String serializeExisting(JsonNode root) { try { - return new ObjectMapper().writeValueAsString(root); + return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root); } catch (JsonProcessingException e) { throw new IllegalStateException("Could not serialize generated schema json", e); } diff --git a/src/com/amazonaws/service/apigateway/importer/impl/sdk/ApiGatewaySdkSwaggerApiImporter.java b/src/com/amazonaws/service/apigateway/importer/impl/sdk/ApiGatewaySdkSwaggerApiImporter.java index 61c9139..b09da77 100644 --- a/src/com/amazonaws/service/apigateway/importer/impl/sdk/ApiGatewaySdkSwaggerApiImporter.java +++ b/src/com/amazonaws/service/apigateway/importer/impl/sdk/ApiGatewaySdkSwaggerApiImporter.java @@ -113,13 +113,13 @@ private void createModels(RestApi api, Map definitions, String modelContentType) { LOG.info(format("Creating model for api id %s with name %s", api.getId(), modelName)); - createModel(api, modelName, model.getDescription(), generateSchema(model, modelName, definitions), modelContentType); + createModel(api, modelName, model.getDescription(), generateSchema(api, model, modelName, definitions), modelContentType); } private void createModel(RestApi api, String modelName, Property model, String modelContentType) { LOG.info(format("Creating model for api id %s with name %s", api.getId(), modelName)); - createModel(api, modelName, model.getDescription(), generateSchema(model, modelName, swagger.getDefinitions()), modelContentType); + createModel(api, modelName, model.getDescription(), generateSchema(api, model, modelName, swagger.getDefinitions()), modelContentType); } private void updateMethods(RestApi api, String basePath, Map paths, List apiProduces) { @@ -320,17 +320,17 @@ private Boolean isApiKeyRequired(Operation op) { return false; } - private String generateSchema(Property model, String modelName, Map definitions) { - return generateSchemaString(model, modelName, definitions); + private String generateSchema(RestApi api, Property model, String modelName, Map definitions) { + return generateSchemaString(api, model, modelName, definitions); } - private String generateSchemaString(Object model, String modelName, Map definitions) { + private String generateSchemaString(RestApi api, Object model, String modelName, Map definitions) { try { String modelSchema = Json.mapper().writeValueAsString(model); String models = Json.mapper().writeValueAsString(definitions); // inline all references - String schema = new SchemaTransformer().flatten(modelSchema, models); + String schema = new SchemaTransformer().flatten(api.getId(), modelSchema, models); LOG.info("Generated json-schema for model " + modelName + ": " + schema); @@ -340,8 +340,8 @@ private String generateSchemaString(Object model, String modelName, Map definitions) { - return generateSchemaString(model, modelName, definitions); + private String generateSchema(RestApi api, com.wordnik.swagger.models.Model model, String modelName, Map definitions) { + return generateSchemaString(api, model, modelName, definitions); } private Optional getInputModel(BodyParameter p) { @@ -400,7 +400,7 @@ private void updateModels(RestApi api, Map