@@ -119,79 +119,98 @@ public OpenAPI modifyWithBallerinaConventions(OpenAPI openapi, Map<String, Strin
119
119
return openapi ;
120
120
}
121
121
122
- private static void modifyOASWithObjectPropertyNameInlineSchema (OpenAPI openAPI ) {
123
- for (Map .Entry <String , PathItem > pathEntry : openAPI .getPaths ().entrySet ()) {
124
- PathItem pathItem = pathEntry .getValue ();
125
- for (PathItem .HttpMethod method : pathItem .readOperationsMap ().keySet ()) {
126
- Operation operation = pathItem .readOperationsMap ().get (method );
127
- if (operation .getRequestBody () != null ) {
128
- checkForInlineObjects (operation .getRequestBody ().getContent ());
129
- }
130
- if (operation .getParameters () != null ) {
131
- for (Parameter parameter : operation .getParameters ()) {
132
- if (parameter .getSchema () != null ) {
133
- updateInlineSchema (parameter .getSchema ());
134
- }
135
- }
136
- }
137
- for (Map .Entry <String , ApiResponse > responseEntry : operation .getResponses ().entrySet ()) {
138
- ApiResponse response = responseEntry .getValue ();
139
- if (response .getContent () != null ) {
140
- checkForInlineObjects (response .getContent ());
141
- }
142
- }
122
+ private void modifyOASWithObjectPropertyNameInlineSchema (OpenAPI openAPI ) {
123
+ openAPI .getPaths ().forEach ((path , pathItem ) -> processPathItem (pathItem ));
124
+ }
125
+
126
+ private void processPathItem (PathItem pathItem ) {
127
+ pathItem .readOperationsMap ().forEach ((method , operation ) -> processOperationWithInlineSchema (operation ));
128
+ }
129
+
130
+ private void processOperationWithInlineSchema (Operation operation ) {
131
+ if (operation .getRequestBody () != null ) {
132
+ updateInlineObjectInContent (operation .getRequestBody ().getContent ());
133
+ }
134
+ processParametersWithInlineSchema (operation .getParameters ());
135
+ processResponsesWithInlineSchema (operation .getResponses ());
136
+ }
137
+
138
+ private void processParametersWithInlineSchema (List <Parameter > parameters ) {
139
+ if (parameters == null ) return ;
140
+
141
+ for (Parameter parameter : parameters ) {
142
+ if (parameter .getSchema () != null ) {
143
+ updateInlineSchema (parameter .getSchema ());
143
144
}
144
145
}
145
146
}
146
147
148
+ private void processResponsesWithInlineSchema (Map <String , ApiResponse > responses ) {
149
+ responses .forEach ((status , response ) -> {
150
+ if (response .getContent () != null ) {
151
+ updateInlineObjectInContent (response .getContent ());
152
+ }
153
+ });
154
+ }
147
155
148
- private static void updateInlineSchema (Schema schema ) {
156
+ private void updateInlineSchema (Schema <?> schema ) {
149
157
if (schema == null ) {
150
158
return ;
151
159
}
152
- if (schema instanceof ComposedSchema ) {
153
- ComposedSchema composedSchema = (ComposedSchema ) schema ;
154
- if (composedSchema .getAllOf () != null ) {
155
- for (Schema subSchema : composedSchema .getAllOf ()) {
156
- updateInlineSchema (subSchema );
157
- }
158
- }
159
- if (composedSchema .getAnyOf () != null ) {
160
- for (Schema subSchema : composedSchema .getAnyOf ()) {
161
- updateInlineSchema (subSchema );
162
- }
163
- }
164
- if (composedSchema .getOneOf () != null ) {
165
- for (Schema subSchema : composedSchema .getOneOf ()) {
166
- updateInlineSchema (subSchema );
167
- }
168
- }
169
- } else if (schema .getType () == null && schema .get$ref () == null && schema .getProperties () != null ) {
160
+ handleInlineSchemaInComposedSchema (schema );
161
+ handleInlineObjectSchema (schema );
162
+ handleInlineSchemaItems (schema );
163
+ handleInlineSchemaInAdditionalProperties (schema );
164
+ handleInlineSchemaProperties (schema );
165
+ }
166
+
167
+ private void handleInlineSchemaInComposedSchema (Schema <?> schema ) {
168
+ if (schema instanceof ComposedSchema composedSchema ) {
169
+ processSubSchemas (composedSchema .getAllOf ());
170
+ processSubSchemas (composedSchema .getAnyOf ());
171
+ processSubSchemas (composedSchema .getOneOf ());
172
+ }
173
+ }
174
+
175
+ private void processSubSchemas (List <Schema > subSchemas ) {
176
+ if (subSchemas != null ) {
177
+ subSchemas .forEach (this ::updateInlineSchema );
178
+ }
179
+ }
180
+
181
+ private static void handleInlineObjectSchema (Schema <?> schema ) {
182
+ if (isInlineObjectSchema (schema )) {
170
183
Map <String , Schema > properties = schema .getProperties ();
171
- if (Objects .nonNull (properties ) && !properties .isEmpty ()) {
172
- schema .setProperties (getPropertiesWithBallerinaNameExtension (properties ));
173
- }
174
- } else if (schema instanceof ObjectSchema objectSchema && schema .getProperties () != null ) {
175
- Map <String , Schema > properties = objectSchema .getProperties ();
176
- if (Objects .nonNull (properties ) && !properties .isEmpty ()) {
184
+ if (properties != null && !properties .isEmpty ()) {
177
185
schema .setProperties (getPropertiesWithBallerinaNameExtension (properties ));
178
186
}
179
187
}
188
+ }
189
+
190
+ private static boolean isInlineObjectSchema (Schema <?> schema ) {
191
+ return schema instanceof ObjectSchema ||
192
+ (schema .getType () == null && schema .get$ref () == null && schema .getProperties () != null );
193
+ }
194
+
195
+ private void handleInlineSchemaItems (Schema <?> schema ) {
180
196
if (schema .getItems () != null ) {
181
197
updateInlineSchema (schema .getItems ());
182
198
}
199
+ }
200
+
201
+ private void handleInlineSchemaInAdditionalProperties (Schema <?> schema ) {
183
202
if (schema .getAdditionalProperties () instanceof Schema ) {
184
203
updateInlineSchema ((Schema ) schema .getAdditionalProperties ());
185
204
}
205
+ }
206
+
207
+ private void handleInlineSchemaProperties (Schema <?> schema ) {
186
208
if (schema .getProperties () != null ) {
187
- Map <String , Schema > properties = schema .getProperties ();
188
- for (Map .Entry <String , Schema > property : properties .entrySet ()) {
189
- updateInlineSchema (property .getValue ());
190
- }
209
+ schema .getProperties ().values ().forEach (this ::updateInlineSchema );
191
210
}
192
211
}
193
212
194
- private static void checkForInlineObjects (Map <String , io .swagger .v3 .oas .models .media .MediaType > content ) {
213
+ private void updateInlineObjectInContent (Map <String , io .swagger .v3 .oas .models .media .MediaType > content ) {
195
214
for (Map .Entry <String , io .swagger .v3 .oas .models .media .MediaType > entry : content .entrySet ()) {
196
215
Schema schema = entry .getValue ().getSchema ();
197
216
updateInlineSchema (schema );
0 commit comments