17
17
import com .fasterxml .jackson .core .JsonProcessingException ;
18
18
import com .fasterxml .jackson .databind .JsonNode ;
19
19
import com .fasterxml .jackson .databind .ObjectMapper ;
20
+ import com .fasterxml .jackson .databind .SerializationFeature ;
20
21
import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
21
22
import com .fasterxml .jackson .databind .node .ObjectNode ;
22
23
import com .fasterxml .jackson .databind .node .TextNode ;
@@ -39,15 +40,15 @@ public class SchemaTransformer {
39
40
40
41
/**
41
42
* Get a schema schema in "flattened" form whereby all dependent references are resolved
42
- * and included as inline schema definitions
43
+ * and included as external schema definitions
43
44
*
44
45
* @return the json-schema string in flattened form
45
46
*/
46
- public String flatten (String model , String models ) {
47
- return getFlattened (deserialize (model ), deserialize (models ));
47
+ public String flatten (String apiId , String model , String models ) {
48
+ return getFlattened (apiId , deserialize (model ), deserialize (models ));
48
49
}
49
50
50
- private void buildSchemaReferenceMap (JsonNode model , JsonNode models , Map <String , String > modelMap ) {
51
+ private void buildSchemaReferenceMap (String apiId , JsonNode model , JsonNode models , Map <String , String > modelMap ) {
51
52
Map <JsonNode , JsonNode > refs = new HashMap <>();
52
53
findReferences (model , refs );
53
54
@@ -58,10 +59,10 @@ private void buildSchemaReferenceMap(JsonNode model, JsonNode models, Map<String
58
59
59
60
JsonNode subSchema = getSchema (schemaName , models );
60
61
61
- // replace reference values with inline definitions
62
- replaceRef ((ObjectNode ) refs .get (ref ), schemaName );
62
+ // replace reference values with external definitions
63
+ replaceRef (apiId , (ObjectNode ) refs .get (ref ), schemaName );
63
64
64
- buildSchemaReferenceMap (subSchema , models , modelMap );
65
+ buildSchemaReferenceMap (apiId , subSchema , models , modelMap );
65
66
66
67
modelMap .put (schemaName , serializeExisting (subSchema ));
67
68
}
@@ -71,12 +72,10 @@ private JsonNode getSchema(String schemaName, JsonNode models) {
71
72
return models .findPath (schemaName );
72
73
}
73
74
74
- private String getFlattened (JsonNode model , JsonNode models ) {
75
+ private String getFlattened (String apiId , JsonNode model , JsonNode models ) {
75
76
HashMap <String , String > schemaMap = new HashMap <>();
76
77
77
- buildSchemaReferenceMap (model , models , schemaMap );
78
-
79
- replaceRefs (model , schemaMap );
78
+ buildSchemaReferenceMap (apiId , model , models , schemaMap );
80
79
81
80
if (LOG .isTraceEnabled ()) {
82
81
try {
@@ -108,26 +107,12 @@ private void validate(JsonNode rootNode) {
108
107
}
109
108
}
110
109
111
- /*
112
- * Add schema references as inline definitions to the root schema
113
- */
114
- private void replaceRefs (JsonNode root , HashMap <String , String > schemaMap ) {
115
-
116
- ObjectNode definitionsNode = new ObjectNode (JsonNodeFactory .instance );
117
-
118
- for (Map .Entry <String , String > entry : schemaMap .entrySet ()) {
119
- JsonNode schemaNode = deserialize (entry .getValue ());
120
- definitionsNode .set (entry .getKey (), schemaNode );
121
- }
122
-
123
- ((ObjectNode )root ).set ("definitions" , definitionsNode );
124
- }
125
-
126
110
/*
127
111
* Replace a reference node with an inline reference
128
112
*/
129
- private void replaceRef (ObjectNode parent , String schemaName ) {
130
- parent .set ("$ref" , new TextNode ("#/definitions/" + schemaName ));
113
+ private void replaceRef (String apiId , ObjectNode parent , String schemaName ) {
114
+ // parent.set("$ref", new TextNode("#/definitions/" + schemaName));
115
+ parent .set ("$ref" , new TextNode ("https://apigateway.amazonaws.com/restapis/" + apiId + "/models/" + schemaName ));
131
116
}
132
117
133
118
/*
@@ -163,7 +148,7 @@ JsonNode deserialize(String schemaText) {
163
148
*/
164
149
private String serializeExisting (JsonNode root ) {
165
150
try {
166
- return new ObjectMapper ().writeValueAsString (root );
151
+ return new ObjectMapper ().writerWithDefaultPrettyPrinter (). writeValueAsString (root );
167
152
} catch (JsonProcessingException e ) {
168
153
throw new IllegalStateException ("Could not serialize generated schema json" , e );
169
154
}
0 commit comments