|
1 | 1 | package io.swagger.codegen.v3.generators.go;
|
2 | 2 |
|
3 | 3 | import io.swagger.codegen.v3.CliOption;
|
4 |
| -import io.swagger.codegen.v3.CodegenConfig; |
5 | 4 | import io.swagger.codegen.v3.CodegenConstants;
|
| 5 | +import io.swagger.codegen.v3.CodegenContent; |
6 | 6 | import io.swagger.codegen.v3.CodegenModel;
|
7 | 7 | import io.swagger.codegen.v3.CodegenOperation;
|
8 | 8 | import io.swagger.codegen.v3.CodegenParameter;
|
9 | 9 | import io.swagger.codegen.v3.CodegenProperty;
|
| 10 | +import io.swagger.codegen.v3.ISchemaHandler; |
10 | 11 | import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
|
| 12 | +import io.swagger.codegen.v3.generators.util.OpenAPIUtil; |
11 | 13 | import io.swagger.v3.core.util.Yaml;
|
12 | 14 | import io.swagger.v3.oas.models.OpenAPI;
|
13 | 15 | import io.swagger.v3.oas.models.media.ArraySchema;
|
@@ -77,6 +79,7 @@ public AbstractGoCodegen() {
|
77 | 79 | typeMapping.put("number", "float32");
|
78 | 80 | typeMapping.put("float", "float32");
|
79 | 81 | typeMapping.put("double", "float64");
|
| 82 | + typeMapping.put("BigDecimal", "float64"); |
80 | 83 | typeMapping.put("boolean", "bool");
|
81 | 84 | typeMapping.put("string", "string");
|
82 | 85 | typeMapping.put("UUID", "string");
|
@@ -265,6 +268,14 @@ public String getTypeDeclaration(Schema schema) {
|
265 | 268 | @Override
|
266 | 269 | public String getSchemaType(Schema schema) {
|
267 | 270 | String schemaType = super.getSchemaType(schema);
|
| 271 | + |
| 272 | + if (schema.get$ref() != null) { |
| 273 | + final Schema refSchema = OpenAPIUtil.getSchemaFromName(schemaType, this.openAPI); |
| 274 | + if (refSchema != null && !isObjectSchema(refSchema)) { |
| 275 | + schemaType = super.getSchemaType(refSchema); |
| 276 | + } |
| 277 | + } |
| 278 | + |
268 | 279 | String type;
|
269 | 280 | if(typeMapping.containsKey(schemaType)) {
|
270 | 281 | type = typeMapping.get(schemaType);
|
@@ -325,34 +336,36 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
325 | 336 | boolean addedTimeImport = false;
|
326 | 337 | boolean addedOSImport = false;
|
327 | 338 | for (CodegenOperation operation : operations) {
|
328 |
| - for (CodegenParameter param : operation.allParams) { |
329 |
| - // import "os" if the operation uses files |
330 |
| - if (!addedOSImport && param.dataType == "*os.File") { |
331 |
| - imports.add(createMapping("import", "os")); |
332 |
| - addedOSImport = true; |
333 |
| - } |
334 |
| - |
335 |
| - // import "time" if the operation has a required time parameter. |
336 |
| - if (param.required) { |
337 |
| - if (!addedTimeImport && param.dataType == "time.Time") { |
338 |
| - imports.add(createMapping("import", "time")); |
339 |
| - addedTimeImport = true; |
| 339 | + for (CodegenContent codegenContent : operation.getContents()) { |
| 340 | + for (CodegenParameter param : codegenContent.getParameters()) { |
| 341 | + // import "os" if the operation uses files |
| 342 | + if (!addedOSImport && param.dataType == "*os.File") { |
| 343 | + imports.add(createMapping("import", "os")); |
| 344 | + addedOSImport = true; |
340 | 345 | }
|
341 |
| - } |
342 | 346 |
|
343 |
| - // import "optionals" package if the parameter is primitive and optional |
344 |
| - if (!param.required && getBooleanValue(param, CodegenConstants.IS_PRIMITIVE_TYPE_EXT_NAME)) { |
345 |
| - if (!addedOptionalImport) { |
346 |
| - imports.add(createMapping("import", "github.com/antihax/optional")); |
347 |
| - addedOptionalImport = true; |
| 347 | + // import "time" if the operation has a required time parameter. |
| 348 | + if (param.required) { |
| 349 | + if (!addedTimeImport && param.dataType == "time.Time") { |
| 350 | + imports.add(createMapping("import", "time")); |
| 351 | + addedTimeImport = true; |
| 352 | + } |
348 | 353 | }
|
349 |
| - // We need to specially map Time type to the optionals package |
350 |
| - if (param.dataType == "time.Time") { |
351 |
| - param.vendorExtensions.put("x-optionalDataType", "Time"); |
352 |
| - continue; |
| 354 | + |
| 355 | + // import "optionals" package if the parameter is primitive and optional |
| 356 | + if (!param.required && param.getIsPrimitiveType()) { |
| 357 | + if (!addedOptionalImport) { |
| 358 | + imports.add(createMapping("import", "github.com/antihax/optional")); |
| 359 | + addedOptionalImport = true; |
| 360 | + } |
| 361 | + // We need to specially map Time type to the optionals package |
| 362 | + if (param.dataType == "time.Time") { |
| 363 | + param.vendorExtensions.put("x-optionalDataType", "Time"); |
| 364 | + continue; |
| 365 | + } |
| 366 | + // Map optional type to dataType |
| 367 | + param.vendorExtensions.put("x-optionalDataType", param.dataType.substring(0, 1).toUpperCase() + param.dataType.substring(1)); |
353 | 368 | }
|
354 |
| - // Map optional type to dataType |
355 |
| - param.vendorExtensions.put("x-optionalDataType", param.dataType.substring(0, 1).toUpperCase() + param.dataType.substring(1)); |
356 | 369 | }
|
357 | 370 | }
|
358 | 371 | }
|
@@ -426,7 +439,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
426 | 439 |
|
427 | 440 | @Override
|
428 | 441 | public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
|
429 |
| - OpenAPI openAPI = (OpenAPI)objs.get("openapi"); |
| 442 | + OpenAPI openAPI = (OpenAPI)objs.get("openAPI"); |
430 | 443 | if(openAPI != null) {
|
431 | 444 | try {
|
432 | 445 | objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(openAPI));
|
@@ -527,4 +540,25 @@ public String toEnumName(CodegenProperty property) {
|
527 | 540 | public void setWithXml(boolean withXml) {
|
528 | 541 | this.withXml = withXml;
|
529 | 542 | }
|
| 543 | + |
| 544 | + @Override |
| 545 | + public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> allDefinitions) { |
| 546 | + final CodegenModel codegenModel = super.fromModel(name, schema, allDefinitions); |
| 547 | + if (!getBooleanValue(codegenModel, CodegenConstants.IS_ALIAS_EXT_NAME)) { |
| 548 | + boolean isAlias = schema instanceof ArraySchema |
| 549 | + || schema instanceof MapSchema |
| 550 | + || (!isObjectSchema(schema)); |
| 551 | + |
| 552 | + codegenModel.getVendorExtensions().put(CodegenConstants.IS_ALIAS_EXT_NAME, isAlias); |
| 553 | + } |
| 554 | + |
| 555 | + |
| 556 | + |
| 557 | + return codegenModel; |
| 558 | + } |
| 559 | + |
| 560 | + @Override |
| 561 | + public ISchemaHandler getSchemaHandler() { |
| 562 | + return new GoSchemaHandler(this); |
| 563 | + } |
530 | 564 | }
|
0 commit comments