forked from loafle/openapi-generator-original
Make InlineModelResolver resolve vendorExtensions correctly for Schema Object (#3719)
* Fix InlineModelResolver to ensure the vendorExtensions could be accessed via schema object or inline model. * format the code * Copy vendor extension to property only.
This commit is contained in:
parent
bc2f614b92
commit
0c59aefe90
@ -56,8 +56,7 @@ public class InlineModelResolver {
|
||||
swagger.addDefinition(modelName, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (model instanceof ArrayModel) {
|
||||
} else if (model instanceof ArrayModel) {
|
||||
ArrayModel am = (ArrayModel) model;
|
||||
Property inner = am.getItems();
|
||||
|
||||
@ -95,9 +94,9 @@ public class InlineModelResolver {
|
||||
Model model = modelFromProperty(op, modelName);
|
||||
String existing = matchGenerated(model);
|
||||
if (existing != null) {
|
||||
response.setSchema(new RefProperty(existing));
|
||||
response.setSchema(this.makeRefProperty(existing, property));
|
||||
} else {
|
||||
response.setSchema(new RefProperty(modelName));
|
||||
response.setSchema(this.makeRefProperty(modelName, property));
|
||||
addGenerated(modelName, model);
|
||||
swagger.addDefinition(modelName, model);
|
||||
}
|
||||
@ -110,13 +109,14 @@ public class InlineModelResolver {
|
||||
ObjectProperty op = (ObjectProperty) inner;
|
||||
if (op.getProperties() != null && op.getProperties().size() > 0) {
|
||||
flattenProperties(op.getProperties(), pathname);
|
||||
String modelName = resolveModelName( op.getTitle(),"inline_response_" + key);
|
||||
String modelName = resolveModelName(op.getTitle(),
|
||||
"inline_response_" + key);
|
||||
Model innerModel = modelFromProperty(op, modelName);
|
||||
String existing = matchGenerated(innerModel);
|
||||
if (existing != null) {
|
||||
ap.setItems(new RefProperty(existing));
|
||||
ap.setItems(this.makeRefProperty(existing, op));
|
||||
} else {
|
||||
ap.setItems(new RefProperty(modelName));
|
||||
ap.setItems(this.makeRefProperty(modelName, op));
|
||||
addGenerated(modelName, innerModel);
|
||||
swagger.addDefinition(modelName, innerModel);
|
||||
}
|
||||
@ -130,7 +130,8 @@ public class InlineModelResolver {
|
||||
ObjectProperty op = (ObjectProperty) innerProperty;
|
||||
if (op.getProperties() != null && op.getProperties().size() > 0) {
|
||||
flattenProperties(op.getProperties(), pathname);
|
||||
String modelName = resolveModelName( op.getTitle(),"inline_response_" + key);
|
||||
String modelName = resolveModelName(op.getTitle(),
|
||||
"inline_response_" + key);
|
||||
Model innerModel = modelFromProperty(op, modelName);
|
||||
String existing = matchGenerated(innerModel);
|
||||
if (existing != null) {
|
||||
@ -191,8 +192,7 @@ public class InlineModelResolver {
|
||||
private String resolveModelName(String title, String key) {
|
||||
if (title == null) {
|
||||
return uniqueName(key);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return uniqueName(title);
|
||||
}
|
||||
}
|
||||
@ -215,7 +215,11 @@ public class InlineModelResolver {
|
||||
public String uniqueName(String key) {
|
||||
int count = 0;
|
||||
boolean done = false;
|
||||
key = key.replaceAll("[^a-z_\\.A-Z0-9 ]", ""); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
key = key.replaceAll("[^a-z_\\.A-Z0-9 ]", ""); // FIXME: a parameter
|
||||
// should not be
|
||||
// assigned. Also declare
|
||||
// the methods parameters
|
||||
// as 'final'.
|
||||
while (!done) {
|
||||
String name = key;
|
||||
if (count > 0) {
|
||||
@ -239,9 +243,8 @@ public class InlineModelResolver {
|
||||
Map<String, Model> modelsToAdd = new HashMap<String, Model>();
|
||||
for (String key : properties.keySet()) {
|
||||
Property property = properties.get(key);
|
||||
if (property instanceof ObjectProperty &&
|
||||
((ObjectProperty)property).getProperties() != null &&
|
||||
((ObjectProperty)property).getProperties().size() > 0) {
|
||||
if (property instanceof ObjectProperty && ((ObjectProperty) property).getProperties() != null
|
||||
&& ((ObjectProperty) property).getProperties().size() > 0) {
|
||||
|
||||
ObjectProperty op = (ObjectProperty) property;
|
||||
|
||||
@ -320,6 +323,7 @@ public class InlineModelResolver {
|
||||
if (obj != null) {
|
||||
example = obj.toString();
|
||||
}
|
||||
|
||||
Property inner = object.getItems();
|
||||
if (inner instanceof ObjectProperty) {
|
||||
ArrayModel model = new ArrayModel();
|
||||
@ -328,6 +332,7 @@ public class InlineModelResolver {
|
||||
model.setItems(object.getItems());
|
||||
return model;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -375,6 +380,32 @@ public class InlineModelResolver {
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a RefProperty
|
||||
*
|
||||
* @param ref
|
||||
* @param property
|
||||
* @return
|
||||
*/
|
||||
public Property makeRefProperty(String ref, Property property) {
|
||||
RefProperty newProperty = new RefProperty(ref);
|
||||
this.copyVendorExtensions(property, newProperty);
|
||||
return newProperty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy vendor extensions from Property to another Property
|
||||
*
|
||||
* @param source
|
||||
* @param target
|
||||
*/
|
||||
public void copyVendorExtensions(Property source, AbstractProperty target) {
|
||||
Map<String, Object> vendorExtensions = source.getVendorExtensions();
|
||||
for (String extName : vendorExtensions.keySet()) {
|
||||
target.setVendorExtension(extName, vendorExtensions.get(extName));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSkipMatches() {
|
||||
return skipMatches;
|
||||
}
|
||||
|
@ -191,21 +191,24 @@ public class InlineModelResolverTest {
|
||||
.response(200, new Response()
|
||||
.description("it works!")
|
||||
.schema(new ObjectProperty()
|
||||
.property("name", new StringProperty())))))
|
||||
.property("name", new StringProperty()).vendorExtension("x-ext", "ext-prop")))))
|
||||
.path("/foo/baz", new Path()
|
||||
.get(new Operation()
|
||||
.response(200, new Response()
|
||||
.vendorExtension("x-foo", "bar")
|
||||
.description("it works!")
|
||||
.schema(new ObjectProperty()
|
||||
.property("name", new StringProperty())))));
|
||||
.property("name", new StringProperty()).vendorExtension("x-ext", "ext-prop")))));
|
||||
new InlineModelResolver().flatten(swagger);
|
||||
|
||||
Map<String, Response> responses = swagger.getPaths().get("/foo/bar").getGet().getResponses();
|
||||
|
||||
Response response = responses.get("200");
|
||||
assertNotNull(response);
|
||||
assertTrue(response.getSchema() instanceof RefProperty);
|
||||
Property schema = response.getSchema();
|
||||
assertTrue(schema instanceof RefProperty);
|
||||
assertEquals(1, schema.getVendorExtensions().size());
|
||||
assertEquals("ext-prop", schema.getVendorExtensions().get("x-ext"));
|
||||
|
||||
ModelImpl model = (ModelImpl)swagger.getDefinitions().get("inline_response_200");
|
||||
assertTrue(model.getProperties().size() == 1);
|
||||
@ -433,15 +436,17 @@ public class InlineModelResolverTest {
|
||||
public void resolveInlineArrayResponse() throws Exception {
|
||||
Swagger swagger = new Swagger();
|
||||
|
||||
ArrayProperty schema = new ArrayProperty()
|
||||
.items(new ObjectProperty()
|
||||
.property("name", new StringProperty())
|
||||
.vendorExtension("x-ext", "ext-items"))
|
||||
.vendorExtension("x-ext", "ext-prop");
|
||||
swagger.path("/foo/baz", new Path()
|
||||
.get(new Operation()
|
||||
.response(200, new Response()
|
||||
.vendorExtension("x-foo", "bar")
|
||||
.description("it works!")
|
||||
.schema(new ArrayProperty()
|
||||
.items(
|
||||
new ObjectProperty()
|
||||
.property("name", new StringProperty()))))));
|
||||
.schema(schema))));
|
||||
|
||||
new InlineModelResolver().flatten(swagger);
|
||||
|
||||
@ -455,6 +460,9 @@ public class InlineModelResolverTest {
|
||||
assertTrue(responseProperty instanceof ArrayProperty);
|
||||
|
||||
ArrayProperty ap = (ArrayProperty) responseProperty;
|
||||
assertEquals(1, ap.getVendorExtensions().size());
|
||||
assertEquals("ext-prop", ap.getVendorExtensions().get("x-ext"));
|
||||
|
||||
Property p = ap.getItems();
|
||||
|
||||
assertNotNull(p);
|
||||
@ -463,6 +471,8 @@ public class InlineModelResolverTest {
|
||||
assertEquals(rp.getType(), "ref");
|
||||
assertEquals(rp.get$ref(), "#/definitions/inline_response_200");
|
||||
assertEquals(rp.getSimpleRef(), "inline_response_200");
|
||||
assertEquals(1, rp.getVendorExtensions().size());
|
||||
assertEquals("ext-items", rp.getVendorExtensions().get("x-ext"));
|
||||
|
||||
Model inline = swagger.getDefinitions().get("inline_response_200");
|
||||
assertNotNull(inline);
|
||||
@ -522,6 +532,7 @@ public class InlineModelResolverTest {
|
||||
|
||||
MapProperty schema = new MapProperty();
|
||||
schema.setAdditionalProperties(new StringProperty());
|
||||
schema.setVendorExtension("x-ext", "ext-prop");
|
||||
|
||||
swagger.path("/foo/baz", new Path()
|
||||
.get(new Operation()
|
||||
@ -537,6 +548,8 @@ public class InlineModelResolverTest {
|
||||
Property property = response.getSchema();
|
||||
assertTrue(property instanceof MapProperty);
|
||||
assertTrue(swagger.getDefinitions().size() == 0);
|
||||
assertEquals(1, property.getVendorExtensions().size());
|
||||
assertEquals("ext-prop", property.getVendorExtensions().get("x-ext"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -546,6 +559,7 @@ public class InlineModelResolverTest {
|
||||
MapProperty schema = new MapProperty();
|
||||
schema.setAdditionalProperties(new ObjectProperty()
|
||||
.property("name", new StringProperty()));
|
||||
schema.setVendorExtension("x-ext", "ext-prop");
|
||||
|
||||
swagger.path("/foo/baz", new Path()
|
||||
.get(new Operation()
|
||||
@ -558,6 +572,8 @@ public class InlineModelResolverTest {
|
||||
Response response = swagger.getPaths().get("/foo/baz").getGet().getResponses().get("200");
|
||||
Property property = response.getSchema();
|
||||
assertTrue(property instanceof MapProperty);
|
||||
assertEquals(1, property.getVendorExtensions().size());
|
||||
assertEquals("ext-prop", property.getVendorExtensions().get("x-ext"));
|
||||
assertTrue(swagger.getDefinitions().size() == 1);
|
||||
|
||||
Model inline = swagger.getDefinitions().get("inline_response_200");
|
||||
|
Loading…
x
Reference in New Issue
Block a user