|
1 | 1 | package io.swagger.codegen.v3.generators.ruby;
|
2 | 2 |
|
3 |
| -import io.swagger.codegen.v3.*; |
| 3 | +import io.swagger.codegen.v3.CliOption; |
| 4 | +import io.swagger.codegen.v3.CodegenConstants; |
| 5 | +import io.swagger.codegen.v3.CodegenOperation; |
| 6 | +import io.swagger.codegen.v3.CodegenParameter; |
| 7 | +import io.swagger.codegen.v3.CodegenProperty; |
| 8 | +import io.swagger.codegen.v3.CodegenType; |
| 9 | +import io.swagger.codegen.v3.SupportingFile; |
4 | 10 | import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
|
| 11 | +import io.swagger.v3.oas.models.OpenAPI; |
| 12 | +import io.swagger.v3.oas.models.Operation; |
| 13 | +import io.swagger.v3.oas.models.media.ArraySchema; |
| 14 | +import io.swagger.v3.oas.models.media.BooleanSchema; |
| 15 | +import io.swagger.v3.oas.models.media.IntegerSchema; |
| 16 | +import io.swagger.v3.oas.models.media.MapSchema; |
| 17 | +import io.swagger.v3.oas.models.media.NumberSchema; |
| 18 | +import io.swagger.v3.oas.models.media.Schema; |
| 19 | +import io.swagger.v3.oas.models.media.StringSchema; |
5 | 20 | import org.apache.commons.lang3.StringUtils;
|
6 | 21 | import org.slf4j.Logger;
|
7 | 22 | import org.slf4j.LoggerFactory;
|
@@ -232,38 +247,38 @@ public void processOpts() {
|
232 | 247 | writeOptional(outputFolder, new SupportingFile("configuration_spec.mustache", specFolder, "configuration_spec.rb"));
|
233 | 248 | writeOptional(outputFolder, new SupportingFile("api_client_spec.mustache", specFolder, "api_client_spec.rb"));
|
234 | 249 | // not including base object test as the moment as not all API has model
|
235 |
| - //writeOptional(outputFolder, new SupportingFile("base_object_spec.mustache", specFolder, "base_object_spec.rb")); |
236 |
| - } |
237 |
| - |
238 |
| -// @Override |
239 |
| -// public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) { |
240 |
| -// CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger); |
241 |
| -// // Set vendor-extension to be used in template: |
242 |
| -// // x-codegen-hasMoreRequired |
243 |
| -// // x-codegen-hasMoreOptional |
244 |
| -// // x-codegen-hasRequiredParams |
245 |
| -// CodegenParameter lastRequired = null; |
246 |
| -// CodegenParameter lastOptional = null; |
247 |
| -// for (CodegenParameter p : op.allParams) { |
248 |
| -// if (p.required) { |
249 |
| -// lastRequired = p; |
250 |
| -// } else { |
251 |
| -// lastOptional = p; |
252 |
| -// } |
253 |
| -// } |
254 |
| -// for (CodegenParameter p : op.allParams) { |
255 |
| -// if (p == lastRequired) { |
256 |
| -// p.vendorExtensions.put("x-codegen-hasMoreRequired", false); |
257 |
| -// } else if (p == lastOptional) { |
258 |
| -// p.vendorExtensions.put("x-codegen-hasMoreOptional", false); |
259 |
| -// } else { |
260 |
| -// p.vendorExtensions.put("x-codegen-hasMoreRequired", true); |
261 |
| -// p.vendorExtensions.put("x-codegen-hasMoreOptional", true); |
262 |
| -// } |
263 |
| -// } |
264 |
| -// op.vendorExtensions.put("x-codegen-hasRequiredParams", lastRequired != null); |
265 |
| -// return op; |
266 |
| -// } |
| 250 | + writeOptional(outputFolder, new SupportingFile("base_object_spec.mustache", specFolder, "base_object_spec.rb")); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> definitions, OpenAPI openAPI) { |
| 255 | + CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, openAPI); |
| 256 | + // Set vendor-extension to be used in template: |
| 257 | + // x-codegen-hasMoreRequired |
| 258 | + // x-codegen-hasMoreOptional |
| 259 | + // x-codegen-hasRequiredParams |
| 260 | + CodegenParameter lastRequired = null; |
| 261 | + CodegenParameter lastOptional = null; |
| 262 | + for (CodegenParameter p : op.allParams) { |
| 263 | + if (p.required) { |
| 264 | + lastRequired = p; |
| 265 | + } else { |
| 266 | + lastOptional = p; |
| 267 | + } |
| 268 | + } |
| 269 | + for (CodegenParameter p : op.allParams) { |
| 270 | + if (p == lastRequired) { |
| 271 | + p.vendorExtensions.put("x-codegen-hasMoreRequired", false); |
| 272 | + } else if (p == lastOptional) { |
| 273 | + p.vendorExtensions.put("x-codegen-hasMoreOptional", false); |
| 274 | + } else { |
| 275 | + p.vendorExtensions.put("x-codegen-hasMoreRequired", true); |
| 276 | + p.vendorExtensions.put("x-codegen-hasMoreOptional", true); |
| 277 | + } |
| 278 | + } |
| 279 | + op.vendorExtensions.put("x-codegen-hasRequiredParams", lastRequired != null); |
| 280 | + return op; |
| 281 | + } |
267 | 282 |
|
268 | 283 | @Override
|
269 | 284 | public CodegenType getTag() {
|
@@ -338,74 +353,66 @@ public String modelDocFileFolder() {
|
338 | 353 | return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
|
339 | 354 | }
|
340 | 355 |
|
341 |
| -// @Override |
342 |
| -// public String getTypeDeclaration(Property p) { |
343 |
| -// if (p instanceof ArrayProperty) { |
344 |
| -// ArrayProperty ap = (ArrayProperty) p; |
345 |
| -// Property inner = ap.getItems(); |
346 |
| -// return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">"; |
347 |
| -// } else if (p instanceof MapProperty) { |
348 |
| -// MapProperty mp = (MapProperty) p; |
349 |
| -// Property inner = mp.getAdditionalProperties(); |
350 |
| -// return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">"; |
351 |
| -// } |
352 |
| -// return super.getTypeDeclaration(p); |
353 |
| -// } |
354 |
| - |
355 |
| -// @Override |
356 |
| -// public String toDefaultValue(Property p) { |
357 |
| -// if (p instanceof IntegerProperty) { |
358 |
| -// IntegerProperty dp = (IntegerProperty) p; |
359 |
| -// if (dp.getDefault() != null) { |
360 |
| -// return dp.getDefault().toString(); |
361 |
| -// } |
362 |
| -// } else if (p instanceof LongProperty) { |
363 |
| -// LongProperty dp = (LongProperty) p; |
364 |
| -// if (dp.getDefault() != null) { |
365 |
| -// return dp.getDefault().toString(); |
366 |
| -// } |
367 |
| -// } else if (p instanceof DoubleProperty) { |
368 |
| -// DoubleProperty dp = (DoubleProperty) p; |
369 |
| -// if (dp.getDefault() != null) { |
370 |
| -// return dp.getDefault().toString(); |
371 |
| -// } |
372 |
| -// } else if (p instanceof FloatProperty) { |
373 |
| -// FloatProperty dp = (FloatProperty) p; |
374 |
| -// if (dp.getDefault() != null) { |
375 |
| -// return dp.getDefault().toString(); |
376 |
| -// } |
377 |
| -// } else if (p instanceof BooleanProperty) { |
378 |
| -// BooleanProperty bp = (BooleanProperty) p; |
379 |
| -// if (bp.getDefault() != null) { |
380 |
| -// return bp.getDefault().toString(); |
381 |
| -// } |
382 |
| -// } else if (p instanceof StringProperty) { |
383 |
| -// StringProperty sp = (StringProperty) p; |
384 |
| -// if (sp.getDefault() != null) { |
385 |
| -// return "'" + escapeText(sp.getDefault()) + "'"; |
386 |
| -// } |
387 |
| -// } |
388 |
| -// |
389 |
| -// return null; |
390 |
| -// } |
391 |
| - |
392 |
| -// @Override |
393 |
| -// public String getSwaggerType(Property p) { |
394 |
| -// String swaggerType = super.getSwaggerType(p); |
395 |
| -// String type = null; |
396 |
| -// if (typeMapping.containsKey(swaggerType)) { |
397 |
| -// type = typeMapping.get(swaggerType); |
398 |
| -// if (languageSpecificPrimitives.contains(type)) { |
399 |
| -// return type; |
400 |
| -// } |
401 |
| -// } else { |
402 |
| -// type = swaggerType; |
403 |
| -// } |
404 |
| -// if (type == null) { |
405 |
| -// return null; |
406 |
| -// } |
407 |
| -// return toModelName(type); |
408 |
| -// } |
| 356 | + @Override |
| 357 | + public String getTypeDeclaration(Schema schema) { |
| 358 | + if (schema instanceof ArraySchema) { |
| 359 | + ArraySchema arraySchema = (ArraySchema) schema; |
| 360 | + Schema inner = arraySchema.getItems(); |
| 361 | + return getSchemaType(schema) + "<" + getTypeDeclaration(inner) + ">"; |
| 362 | + } else if (schema instanceof MapSchema) { |
| 363 | + MapSchema mapSchema = (MapSchema) schema; |
| 364 | + if (mapSchema.getAdditionalProperties() instanceof Schema) { |
| 365 | + Schema inner = (Schema) mapSchema.getAdditionalProperties(); |
| 366 | + return getSchemaType(schema) + "<String, " + getTypeDeclaration(inner) + ">"; |
| 367 | + } |
| 368 | + } |
| 369 | + return super.getTypeDeclaration(schema); |
| 370 | + } |
| 371 | + |
| 372 | + @Override |
| 373 | + public String toDefaultValue(Schema schema) { |
| 374 | + if (schema instanceof IntegerSchema) { |
| 375 | + IntegerSchema integerSchema = (IntegerSchema) schema; |
| 376 | + if (integerSchema.getDefault() != null) { |
| 377 | + return integerSchema.getDefault().toString(); |
| 378 | + } |
| 379 | + } else if (schema instanceof NumberSchema) { |
| 380 | + NumberSchema numberSchema = (NumberSchema) schema; |
| 381 | + if (numberSchema.getDefault() != null) { |
| 382 | + return numberSchema.getDefault().toString(); |
| 383 | + } |
| 384 | + } else if (schema instanceof BooleanSchema) { |
| 385 | + BooleanSchema booleanSchema = (BooleanSchema) schema; |
| 386 | + if (booleanSchema.getDefault() != null) { |
| 387 | + return booleanSchema.getDefault().toString(); |
| 388 | + } |
| 389 | + } else if (schema instanceof StringSchema) { |
| 390 | + StringSchema stringSchema = (StringSchema) schema; |
| 391 | + if (stringSchema.getDefault() != null) { |
| 392 | + return "'" + escapeText(stringSchema.getDefault()) + "'"; |
| 393 | + } |
| 394 | + } |
| 395 | + |
| 396 | + return null; |
| 397 | + } |
| 398 | + |
| 399 | + @Override |
| 400 | + public String getSchemaType(Schema schema) { |
| 401 | + String swaggerType = super.getSchemaType(schema); |
| 402 | + String type = null; |
| 403 | + if (typeMapping.containsKey(swaggerType)) { |
| 404 | + type = typeMapping.get(swaggerType); |
| 405 | + if (languageSpecificPrimitives.contains(type)) { |
| 406 | + return type; |
| 407 | + } |
| 408 | + } else { |
| 409 | + type = swaggerType; |
| 410 | + } |
| 411 | + if (type == null) { |
| 412 | + return null; |
| 413 | + } |
| 414 | + return toModelName(type); |
| 415 | + } |
409 | 416 |
|
410 | 417 | @Override
|
411 | 418 | public String toVarName(String name) {
|
|
0 commit comments