17
17
18
18
package org .openapitools .codegen ;
19
19
20
+ import com .fasterxml .jackson .core .JsonProcessingException ;
21
+ import com .fasterxml .jackson .core .util .DefaultPrettyPrinter ;
22
+ import com .fasterxml .jackson .databind .MapperFeature ;
23
+ import com .fasterxml .jackson .databind .ObjectMapper ;
20
24
import io .swagger .v3 .core .util .Json ;
21
25
import io .swagger .v3 .oas .models .*;
22
26
import io .swagger .v3 .oas .models .callbacks .Callback ;
25
29
import io .swagger .v3 .oas .models .parameters .RequestBody ;
26
30
import io .swagger .v3 .oas .models .responses .ApiResponse ;
27
31
import io .swagger .v3 .oas .models .responses .ApiResponses ;
32
+ import org .apache .commons .lang3 .StringUtils ;
28
33
import org .openapitools .codegen .utils .ModelUtils ;
29
34
import org .slf4j .Logger ;
30
35
import org .slf4j .LoggerFactory ;
@@ -36,6 +41,17 @@ public class InlineModelResolver {
36
41
private OpenAPI openapi ;
37
42
private Map <String , Schema > addedModels = new HashMap <String , Schema >();
38
43
private Map <String , String > generatedSignature = new HashMap <String , String >();
44
+
45
+ // structure mapper sorts properties alphabetically on write to ensure models are
46
+ // serialized consistently for lookup of existing models
47
+ private static ObjectMapper structureMapper ;
48
+
49
+ static {
50
+ structureMapper = Json .mapper ().copy ();
51
+ structureMapper .configure (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY , true );
52
+ structureMapper .writer (new DefaultPrettyPrinter ());
53
+ }
54
+
39
55
static Logger LOGGER = LoggerFactory .getLogger (InlineModelResolver .class );
40
56
41
57
void flatten (OpenAPI openapi ) {
@@ -488,15 +504,25 @@ private String resolveModelName(String title, String key) {
488
504
}
489
505
490
506
private String matchGenerated (Schema model ) {
491
- String json = Json .pretty (model );
492
- if (generatedSignature .containsKey (json )) {
493
- return generatedSignature .get (json );
507
+ try {
508
+ String json = structureMapper .writeValueAsString (model );
509
+ if (generatedSignature .containsKey (json )) {
510
+ return generatedSignature .get (json );
511
+ }
512
+ } catch (JsonProcessingException e ) {
513
+ e .printStackTrace ();
494
514
}
515
+
495
516
return null ;
496
517
}
497
518
498
519
private void addGenerated (String name , Schema model ) {
499
- generatedSignature .put (Json .pretty (model ), name );
520
+ try {
521
+ String json = structureMapper .writeValueAsString (model );
522
+ generatedSignature .put (json , name );
523
+ } catch (JsonProcessingException e ) {
524
+ e .printStackTrace ();
525
+ }
500
526
}
501
527
502
528
/**
0 commit comments