Skip to content

Commit 5521c04

Browse files
committed
tweaked additional properties and fixed conflicts with previous version.
1 parent 678ad89 commit 5521c04

File tree

59 files changed

+346
-322
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+346
-322
lines changed

modules/swagger-codegen/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@
276276
<!-- <version>${jmockit-version}</version> -->
277277
<scope>test</scope>
278278
</dependency>
279+
<dependency>
280+
<groupId>com.google.guava</groupId>
281+
<artifactId>guava</artifactId>
282+
<version>23.0</version>
283+
</dependency>
284+
279285
<dependency>
280286
<groupId>com.googlecode.java-diff-utils</groupId>
281287
<artifactId>diffutils</artifactId>

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import com.google.common.base.Function;
55
import com.google.common.collect.Lists;
66
import com.samskivert.mustache.Mustache.Compiler;
7-
import io.swagger.codegen.languages.helpers.ExtensionHelper;
87
import io.swagger.codegen.languages.helpers.HasHelper;
98
import io.swagger.codegen.languages.helpers.HasNotHelper;
109
import io.swagger.codegen.languages.helpers.IsHelper;
1110
import io.swagger.codegen.languages.helpers.IsNotHelper;
12-
import io.swagger.codegen.languages.helpers.NoneExtensionHelper;
1311
import io.swagger.codegen.utils.ModelUtils;
1412
import io.swagger.v3.core.util.Json;
1513
import io.swagger.v3.oas.models.OpenAPI;
@@ -43,6 +41,7 @@
4341
import io.swagger.v3.oas.models.security.OAuthFlows;
4442
import io.swagger.v3.oas.models.security.SecurityScheme;
4543
import io.swagger.v3.parser.util.SchemaTypeUtil;
44+
import org.apache.commons.lang3.ArrayUtils;
4645
import org.apache.commons.lang3.ObjectUtils;
4746
import org.apache.commons.lang3.StringEscapeUtils;
4847
import org.apache.commons.lang3.StringUtils;
@@ -52,6 +51,7 @@
5251
import javax.annotation.Nullable;
5352
import java.io.File;
5453
import java.util.ArrayList;
54+
import java.util.Arrays;
5555
import java.util.Collections;
5656
import java.util.Comparator;
5757
import java.util.HashMap;
@@ -64,6 +64,7 @@
6464
import java.util.TreeSet;
6565
import java.util.regex.Matcher;
6666
import java.util.regex.Pattern;
67+
import java.util.stream.Collectors;
6768

