Skip to content

Commit 3aba427

Browse files
authored
clone Schema using AnnotationUtils.clone with WA (to clone schemas wi… (OpenAPITools#18867)
* clone Schema using AnnotationUtils.clone with WA (to clone schemas with example field set) * changes after scripts run
1 parent 6ae8a8f commit 3aba427

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2862,7 +2862,9 @@ private void mergeProperties(Map<String, Schema> existingProperties, Map<String,
28622862
if (null != existingProperties && null != newProperties) {
28632863
Schema existingType = existingProperties.get("type");
28642864
Schema newType = newProperties.get("type");
2865-
newProperties.forEach((key, value) -> existingProperties.put(key, ModelUtils.cloneSchema(value)));
2865+
newProperties.forEach((key, value) ->
2866+
existingProperties.put(key, ModelUtils.cloneSchema(value, specVersionGreaterThanOrEqualTo310(openAPI)))
2867+
);
28662868
if (null != existingType && null != newType && null != newType.getEnum() && !newType.getEnum().isEmpty()) {
28672869
for (Object e : newType.getEnum()) {
28682870
// ensure all interface enum types are added to schema

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
package org.openapitools.codegen.utils;
1919

20-
import com.fasterxml.jackson.core.JsonProcessingException;
2120
import com.fasterxml.jackson.databind.JsonNode;
2221
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
22+
import io.swagger.v3.core.util.AnnotationsUtils;
2423
import io.swagger.v3.oas.models.OpenAPI;
2524
import io.swagger.v3.oas.models.Operation;
2625
import io.swagger.v3.oas.models.PathItem;
@@ -64,6 +63,8 @@ public class ModelUtils {
6463

6564
private static final String URI_FORMAT = "uri";
6665

66+
private static final Set<String> OPENAPI_TYPES = Set.of("array", "integer", "number", "boolean", "string", "object");
67+
6768
private static final String generateAliasAsModelKey = "generateAliasAsModel";
6869

6970
// A vendor extension to track the value of the 'swagger' field in a 2.0 doc, if applicable.
@@ -76,16 +77,10 @@ public class ModelUtils {
7677

7778
private static final ObjectMapper JSON_MAPPER;
7879
private static final ObjectMapper YAML_MAPPER;
79-
private static final ObjectMapper TYPED_JSON_MAPPER = new ObjectMapper();
8080

8181
static {
8282
JSON_MAPPER = ObjectMapperFactory.createJson();
8383
YAML_MAPPER = ObjectMapperFactory.createYaml();
84-
85-
BasicPolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
86-
.allowIfSubType(Object.class)
87-
.build();
88-
TYPED_JSON_MAPPER.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.EVERYTHING);
8984
}
9085

9186
public static boolean isDisallowAdditionalPropertiesIfNotPresent() {
@@ -2186,13 +2181,19 @@ public static boolean isParent(Schema schema) {
21862181
return false;
21872182
}
21882183

2189-
public static Schema cloneSchema(Schema schema) {
2190-
try {
2191-
String json = TYPED_JSON_MAPPER.writeValueAsString(schema);
2192-
return TYPED_JSON_MAPPER.readValue(json, schema.getClass());
2193-
} catch (JsonProcessingException ex) {
2194-
LOGGER.error("Can't clone schema {}", schema, ex);
2195-
return schema;
2184+
public static Schema cloneSchema(Schema schema, boolean openapi31) {
2185+
if (openapi31) {
2186+
return AnnotationsUtils.clone(schema, openapi31);
2187+
} else {
2188+
// AnnotationsUtils.clone doesn't support custom schema types for OpenAPI < 3.1
2189+
String schemaType = schema.getType();
2190+
if (schemaType != null && !OPENAPI_TYPES.contains(schemaType)) {
2191+
schema.setType(null);
2192+
}
2193+
Schema result = AnnotationsUtils.clone(schema, openapi31);
2194+
schema.setType(schemaType);
2195+
result.setType(schemaType);
2196+
return result;
21962197
}
21972198
}
21982199

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,51 +399,66 @@ public void testCloneNumberSchema() {
399399
.name("test-schema")
400400
.minimum(new BigDecimal(100));
401401

402-
Schema deepCopy = ModelUtils.cloneSchema(schema);
402+
Schema deepCopy = ModelUtils.cloneSchema(schema, false);
403403

404-
Assert.assertEquals(schema, deepCopy);
404+
Assert.assertEquals(deepCopy, schema);
405+
Assert.assertNotSame(deepCopy, schema);
405406
}
406407

407408
@Test
408409
public void testCloneCustomSchema() {
409-
Schema schema = new Schema().type("money");
410+
Schema schema = new ObjectSchema().type("money");
410411

411-
Schema deepCopy = ModelUtils.cloneSchema(schema);
412+
Schema deepCopy = ModelUtils.cloneSchema(schema, false);
412413

413-
Assert.assertEquals(schema, deepCopy);
414+
Assert.assertEquals(deepCopy, schema);
415+
Assert.assertNotSame(deepCopy, schema);
414416
}
415417

416418
@Test
417419
public void testCloneComposedSchema() {
418-
Schema base1 = new Schema()
420+
Schema base1 = new ObjectSchema()
419421
.name("Base1")
420422
.addProperty("foo", new StringSchema());
421-
Schema base2 = new Schema()
423+
Schema base2 = new ObjectSchema()
422424
.name("Base2")
423425
.addProperty("bar", new StringSchema());
424426
Schema composedSchema = new ComposedSchema()
425427
.name("Composed")
426428
.allOf(List.of(base1, base2))
427429
.addProperty("baz", new StringSchema());
428430

429-
var deepCopy = ModelUtils.cloneSchema(composedSchema);
431+
Schema deepCopy = ModelUtils.cloneSchema(composedSchema, false);
430432

431-
Assert.assertEquals(composedSchema, deepCopy);
433+
Assert.assertEquals(deepCopy, composedSchema);
434+
Assert.assertNotSame(deepCopy, composedSchema);
432435
}
433436

434437
@Test
435438
public void testCloneArrayOfEnumsSchema() {
436-
Schema arraySchema = new Schema()
439+
Schema schema = new ArraySchema()
437440
.name("ArrayType")
438441
.type("array")
439-
.items(new Schema()
442+
.items(new StringSchema()
440443
.type("string")
441444
._enum(List.of("SUCCESS", "FAILURE", "SKIPPED"))
442445
)
443446
._default(List.of("SUCCESS", "FAILURE"));
444447

445-
var deepCopy = ModelUtils.cloneSchema(arraySchema);
448+
Schema deepCopy = ModelUtils.cloneSchema(schema, false);
446449

447-
Assert.assertEquals(arraySchema, deepCopy);
450+
Assert.assertEquals(deepCopy, schema);
451+
Assert.assertNotSame(deepCopy, schema);
452+
}
453+
454+
@Test
455+
public void testCloneDateTimeSchemaWithExample() {
456+
Schema schema = new DateTimeSchema()
457+
.example("2020-02-02T20:20:20.000222Z");
458+
459+
Schema deepCopy = ModelUtils.cloneSchema(schema, false);
460+
461+
Assert.assertEquals(deepCopy, schema);
462+
Assert.assertNotSame(deepCopy, schema);
448463
}
449464
}

samples/client/echo_api/r/R/data_query.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ DataQuery <- R6::R6Class(
3030
#' Initialize a new DataQuery class.
3131
#'
3232
#' @param id Query
33-
#' @param outcomes outcomes. Default to ["SUCCESS","FAILURE"].
33+
#' @param outcomes outcomes. Default to [SUCCESS, FAILURE].
3434
#' @param suffix test suffix
3535
#' @param text Some text containing white spaces
3636
#' @param date A date
3737
#' @param ... Other optional arguments.
3838
#' @export
39-
initialize = function(`id` = NULL, `outcomes` = ["SUCCESS","FAILURE"], `suffix` = NULL, `text` = NULL, `date` = NULL, ...) {
39+
initialize = function(`id` = NULL, `outcomes` = [SUCCESS, FAILURE], `suffix` = NULL, `text` = NULL, `date` = NULL, ...) {
4040
if (!is.null(`id`)) {
4141
if (!(is.numeric(`id`) && length(`id`) == 1)) {
4242
stop(paste("Error! Invalid data for `id`. Must be an integer:", `id`))

samples/client/echo_api/r/docs/DataQuery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
77
**id** | **integer** | Query | [optional]
8-
**outcomes** | **array[character]** | | [optional] [default to [&quot;SUCCESS&quot;,&quot;FAILURE&quot;]] [Enum: ]
8+
**outcomes** | **array[character]** | | [optional] [default to [SUCCESS, FAILURE]] [Enum: ]
99
**suffix** | **character** | test suffix | [optional]
1010
**text** | **character** | Some text containing white spaces | [optional]
1111
**date** | **character** | A date | [optional]

0 commit comments

Comments
 (0)