Skip to content

Minor fixes for InlineModelResolver #1756

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

Merged
merged 5 commits into from
Dec 26, 2018
Merged
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 @@ -27,7 +27,6 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.core.util.Json;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
Expand All @@ -42,12 +41,11 @@

public class InlineModelResolver {
private OpenAPI openapi;
private boolean skipMatches;
private Map<String, Schema> addedModels = new HashMap<String, Schema>();
private Map<String, String> generatedSignature = new HashMap<String, String>();
static Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);
Map<String, Schema> addedModels = new HashMap<String, Schema>();
Map<String, String> generatedSignature = new HashMap<String, String>();

public void flatten(OpenAPI openapi) {
void flatten(OpenAPI openapi) {
this.openapi = openapi;

if (openapi.getComponents() == null) {
Expand Down Expand Up @@ -341,22 +339,19 @@ private String resolveModelName(String title, String key) {
}
}

public String matchGenerated(Schema model) {
if (this.skipMatches) {
return null;
}
private String matchGenerated(Schema model) {
String json = Json.pretty(model);
if (generatedSignature.containsKey(json)) {
return generatedSignature.get(json);
}
return null;
}

public void addGenerated(String name, Schema model) {
private void addGenerated(String name, Schema model) {
generatedSignature.put(Json.pretty(model), name);
}

public String uniqueName(String key) {
private String uniqueName(String key) {
if (key == null) {
key = "NULL_UNIQUE_NAME";
LOGGER.warn("null key found. Default to NULL_UNIQUE_NAME");
Expand All @@ -380,7 +375,7 @@ public String uniqueName(String key) {
return key;
}

public void flattenProperties(Map<String, Schema> properties, String path) {
private void flattenProperties(Map<String, Schema> properties, String path) {
if (properties == null) {
return;
}
Expand Down Expand Up @@ -465,28 +460,7 @@ public void flattenProperties(Map<String, Schema> properties, String path) {
}
}

@SuppressWarnings("static-method")
public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused") String path) {
String description = object.getDescription();
String example = null;
Object obj = object.getExample();

if (obj != null) {
example = obj.toString();
}
Schema inner = object.getItems();
if (inner instanceof ObjectSchema) {
ArraySchema model = new ArraySchema();
model.setDescription(description);
model.setExample(example);
model.setItems(object.getItems());
model.setName(object.getName());
return model;
}
return null;
}

public Schema modelFromProperty(ObjectSchema object, String path) {
private Schema modelFromProperty(ObjectSchema object, String path) {
String description = object.getDescription();
String example = null;
Object obj = object.getExample();
Expand All @@ -508,30 +482,14 @@ public Schema modelFromProperty(ObjectSchema object, String path) {
return model;
}

@SuppressWarnings("static-method")
public Schema modelFromProperty(MapSchema object, @SuppressWarnings("unused") String path) {
String description = object.getDescription();
String example = null;
Object obj = object.getExample();
if (obj != null) {
example = obj.toString();
}
ArraySchema model = new ArraySchema();
model.setDescription(description);
model.setName(object.getName());
model.setExample(example);
model.setItems(ModelUtils.getAdditionalProperties(object));
return model;
}

/**
* Make a Schema
*
* @param ref new property name
* @param property Schema
* @return {@link Schema} A constructed OpenAPI property
*/
public Schema makeSchema(String ref, Schema property) {
private Schema makeSchema(String ref, Schema property) {
Schema newProperty = new Schema().$ref(ref);
this.copyVendorExtensions(property, newProperty);
return newProperty;
Expand All @@ -544,7 +502,7 @@ public Schema makeSchema(String ref, Schema property) {
* @param target target property
*/

public void copyVendorExtensions(Schema source, Schema target) {
private void copyVendorExtensions(Schema source, Schema target) {
Map<String, Object> vendorExtensions = source.getExtensions();
if (vendorExtensions == null) {
return;
Expand All @@ -553,12 +511,4 @@ public void copyVendorExtensions(Schema source, Schema target) {
target.addExtension(extName, vendorExtensions.get(extName));
}
}

public boolean isSkipMatches() {
return skipMatches;
}

public void setSkipMatches(boolean skipMatches) {
this.skipMatches = skipMatches;
}
}