6869
import static io.swagger.codegen.CodegenHelper.getDefaultIncludes;
6970
import static io.swagger.codegen.CodegenHelper.getImportMappings;
@@ -811,8 +812,8 @@ else if (Parameter.StyleEnum.SPACEDELIMITED.equals(queryParameter.getStyle())) {
811812
* @return string presentation of the instantiation type of the property
812813
*/
813814
public String toInstantiationType(Schema property) {
814-
if (property instanceof MapSchema || property.getAdditionalProperties() != null) {
815-
Schema additionalProperties = property.getAdditionalProperties();
815+
if (property instanceof MapSchema || property.getAdditionalProperties() != null && property.getAdditionalProperties() instanceof Schema) {
816+
Schema additionalProperties = (Schema) property.getAdditionalProperties();
816817
String type = additionalProperties.getType();
817818
if (null == type) {
818819
LOGGER.error("No Type defined for Additional Property " + additionalProperties + "\n" //
@@ -1506,7 +1507,7 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
15061507
Schema items = ((ArraySchema) propertySchema).getItems();
15071508
CodegenProperty innerCodegenProperty = fromProperty(itemName, items);
15081509
updatePropertyForArray(codegenProperty, innerCodegenProperty);
1509-
} else if (propertySchema instanceof MapSchema || propertySchema.getAdditionalProperties() != null) {
1510+
} else if (propertySchema instanceof MapSchema || (propertySchema.getAdditionalProperties() != null && propertySchema.getAdditionalProperties() instanceof Schema)) {
15101511

15111512
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_CONTAINER_EXT_NAME, Boolean.TRUE);
15121513
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_MAP_CONTAINER_EXT_NAME, Boolean.TRUE);
@@ -1516,7 +1517,7 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
15161517
codegenProperty.maxItems = propertySchema.getMaxProperties();
15171518

15181519
// handle inner property
1519-
CodegenProperty cp = fromProperty("inner", propertySchema.getAdditionalProperties());
1520+
CodegenProperty cp = fromProperty("inner", (Schema) propertySchema.getAdditionalProperties());
15201521
updatePropertyForMap(codegenProperty, cp);
15211522
} else {
15221523
if (StringUtils.isNotBlank(propertySchema.get$ref())) {
@@ -1789,7 +1790,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
17891790
codegenOperation.returnBaseType = innerProperty.baseType;
17901791
} else if (responseSchema instanceof MapSchema) {
17911792
MapSchema mapSchema = (MapSchema) responseSchema;
1792-
CodegenProperty innerProperty = fromProperty("response", mapSchema.getAdditionalProperties());
1793+
CodegenProperty innerProperty = fromProperty("response", (Schema) mapSchema.getAdditionalProperties());
17931794
codegenOperation.returnBaseType = innerProperty.baseType;
17941795
} else {
17951796
if (codegenProperty.complexType != null) {
@@ -2098,7 +2099,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
20982099
codegenProperty = codegenProperty.items;
20992100
}
21002101
} else if (parameterSchema instanceof MapSchema) { // for map parameter
2101-
CodegenProperty codegenProperty = fromProperty("inner", parameterSchema.getAdditionalProperties());
2102+
CodegenProperty codegenProperty = fromProperty("inner", (Schema) parameterSchema.getAdditionalProperties());
21022103
codegenParameter.items = codegenProperty;
21032104
codegenParameter.baseType = codegenProperty.datatype;
21042105
codegenParameter.getVendorExtensions().put(CodegenConstants.IS_CONTAINER_EXT_NAME, Boolean.TRUE);
@@ -3374,7 +3375,7 @@ protected void addConsumesInfo(Operation operation, CodegenOperation codegenOper
33743375
codegenOperation.getVendorExtensions().put(CodegenConstants.HAS_CONSUMES_EXT_NAME, Boolean.TRUE);
33753376
}
33763377

3377-
protected Set<String> getConsumesInfo(Operation operation) {
3378+
public static Set<String> getConsumesInfo(Operation operation) {
33783379
if(operation.getRequestBody() == null || operation.getRequestBody().getContent() == null || operation.getRequestBody().getContent().isEmpty()) {
33793380
return null;
33803381
}
@@ -3410,7 +3411,7 @@ protected void addProducesInfo(ApiResponse response, CodegenOperation codegenOpe
34103411
}
34113412

34123413

3413-
protected Set<String> getProducesInfo(Operation operation) {
3414+
public static Set<String> getProducesInfo(Operation operation) {
34143415
if(operation.getResponses() == null || operation.getResponses().isEmpty()) {
34153416
return null;
34163417
}
@@ -3468,6 +3469,11 @@ else if (Parameter.StyleEnum.SPACEDELIMITED.equals(parameter.getStyle())) {
34683469
}
34693470
}
34703471

3472+
protected static boolean hasSchemaProperties(Schema schema) {
3473+
final Object additionalProperties = schema.getAdditionalProperties();
3474+
return additionalProperties != null && additionalProperties instanceof Schema;
3475+
}
3476+
34713477
public CodegenType getTag() {
34723478
return null;
34733479
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/examples/ExampleGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,14 @@ private Object resolvePropertyToExample(String propertyName, String mediaType, S
197197
return (long) randomNumber(min, max);
198198
}
199199
return (int) randomNumber(min, max);
200-
} else if (property instanceof MapSchema) {
200+
} else if (property instanceof MapSchema && property.getAdditionalProperties() != null && property.getAdditionalProperties() instanceof Schema) {
201201
Map<String, Object> mp = new HashMap<String, Object>();
202202
if (property.getName() != null) {
203203
mp.put(property.getName(),
204-
resolvePropertyToExample(propertyName, mediaType, property.getAdditionalProperties(), processedModels));
204+
resolvePropertyToExample(propertyName, mediaType, (Schema) property.getAdditionalProperties(), processedModels));
205205
} else {
206206
mp.put("key",
207-
resolvePropertyToExample(propertyName, mediaType, property.getAdditionalProperties(), processedModels));
207+
resolvePropertyToExample(propertyName, mediaType, (Schema) property.getAdditionalProperties(), processedModels));
208208
}
209209
return mp;
210210
} else if (property instanceof ObjectSchema) {

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractAdaCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ public String getTypeDeclaration(Schema property) {
314314
Schema inner = arraySchema.getItems();
315315
return getTypeDeclaration(inner) + "_Vectors.Vector";
316316
}
317-
if (property instanceof MapSchema) {
317+
if (property instanceof MapSchema && hasSchemaProperties(property)) {
318318
MapSchema mapSchema = (MapSchema) property;
319-
Schema inner = mapSchema.getAdditionalProperties();
319+
Schema inner = (Schema) mapSchema.getAdditionalProperties();
320320
String name = getTypeDeclaration(inner) + "_Map";
321321
if (name.startsWith("Swagger.")) {
322322
return name;

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@ public String getTypeDeclaration(Schema propertySchema) {
710710
if (propertySchema instanceof ArraySchema) {
711711
Schema inner = ((ArraySchema) propertySchema).getItems();
712712
return String.format("%s<%s>", getSchemaType(propertySchema), getTypeDeclaration(inner));
713-
} else if (propertySchema instanceof MapSchema) {
714-
Schema inner = propertySchema.getAdditionalProperties();
713+
} else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
714+
Schema inner = (Schema) propertySchema.getAdditionalProperties();
715715
return String.format("%s<string, %s>", getSchemaType(propertySchema), getTypeDeclaration(inner));
716716
}
717717
return super.getTypeDeclaration(propertySchema);

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractEiffelCodegen.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ public String getTypeDeclaration(Schema schema) {
271271
Schema inner = ((ArraySchema) schema).getItems();
272272
return String.format("LIST [%s]", getTypeDeclaration(inner));
273273

274-
} else if (schema instanceof MapSchema) {
275-
Schema inner = schema.getAdditionalProperties();
274+
} else if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
275+
Schema inner = (Schema) schema.getAdditionalProperties();
276276
return String.format("%s[%s]", getSchemaType(schema), getTypeDeclaration(inner));
277277
}
278278
// return super.getTypeDeclaration(p);
@@ -541,8 +541,8 @@ public Map<String, String> createMapping(String key, String value) {
541541

542542
@Override
543543
public String toInstantiationType(Schema schema) {
544-
if (schema instanceof MapSchema) {
545-
Schema additionalProperties2 = schema.getAdditionalProperties();
544+
if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
545+
Schema additionalProperties2 = (Schema) schema.getAdditionalProperties();
546546
String type = additionalProperties2.getType();
547547
if (null == type) {
548548
LOGGER.error("No Type defined for Additional Property " + additionalProperties2 + "\n" //

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package io.swagger.codegen.languages;
22

33
import io.swagger.codegen.*;
4-
import io.swagger.models.properties.ArrayProperty;
5-
import io.swagger.models.properties.MapProperty;
6-
import io.swagger.models.properties.Property;
7-
import io.swagger.models.parameters.Parameter;
8-
import io.swagger.models.Swagger;
9-
import io.swagger.util.Yaml;
104

115
import java.util.*;
126

7+
import io.swagger.v3.core.util.Yaml;
8+
import io.swagger.v3.oas.models.OpenAPI;
9+
import io.swagger.v3.oas.models.media.ArraySchema;
10+
import io.swagger.v3.oas.models.media.MapSchema;
11+
import io.swagger.v3.oas.models.media.Schema;
1312
import org.apache.commons.lang3.StringUtils;
1413

1514
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -219,41 +218,41 @@ public void postProcessParameter(CodegenParameter parameter){
219218
}
220219

221220
@Override
222-
public String getTypeDeclaration(Property p) {
223-
if(p instanceof ArrayProperty) {
224-
ArrayProperty ap = (ArrayProperty) p;
225-
Property inner = ap.getItems();
221+
public String getTypeDeclaration(Schema schema) {
222+
if(schema instanceof ArraySchema) {
223+
ArraySchema arraySchema = (ArraySchema) schema;
224+
Schema inner = arraySchema.getItems();
226225
return "[]" + getTypeDeclaration(inner);
227226
}
228-
else if (p instanceof MapProperty) {
229-
MapProperty mp = (MapProperty) p;
230-
Property inner = mp.getAdditionalProperties();
227+
else if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
228+
MapSchema mapSchema = (MapSchema) schema;
229+
Schema inner = (Schema) mapSchema.getAdditionalProperties();
231230

232-
return getSwaggerType(p) + "[string]" + getTypeDeclaration(inner);
231+
return getSchemaType(schema) + "[string]" + getTypeDeclaration(inner);
233232
}
234233
//return super.getTypeDeclaration(p);
235234

236235
// Not using the supertype invocation, because we want to UpperCamelize
237236
// the type.
238-
String swaggerType = getSwaggerType(p);
239-
if (typeMapping.containsKey(swaggerType)) {
240-
return typeMapping.get(swaggerType);
237+
String schemaType = getSchemaType(schema);
238+
if (typeMapping.containsKey(schemaType)) {
239+
return typeMapping.get(schemaType);
241240
}
242241

243-
if(typeMapping.containsValue(swaggerType)) {
244-
return swaggerType;
242+
if(typeMapping.containsValue(schemaType)) {
243+
return schemaType;
245244
}
246245

247-
if(languageSpecificPrimitives.contains(swaggerType)) {
248-
return swaggerType;
246+
if(languageSpecificPrimitives.contains(schemaType)) {
247+
return schemaType;
249248
}
250249

251-
return toModelName(swaggerType);
250+
return toModelName(schemaType);
252251
}
253252

254253
@Override
255-
public String getSwaggerType(Property p) {
256-
String swaggerType = super.getSwaggerType(p);
254+
public String getSchemaType(Schema schema) {
255+
String swaggerType = super.getSchemaType(schema);
257256
String type = null;
258257
if(typeMapping.containsKey(swaggerType)) {
259258
type = typeMapping.get(swaggerType);
@@ -367,10 +366,10 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
367366

368367
@Override
369368
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
370-
Swagger swagger = (Swagger)objs.get("swagger");
371-
if(swagger != null) {
369+
OpenAPI openAPI = (OpenAPI)objs.get("openapi");
370+
if(openAPI != null) {
372371
try {
373-
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(swagger));
372+
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(openAPI));
374373
} catch (JsonProcessingException e) {
375374
LOGGER.error(e.getMessage(), e);
376375
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,15 @@ public String getTypeDeclaration(Schema propertySchema) {
632632
return String.format("%s<%s>", getSchemaType(propertySchema), getTypeDeclaration(inner));
633633
// return getSwaggerType(propertySchema) + "<" + getTypeDeclaration(inner) + ">";
634634
} else if (propertySchema instanceof MapSchema || propertySchema.getAdditionalProperties() != null) {
635-
Schema inner = propertySchema.getAdditionalProperties();
636-
if (inner == null) {
637-
LOGGER.warn(propertySchema.getName() + "(map property) does not have a proper inner type defined");
638-
// TODO maybe better defaulting to StringProperty than returning null
639-
return null;
635+
if (hasSchemaProperties(propertySchema)) {
636+
Schema inner = (Schema) propertySchema.getAdditionalProperties();
637+
if (inner == null) {
638+
LOGGER.warn(propertySchema.getName() + "(map property) does not have a proper inner type defined");
639+
// TODO maybe better defaulting to StringProperty than returning null
640+
return null;
641+
}
642+
return getSchemaType(propertySchema) + "<String, " + getTypeDeclaration(inner) + ">";
640643
}
641-
return getSchemaType(propertySchema) + "<String, " + getTypeDeclaration(inner) + ">";
642644
}
643645
return super.getTypeDeclaration(propertySchema);
644646
}
@@ -675,7 +677,7 @@ public String toDefaultValue(Schema schema) {
675677
}
676678

677679
return String.format(pattern, typeDeclaration);
678-
} else if (schema instanceof MapSchema) {
680+
} else if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
679681
final String pattern;
680682
if (fullJavaUtil) {
681683
pattern = "new java.util.HashMap<%s>()";
@@ -686,7 +688,7 @@ public String toDefaultValue(Schema schema) {
686688
return null;
687689
}
688690

689-
String typeDeclaration = String.format("String, %s", getTypeDeclaration(schema.getAdditionalProperties()));
691+
String typeDeclaration = String.format("String, %s", getTypeDeclaration((Schema) schema.getAdditionalProperties()));
690692
Object java8obj = additionalProperties.get("java8");
691693
if (java8obj != null) {
692694
Boolean java8 = Boolean.valueOf(java8obj.toString());

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractPhpCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ public String getTypeDeclaration(Schema schema) {
311311
ArraySchema arraySchema = (ArraySchema) schema;
312312
Schema inner = arraySchema.getItems();
313313
return getTypeDeclaration(inner) + "[]";
314-
} else if (schema instanceof MapSchema) {
315-
Schema inner = schema.getAdditionalProperties();
314+
} else if (schema instanceof MapSchema && hasSchemaProperties(schema)) {
315+
Schema inner = (Schema) schema.getAdditionalProperties();
316316
return String.format("%s[string,%s]", getSchemaType(schema), getTypeDeclaration(inner));
317317
} else if (StringUtils.isNotBlank(schema.get$ref())) {
318318
String type = super.getTypeDeclaration(schema);

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractScalaCodegen.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public String getTypeDeclaration(Schema propertySchema) {
150150
if (propertySchema instanceof ArraySchema) {
151151
Schema inner = ((ArraySchema) propertySchema).getItems();
152152
return String.format("%s[%s]", getSchemaType(propertySchema), getTypeDeclaration(inner));
153-
} else if (propertySchema instanceof MapSchema) {
154-
Schema inner = propertySchema.getAdditionalProperties();
153+
} else if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
154+
Schema inner = (Schema) propertySchema.getAdditionalProperties();
155155
return String.format("%s[String, %s]", getSchemaType(propertySchema), getTypeDeclaration(inner));
156156
}
157157
return super.getTypeDeclaration(propertySchema);
@@ -172,8 +172,8 @@ public String getSchemaType(Schema propertySchema) {
172172

173173
@Override
174174
public String toInstantiationType(Schema schemaProperty) {
175-
if (schemaProperty instanceof MapSchema) {
176-
String inner = getSchemaType(schemaProperty.getAdditionalProperties());
175+
if (schemaProperty instanceof MapSchema && hasSchemaProperties(schemaProperty)) {
176+
String inner = getSchemaType((Schema) schemaProperty.getAdditionalProperties());
177177
return String.format("%s[%s]", instantiationTypes.get("map"), inner);
178178
} else if (schemaProperty instanceof ArraySchema) {
179179
ArraySchema arraySchema = (ArraySchema) schemaProperty;
@@ -186,8 +186,8 @@ public String toInstantiationType(Schema schemaProperty) {
186186

187187
@Override
188188
public String toDefaultValue(Schema propertySchema) {
189-
if (propertySchema instanceof MapSchema) {
190-
String inner = getSchemaType(propertySchema.getAdditionalProperties());
189+
if (propertySchema instanceof MapSchema && hasSchemaProperties(propertySchema)) {
190+
String inner = getSchemaType((Schema) propertySchema.getAdditionalProperties());
191191
return String.format("new HashMap[String, %s]()", inner);
192192
} else if(propertySchema instanceof ArraySchema) {
193193
ArraySchema arraySchema = (ArraySchema) propertySchema;

0 commit comments

Comments
 (0)