Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into sorin/fix-ref-query…
Browse files Browse the repository at this point in the history
…-param-explode

* origin/master:
  Add martindelille to code owners (OpenAPITools#12328)
  add tests to set httpUserAgent in r client (OpenAPITools#12321)
  better error messages for oneOf in java okhttp-gson (OpenAPITools#12311)
  [Inline model resolver] various improvements (OpenAPITools#12293)
  • Loading branch information
sorin-florea committed May 10, 2022
2 parents 9b3be0e + 6931f15 commit e4cd1a5
Show file tree
Hide file tree
Showing 200 changed files with 9,797 additions and 8,076 deletions.
5 changes: 5 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ modules/openapi-generator-cli/**/* @jimschubert
modules/openapi-generator-gradle-plugin/**/* @jimschubert
modules/openapi-generator-maven-plugin/**/* @jimschubert

# Martin Delille
/Users/martin/dev/clone/openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQtClientCodegen.java @martindelille
/Users/martin/dev/clone/openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQtAbstractCodegen.java @martindelille
/Users/martin/dev/clone/openapi-generator/modules/openapi-generator/src/main/resources/cpp-qt-client @martindelille
/Users/martin/dev/clone/openapi-generator/samples/client/petstore/cpp-qt @martindelille
1 change: 1 addition & 0 deletions bin/configs/r-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ generatorName: r
outputDir: samples/client/petstore/R
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/r
httpUserAgent: PetstoreAgent
additionalProperties:
packageName: petstore

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ public static Callback getCallback(OpenAPI openAPI, String name) {
* Return the first defined Schema for a RequestBody
*
* @param requestBody request body of the operation
* @return firstSchema
* @return first schema
*/
public static Schema getSchemaFromRequestBody(RequestBody requestBody) {
return getSchemaFromContent(requestBody.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
int match = 0;
ArrayList<String> errorMessages = new ArrayList<>();
TypeAdapter actualAdapter = elementAdapter;

{{#oneOf}}
Expand All @@ -107,6 +108,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
log.log(Level.FINER, "Input data matches schema '{{{.}}}'");
} catch (Exception e) {
// deserialization failed, continue
errorMessages.add(String.format("Deserialization for {{{.}}} failed with `%s`.", e.getMessage()));
log.log(Level.FINER, "Input data does not match schema '{{{.}}}'", e);
}

Expand All @@ -117,7 +119,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return ret;
}

throw new IOException(String.format("Failed deserialization for {{classname}}: %d classes match result, expected 1. JSON: %s", match, jsonObject.toString()));
throw new IOException(String.format("Failed deserialization for {{classname}}: %d classes match result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", match, errorMessages, jsonObject.toString()));
}
}.nullSafe();
}
Expand Down Expand Up @@ -210,17 +212,19 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
public static void validateJsonObject(JsonObject jsonObj) throws IOException {
// validate oneOf schemas one by one
int validCount = 0;
ArrayList<String> errorMessages = new ArrayList<>();
{{#oneOf}}
// validate the json string with {{{.}}}
try {
{{{.}}}.validateJsonObject(jsonObj);
validCount++;
} catch (Exception e) {
errorMessages.add(String.format("Deserialization for {{{.}}} failed with `%s`.", e.getMessage()));
// continue to the next one
}
{{/oneOf}}
if (validCount != 1) {
throw new IOException(String.format("The JSON string is invalid for {{classname}} with oneOf schemas: {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. %d class(es) match the result, expected 1. JSON: %s", validCount, jsonObj.toString()));
throw new IOException(String.format("The JSON string is invalid for {{classname}} with oneOf schemas: {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonObj.toString()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public void testFormParameterHasDefaultValue() {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/fake").getGet().getRequestBody());
Schema requestBodySchema = ModelUtils.getReferencedSchema(openAPI,
ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/fake").getGet().getRequestBody()));
CodegenParameter codegenParameter = codegen.fromFormProperty("enum_form_string", (Schema) requestBodySchema.getProperties().get("enum_form_string"), new HashSet<String>());

Assert.assertEquals(codegenParameter.defaultValue, "-efg");
Expand All @@ -245,6 +246,8 @@ public void testDateTimeFormParameterHasDefaultValue() {
codegen.setOpenAPI(openAPI);

Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/thingy/{date}").getPost().getRequestBody());
// dereference
requestBodySchema = ModelUtils.getReferencedSchema(openAPI, requestBodySchema);
CodegenParameter codegenParameter = codegen.fromFormProperty("visitDate", (Schema) requestBodySchema.getProperties().get("visitDate"),
new HashSet<>());

Expand Down Expand Up @@ -613,7 +616,8 @@ public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
final DefaultCodegen codegen = new DefaultCodegen();

Operation operation = openAPI.getPaths().get("/state").getPost();
Schema schema = ModelUtils.getSchemaFromRequestBody(operation.getRequestBody());
Schema schema = ModelUtils.getReferencedSchema(openAPI,
ModelUtils.getSchemaFromRequestBody(operation.getRequestBody()));
String type = codegen.getSchemaType(schema);

Assert.assertNotNull(type);
Expand Down Expand Up @@ -2344,18 +2348,14 @@ public void testUseOneOfInterfaces() {
cg.preprocessOpenAPI(openAPI);

// assert names of the response/request schema oneOf interfaces are as expected
Assert.assertEquals(
openAPI.getPaths()
.get("/state")
.getPost()
.getRequestBody()
.getContent()
.get("application/json")
.getSchema()
.getExtensions()
.get("x-one-of-name"),
"CreateState"
);
Schema s = ModelUtils.getReferencedSchema(openAPI, openAPI.getPaths()
.get("/state")
.getPost()
.getRequestBody()
.getContent()
.get("application/json")
.getSchema());
Assert.assertEquals(s.getExtensions().get("x-one-of-name"), "CreateStateRequest");
Assert.assertEquals(
openAPI.getPaths()
.get("/state")
Expand All @@ -2372,7 +2372,8 @@ public void testUseOneOfInterfaces() {
// for the array schema, assert that a oneOf interface was added to schema map
Schema items = ((ArraySchema) openAPI.getComponents().getSchemas().get("CustomOneOfArraySchema")).getItems();
Assert.assertEquals(items.get$ref(), "#/components/schemas/CustomOneOfArraySchema_inner");
Schema innerItem = openAPI.getComponents().getSchemas().get("CustomOneOfArraySchema_inner");
//Assert.assertEquals(items.get$ref(), "#/components/schemas/createState_request");
Schema innerItem = ModelUtils.getReferencedSchema(openAPI, openAPI.getComponents().getSchemas().get("CustomOneOfArraySchema_inner"));
Assert.assertEquals(innerItem.getExtensions().get("x-one-of-name"), "CustomOneOfArraySchemaInner");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void resolveInlineModel2EqualInnerModels() {
}

@Test
public void resolveInlineModel2DifferentInnerModelsWIthSameTitle() {
public void resolveInlineModel2DifferentInnerModelsWithSameTitle() {
OpenAPI openapi = new OpenAPI();
openapi.setComponents(new Components());
openapi.getComponents().addSchemas("User", new ObjectSchema()
Expand Down Expand Up @@ -329,7 +329,9 @@ public void resolveInlineRequestBodyWhenNoComponents() {
new InlineModelResolver().flatten(openAPI);

assertNotNull(openAPI.getComponents());
assertNotNull(openAPI.getComponents().getRequestBodies());
// no longer create inline requestBodies as references in the refactored inline model resolver (6.x)
assertNull(openAPI.getComponents().getRequestBodies());
assertNotNull(openAPI.getComponents().getSchemas().get("test1_request"));
}

@Test
Expand Down Expand Up @@ -359,7 +361,8 @@ public void resolveInlineRequestBody() {
.get("/resolve_inline_request_body")
.getPost()
.getRequestBody();
assertNotNull(requestBodyReference.get$ref());
assertEquals("#/components/schemas/resolveInlineRequestBody_request",
requestBodyReference.getContent().get("application/json").getSchema().get$ref());

RequestBody requestBody = ModelUtils.getReferencedRequestBody(openAPI, requestBodyReference);
MediaType mediaType = requestBody.getContent().get("application/json");
Expand Down Expand Up @@ -391,7 +394,8 @@ public void resolveInlineRequestBodyWithTitle() {
new InlineModelResolver().flatten(openAPI);

RequestBody requestBodyReference = openAPI.getPaths().get("/resolve_inline_request_body_with_title").getPost().getRequestBody();
assertEquals("#/components/requestBodies/resolve_inline_request_body_with_title", requestBodyReference.get$ref());
assertEquals("#/components/schemas/resolve_inline_request_body_with_title",
requestBodyReference.getContent().get("application/json").getSchema().get$ref());
}

@Test
Expand Down Expand Up @@ -429,7 +433,7 @@ public void resolveInlineArrayRequestBody() {

ArraySchema requestBody = (ArraySchema) mediaType.getSchema();
assertNotNull(requestBody.getItems().get$ref());
assertEquals("#/components/schemas/inline_object_2", requestBody.getItems().get$ref());
assertEquals("#/components/schemas/resolveInlineArrayRequestBody_request_inner", requestBody.getItems().get$ref());

Schema items = ModelUtils.getReferencedSchema(openAPI, ((ArraySchema) mediaType.getSchema()).getItems());
assertTrue(items.getProperties().get("street") instanceof StringSchema);
Expand Down Expand Up @@ -603,10 +607,11 @@ public void arbitraryObjectRequestBodyProperty() {
.getContent()
.get("application/json");

assertTrue(mediaType.getSchema() instanceof ObjectSchema);

ObjectSchema requestBodySchema = (ObjectSchema) mediaType.getSchema();
assertTrue(requestBodySchema.getProperties().get("arbitrary_object_request_body_property") instanceof ObjectSchema);
assertEquals("#/components/schemas/arbitraryObjectRequestBodyProperty_request", mediaType.getSchema().get$ref());
Schema requestBodySchema = ModelUtils.getReferencedSchema(openAPI, mediaType.getSchema());
assertNotNull(requestBodySchema);
assertEquals(1, requestBodySchema.getProperties().size(), 1);
assertTrue(requestBodySchema.getProperties().get("arbitrary_object_request_body_property") instanceof ObjectSchema);
}

@Test
Expand Down Expand Up @@ -903,9 +908,9 @@ public void arbitraryObjectModelWithArrayInlineWithTitle() {

ObjectSchema items = (ObjectSchema) openAPI.getComponents().getSchemas().get("ArbitraryObjectModelWithArrayInlineWithTitleInner");
assertEquals("ArbitraryObjectModelWithArrayInlineWithTitleInner", items.getTitle());
assertTrue(items.getProperties().get("arbitrary_object_model_with_array_inline_with_title") instanceof ObjectSchema);
assertTrue(items.getProperties().get("arbitrary_object_model_with_array_inline_with_title_property") instanceof ObjectSchema);

ObjectSchema itemsProperty = (ObjectSchema) items.getProperties().get("arbitrary_object_model_with_array_inline_with_title");
ObjectSchema itemsProperty = (ObjectSchema) items.getProperties().get("arbitrary_object_model_with_array_inline_with_title_property");
assertNull(itemsProperty.getProperties());
}

Expand Down Expand Up @@ -941,19 +946,21 @@ public void nullable() {
Schema nullablePropertySchema = ModelUtils.getReferencedSchema(openAPI, nullablePropertyReference);
assertTrue(nullablePropertySchema.getNullable());


Schema nullableRequestBodyReference = (Schema) openAPI
.getPaths()
.get("/nullable_properties")
.getPost()
.getRequestBody()
.getContent()
.get("application/json")
.getSchema()
.getProperties()
.get("nullable_request_body_property");
.getSchema();
//.getProperties()
//.get("nullable_request_body_property");
Schema nullableRequestBodySchema = ModelUtils.getReferencedSchema(openAPI, nullableRequestBodyReference);
assertTrue(nullableRequestBodySchema.getNullable());
//assertEquals(nullableRequestBodySchema, "");
Schema nullableSchema = ModelUtils.getReferencedSchema(openAPI,
((Schema)nullableRequestBodySchema.getProperties().get("nullable_request_body_property")));
assertTrue(nullableSchema.getNullable());
}

@Test
Expand All @@ -970,14 +977,15 @@ public void callbacks() {
.get("{$request.body#/callbackUri}")
.getPost()
.getRequestBody();
assertNotNull(callbackRequestBodyReference.get$ref());
assertNotNull(callbackRequestBodyReference.getContent().get("application/json").getSchema().get$ref());
assertEquals("#/components/schemas/webhookNotify_request", callbackRequestBodyReference.getContent().get("application/json").getSchema().get$ref());

RequestBody resolvedCallbackRequestBody = openAPI
/*RequestBody resolvedCallbackRequestBody = openAPI
.getComponents()
.getRequestBodies()
.get(ModelUtils.getSimpleRef(callbackRequestBodyReference.get$ref()));
.getSchemas()
.get(ModelUtils.getSimpleRef(callbackRequestBodyReference.getContent().get("application/json").getSchema().get$ref()));*/

Schema callbackRequestSchemaReference = resolvedCallbackRequestBody
Schema callbackRequestSchemaReference = callbackRequestBodyReference
.getContent()
.get("application/json")
.getSchema();
Expand All @@ -999,8 +1007,8 @@ public void testInlineSchemaNameMapping() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
InlineModelResolver resolver = new InlineModelResolver();
Map<String, String> inlineSchemaNames = new HashMap<>();
inlineSchemaNames.put("inline_object_2", "SomethingMapped");
inlineSchemaNames.put("inline_object_4", "nothing_new");
inlineSchemaNames.put("resolveInlineArrayRequestBody_request_inner", "SomethingMapped");
inlineSchemaNames.put("arbitraryRequestBodyArrayProperty_request_inner", "nothing_new");
resolver.setInlineSchemaNameMapping(inlineSchemaNames);
resolver.flatten(openAPI);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ public void testDoGenerateRequestBodyRequiredAttribute_3134_Regression() throws
generator.opts(input).generate();

JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/api/ExampleApi.java"))
.assertMethod("exampleApiPost", "InlineObject")
.hasParameter("inlineObject")
.assertMethod("exampleApiPost", "InlineRequest")
.hasParameter("inlineRequest")
.assertParameterAnnotations()
.containsWithNameAndAttributes("RequestBody", ImmutableMap.of("required", "false"));

Expand Down Expand Up @@ -591,7 +591,7 @@ public void testMultipartBoot() throws IOException {

// Check that api validates mixed multipart request
JavaFileAssert.assertThat(files.get("MultipartMixedApi.java"))
.assertMethod("multipartMixed", "MultipartMixedStatus", "MultipartFile", "MultipartMixedMarker")
.assertMethod("multipartMixed", "MultipartMixedStatus", "MultipartFile", "MultipartMixedRequestMarker")
.hasParameter("status").withType("MultipartMixedStatus")
.assertParameterAnnotations()
.containsWithName("Valid")
Expand All @@ -602,7 +602,7 @@ public void testMultipartBoot() throws IOException {
.assertParameterAnnotations()
.containsWithNameAndAttributes("RequestPart", ImmutableMap.of("value", "\"file\"", "required", "true"))
.toParameter().toMethod()
.hasParameter("marker").withType("MultipartMixedMarker")
.hasParameter("marker").withType("MultipartMixedRequestMarker")
.assertParameterAnnotations()
.containsWithNameAndAttributes("RequestParam", ImmutableMap.of("value", "\"marker\"", "required", "false"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public void testComposedSchemasImportTypesIndividually() {
codegen.setOpenAPI(openApi);
PathItem path = openApi.getPaths().get("/pets");
CodegenOperation operation = codegen.fromOperation("/pets", "patch", path.getPatch(), path.getServers());
Assert.assertEquals(operation.imports, Sets.newHashSet("Cat", "Dog"));
// TODO revise the commented test below as oneOf is no longer defined inline
//but instead defined using $ref with the new inline model resolver in 6.x
//Assert.assertEquals(operation.imports, Sets.newHashSet("Cat", "Dog"));
Assert.assertEquals(operation.imports, Sets.newHashSet("InlineRequest"));

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void testGetAllUsedSchemas() {
"SomeObj18",
"Common18",
"SomeObj18_allOf",
"inline_request",
"Obj19ByAge",
"Obj19ByType",
"SomeObj20",
Expand All @@ -78,7 +79,7 @@ public void testGetAllUsedSchemas() {
"AChild30",
"BChild30"
);
Assert.assertEquals(allUsedSchemas.size(), expectedAllUsedSchemas.size());
Assert.assertEquals(allUsedSchemas, expectedAllUsedSchemas);
Assert.assertTrue(allUsedSchemas.containsAll(expectedAllUsedSchemas));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ components:
oneOf:
- $ref: '#/components/schemas/ObjA'
- $ref: '#/components/schemas/ObjB'
- $ref: '#/components/schemas/ObjC'
discriminator:
propertyName: realtype
mapping:
a-type: '#/components/schemas/ObjA'
b-type: '#/components/schemas/ObjB'
c-type: '#/components/schemas/ObjC'
ObjA:
type: object
properties:
Expand All @@ -78,4 +80,11 @@ components:
type: string
code:
type: integer
format: int32
format: int32
ObjC:
type: object
properties:
realtype:
type: string
state:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ components:
title: ArbitraryObjectModelWithArrayInlineWithTitleInner
type: object
properties:
arbitrary_object_model_with_array_inline_with_title:
arbitrary_object_model_with_array_inline_with_title_property:
type: object
EmptyExampleOnStringTypeModels:
type: string
Expand Down

0 comments on commit e4cd1a5

Please sign in to comment.