Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
for (Map.Entry<String, ModelsMap> entry : objs.entrySet()) {
CodegenModel model = ModelUtils.getModelByName(entry.getKey(), objs);

if (model == null) {
LOGGER.warn("Null model found in postProcessAllModels: {}", entry.getKey());
continue;
}

// add the model to the discriminator's mapping so templates have access to more than just the string to string mapping
if (model.discriminator != null && model.discriminator.getMappedModels() != null) {
for (CodegenDiscriminator.MappedModel mappedModel : model.discriminator.getMappedModels()) {
Expand Down Expand Up @@ -5977,6 +5982,10 @@ Map<String, String> getAllAliases(Map<String, Schema> schemas) {
}

private static Boolean isAliasOfSimpleTypes(Schema schema) {
if (schema == null) {
return false;
}

return (!ModelUtils.isObjectSchema(schema)
&& !ModelUtils.isArraySchema(schema)
&& !ModelUtils.isMapSchema(schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1334,8 +1334,10 @@ private ModelsMap processModels(CodegenConfig config, Map<String, Schema> defini
for (Map.Entry<String, Schema> definitionsEntry : definitions.entrySet()) {
String key = definitionsEntry.getKey();
Schema schema = definitionsEntry.getValue();
if (schema == null)
throw new RuntimeException("schema cannot be null in processModels");
if (schema == null) {
LOGGER.warn("Schema {} cannot be null in processModels", key);
continue;
}
CodegenModel cm = config.fromModel(key, schema);
ModelMap mo = new ModelMap();
mo.setModel(cm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (props != null) {
for (String propName : props.keySet()) {
Schema prop = props.get(propName);

if (prop == null) {
continue;
}

String schemaName = resolveModelName(prop.getTitle(), modelPrefix + "_" + propName);
// Recurse to create $refs for inner models
gatherInlineModels(prop, schemaName);
Expand All @@ -308,13 +313,15 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (schema.getAdditionalProperties() != null) {
if (schema.getAdditionalProperties() instanceof Schema) {
Schema inner = (Schema) schema.getAdditionalProperties();
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaOptions.get("MAP_ITEM_SUFFIX"));
// Recurse to create $refs for inner models
gatherInlineModels(inner, schemaName);
if (isModelNeeded(inner)) {
// If this schema should be split into its own model, do so
Schema refSchema = this.makeSchemaInComponents(schemaName, inner);
schema.setAdditionalProperties(refSchema);
if (inner != null) {
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaOptions.get("MAP_ITEM_SUFFIX"));
// Recurse to create $refs for inner models
gatherInlineModels(inner, schemaName);
if (isModelNeeded(inner)) {
// If this schema should be split into its own model, do so
Schema refSchema = this.makeSchemaInComponents(schemaName, inner);
schema.setAdditionalProperties(refSchema);
}
}
}
}
Expand All @@ -334,10 +341,6 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (schema instanceof ArraySchema) {
ArraySchema array = (ArraySchema) schema;
Schema items = array.getItems();
/*if (items.getTitle() != null) {
LOGGER.info("schema title {}", items);
throw new RuntimeException("getTitle for array item is not null");
}*/
if (items == null) {
LOGGER.error("Illegal schema found with array type but no items," +
" items must be defined for array schemas:\n " + schema.toString());
Expand All @@ -360,6 +363,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
List<Schema> newAllOf = new ArrayList<Schema>();
boolean atLeastOneModel = false;
for (Object inner : schema.getAllOf()) {
if (inner == null) {
continue;
}
String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_allOf");
// Recurse to create $refs for inner models
gatherInlineModels((Schema) inner, schemaName);
Expand Down Expand Up @@ -392,6 +398,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (schema.getAnyOf() != null) {
List<Schema> newAnyOf = new ArrayList<Schema>();
for (Object inner : schema.getAnyOf()) {
if (inner == null) {
continue;
}
String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_anyOf");
// Recurse to create $refs for inner models
gatherInlineModels((Schema) inner, schemaName);
Expand All @@ -407,6 +416,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (schema.getOneOf() != null) {
List<Schema> newOneOf = new ArrayList<Schema>();
for (Object inner : schema.getOneOf()) {
if (inner == null) {
continue;
}
String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_oneOf");
// Recurse to create $refs for inner models
gatherInlineModels((Schema) inner, schemaName);
Expand All @@ -423,12 +435,14 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
// Check not schema
if (schema.getNot() != null) {
Schema not = schema.getNot();
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_not");
// Recurse to create $refs for inner models
gatherInlineModels(not, schemaName);
if (isModelNeeded(not)) {
Schema refSchema = this.makeSchemaInComponents(schemaName, not);
schema.setNot(refSchema);
if (not != null) {
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_not");
// Recurse to create $refs for inner models
gatherInlineModels(not, schemaName);
if (isModelNeeded(not)) {
Schema refSchema = this.makeSchemaInComponents(schemaName, not);
schema.setNot(refSchema);
}
}
}
}
Expand Down Expand Up @@ -629,6 +643,9 @@ private void flattenComponents() {
List<String> modelNames = new ArrayList<String>(models.keySet());
for (String modelName : modelNames) {
Schema model = models.get(modelName);
if (model == null) {
continue;
}
if (ModelUtils.isAnyOf(model)) { // contains anyOf only
gatherInlineModels(model, modelName);
} else if (ModelUtils.isOneOf(model)) { // contains oneOf only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ public static boolean isTypeObjectSchema(Schema schema) {
* @return true if the specified schema is an Object schema.
*/
public static boolean isObjectSchema(Schema schema) {
if (schema == null) {
return false;
}

return (schema instanceof ObjectSchema) ||
// must not be a map
(SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && !(schema instanceof MapSchema)) ||
Expand Down Expand Up @@ -549,6 +553,10 @@ public static boolean isComplexComposedSchema(Schema schema) {
* @return true if the specified schema is a Map schema.
*/
public static boolean isMapSchema(Schema schema) {
if (schema == null) {
return false;
}

// additionalProperties explicitly set to false
if (schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) {
return false;
Expand Down Expand Up @@ -1858,6 +1866,10 @@ public static boolean isAllOfWithProperties(Schema schema) {
* @return true if the schema contains oneOf but no properties/allOf/anyOf defined.
*/
public static boolean isOneOf(Schema schema) {
if (schema == null) {
return false;
}

if (hasOneOf(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty()) &&
(schema.getAllOf() == null || schema.getAllOf().isEmpty()) &&
(schema.getAnyOf() == null || schema.getAnyOf().isEmpty())) {
Expand All @@ -1875,7 +1887,7 @@ public static boolean isOneOf(Schema schema) {
* @return true if allOf is not empty
*/
public static boolean hasOneOf(Schema schema) {
if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
if (schema != null && schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
return true;
}

Expand All @@ -1890,6 +1902,10 @@ public static boolean hasOneOf(Schema schema) {
* @return true if the schema contains oneOf but no properties/allOf/anyOf defined.
*/
public static boolean isAnyOf(Schema schema) {
if (schema == null) {
return false;
}

if (hasAnyOf(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty()) &&
(schema.getAllOf() == null || schema.getAllOf().isEmpty()) &&
(schema.getOneOf() == null || schema.getOneOf().isEmpty())) {
Expand All @@ -1907,7 +1923,7 @@ public static boolean isAnyOf(Schema schema) {
* @return true if anyOf is not empty
*/
public static boolean hasAnyOf(Schema schema) {
if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
if (schema != null && schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
return true;
}

Expand Down