Skip to content

Add models copy utility methods #3848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,7 +1,7 @@
package io.swagger.v3.core.filter;

import io.swagger.v3.core.model.ApiDescription;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.CopyUtil;
import io.swagger.v3.core.util.RefUtils;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
Expand All @@ -22,7 +22,6 @@
import io.swagger.v3.oas.models.tags.Tag;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -276,19 +275,14 @@ protected Map<String, Schema> filterComponentsSchema(OpenAPISpecFilter filter, M
}
}

try {
// TODO solve this, and generally handle clone and passing references
Schema clonedModel = Json.mapper().readValue(Json.pretty(definition), Schema.class);
if (clonedModel.getProperties() != null) {
clonedModel.getProperties().clear();
}
if (!clonedProperties.isEmpty()) {
clonedModel.setProperties(clonedProperties);
}
clonedComponentsSchema.put(key, clonedModel);

} catch (IOException e) {
Schema clonedModel = CopyUtil.copy(definition);
if (clonedModel.getProperties() != null) {
clonedModel.getProperties().clear();
}
if (!clonedProperties.isEmpty()) {
clonedModel.setProperties(clonedProperties);
}
clonedComponentsSchema.put(key, clonedModel);
}
}
return clonedComponentsSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import io.swagger.v3.core.converter.ModelConverterContext;
import io.swagger.v3.core.util.AnnotationsUtils;
import io.swagger.v3.core.util.Constants;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.CopyUtil;
import io.swagger.v3.core.util.ObjectMapperFactory;
import io.swagger.v3.core.util.OptionalUtils;
import io.swagger.v3.core.util.PrimitiveType;
Expand Down Expand Up @@ -894,15 +894,12 @@ protected Type findJsonValueType(final BeanDescription beanDesc) {
}

private Schema clone(Schema property) {
if(property == null)
if(property == null) {
return property;
try {
String cloneName = property.getName();
property = Json.mapper().readValue(Json.pretty(property), Schema.class);
property.setName(cloneName);
} catch (IOException e) {
LOGGER.error("Could not clone property, e");
}
String cloneName = property.getName();
property = CopyUtil.copy(property);
property.setName(cloneName);
return property;
}

Expand Down Expand Up @@ -1033,14 +1030,9 @@ private void handleUnwrapped(List<Schema> props, Schema innerModel, String prefi
}
if (innerModel.getProperties() != null) {
for (Schema prop : (Collection<Schema>) innerModel.getProperties().values()) {
try {
Schema clonedProp = Json.mapper().readValue(Json.pretty(prop), Schema.class);
clonedProp.setName(prefix + prop.getName() + suffix);
props.add(clonedProp);
} catch (IOException e) {
LOGGER.error("Exception cloning property", e);
return;
}
Schema clonedProp = CopyUtil.copy(prop);
clonedProp.setName(prefix + prop.getName() + suffix);
props.add(clonedProp);
}
}
}
Expand Down
Loading