Skip to content

Commit 59d5585

Browse files
authored
fix: generate model fields in the same order as in Java (#11579)
* Generate model fields in the same order as in Java Fixes #10133 * Remove unused import
1 parent 3c7dd42 commit 59d5585

23 files changed

Lines changed: 224 additions & 228 deletions

‎fusion-endpoint/src/main/java/com/vaadin/fusion/generator/SchemaGenerator.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import java.util.Arrays;
2121
import java.util.Collections;
2222
import java.util.HashMap;
23+
import java.util.LinkedHashMap;
2324
import java.util.LinkedHashSet;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Optional;
2728
import java.util.Set;
28-
import java.util.TreeMap;
2929
import java.util.stream.Collectors;
3030

3131
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -115,13 +115,13 @@ Schema createSingleSchemaFromResolvedType(GeneratorType type) {
115115
Schema schema = new ObjectSchema()
116116
.name(resolvedReferenceType.getQualifiedName());
117117
Map<String, Boolean> fieldsOptionalMap = getFieldsAndOptionalMap(type);
118-
Set<ResolvedFieldDeclaration> serializableFields = resolvedReferenceType
119-
.getDeclaredFields().stream()
118+
List<ResolvedFieldDeclaration> serializableFields = resolvedReferenceType
119+
.getTypeDeclaration().getDeclaredFields().stream()
120120
.filter(resolvedFieldDeclaration -> fieldsOptionalMap
121121
.containsKey(resolvedFieldDeclaration.getName()))
122-
.collect(Collectors.toSet());
122+
.collect(Collectors.toList());
123123
// Make sure the order is consistent in properties map
124-
schema.setProperties(new TreeMap<>());
124+
schema.setProperties(new LinkedHashMap<>());
125125
for (ResolvedFieldDeclaration resolvedFieldDeclaration : serializableFields) {
126126
String name = resolvedFieldDeclaration.getName();
127127
Schema subtype = openApiObjectGenerator
@@ -182,7 +182,7 @@ private Map<String, Boolean> getFieldsAndOptionalMap(GeneratorType type) {
182182

183183
private Map<String, Schema> getPropertiesFromClassDeclaration(
184184
TypeDeclaration<?> typeDeclaration) {
185-
Map<String, Schema> properties = new TreeMap<>();
185+
Map<String, Schema> properties = new LinkedHashMap<>();
186186
for (FieldDeclaration field : typeDeclaration.getFields()) {
187187
if (field.isTransient() || field.isStatic()
188188
|| field.isAnnotationPresent(JsonIgnore.class)) {

‎fusion-endpoint/src/test/resources/com/vaadin/fusion/generator/endpoints/collectionendpoint/expected-model-collectionendpoint.CollectionEndpoint.Collection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
44
*/
55
export default interface Collection {
6-
readonly author?: string;
76
readonly collectionName?: string;
87
readonly type?: string;
8+
readonly author?: string;
99
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* This module is generated from com.fasterxml.jackson.core.Version.
3-
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4-
*/
2+
* This module is generated from com.fasterxml.jackson.core.Version.
3+
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4+
*/
55
export default interface Version {
6-
readonly artifactId?: string;
7-
readonly groupId?: string;
86
readonly majorVersion: number;
97
readonly minorVersion: number;
108
readonly patchLevel: number;
9+
readonly groupId?: string;
10+
readonly artifactId?: string;
1111
readonly snapshotInfo?: string;
12-
}
12+
}

‎fusion-endpoint/src/test/resources/com/vaadin/fusion/generator/endpoints/inheritedmodel/expected-model-inheritedmodel.InheritedModelEndpoint.ChildModel.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import ArraySchema from '../../../../../../../io/swagger/v3/oas/models/media/Arr
77
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
88
*/
99
export default interface ChildModel extends ParentModel {
10-
readonly abc?: ReadonlyArray<Readonly<Record<string, Version | undefined>> | undefined>;
11-
readonly def?: ReadonlyArray<Readonly<Record<string, Readonly<Record<string, Version | undefined>> | undefined>> | undefined>;
1210
readonly name?: string;
1311
readonly testObject?: ArraySchema;
12+
readonly abc?: ReadonlyArray<Readonly<Record<string, Version | undefined>> | undefined>;
13+
readonly def?: ReadonlyArray<Readonly<Record<string, Readonly<Record<string, Version | undefined>> | undefined>> | undefined>;
1414
}

‎fusion-endpoint/src/test/resources/com/vaadin/fusion/generator/endpoints/inheritedmodel/expected-model-media.ArraySchema.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import Schema from './Schema';
55
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
66
*/
77
export default interface ArraySchema extends Schema {
8-
readonly items?: Schema;
98
readonly type?: string;
10-
}
9+
readonly items?: Schema;
10+
}

‎fusion-endpoint/src/test/resources/com/vaadin/fusion/generator/endpoints/inheritedmodel/expected-model-media.Discriminator.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
44
*/
55
export default interface Discriminator {
6-
readonly mapping?: Readonly<Record<string, string | undefined>>;
76
readonly propertyName?: string;
7+
readonly mapping?: Readonly<Record<string, string | undefined>>;
88
}
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
import ExternalDocumentation from '../ExternalDocumentation';
22
import Discriminator from './Discriminator';
33
import XML from './XML';
4-
54
/**
6-
* This module is generated from io.swagger.v3.oas.models.media.Schema.
7-
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
8-
*/
5+
* This module is generated from io.swagger.v3.oas.models.media.Schema.
6+
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
7+
*/
98
export default interface Schema {
10-
readonly ref?: string;
119
readonly _default?: any;
12-
readonly _enum?: ReadonlyArray<any | undefined>;
13-
readonly additionalProperties?: any;
14-
readonly deprecated?: boolean;
15-
readonly description?: string;
16-
readonly discriminator?: Discriminator;
17-
readonly example?: any;
10+
readonly name?: string;
11+
readonly title?: string;
12+
readonly multipleOf?: number;
13+
readonly maximum?: number;
1814
readonly exclusiveMaximum?: boolean;
15+
readonly minimum?: number;
1916
readonly exclusiveMinimum?: boolean;
20-
readonly extensions?: Readonly<Record<string, any | undefined>>;
21-
readonly externalDocs?: ExternalDocumentation;
22-
readonly format?: string;
23-
readonly maxItems?: number;
2417
readonly maxLength?: number;
25-
readonly maxProperties?: number;
26-
readonly maximum?: number;
27-
readonly minItems?: number;
2818
readonly minLength?: number;
19+
readonly pattern?: string;
20+
readonly maxItems?: number;
21+
readonly minItems?: number;
22+
readonly uniqueItems?: boolean;
23+
readonly maxProperties?: number;
2924
readonly minProperties?: number;
30-
readonly minimum?: number;
31-
readonly multipleOf?: number;
32-
readonly name?: string;
25+
readonly required?: ReadonlyArray<string | undefined>;
26+
readonly type?: string;
3327
readonly not?: Schema;
34-
readonly nullable?: boolean;
35-
readonly pattern?: string;
3628
readonly properties?: Readonly<Record<string, Schema | undefined>>;
29+
readonly additionalProperties?: any;
30+
readonly description?: string;
31+
readonly format?: string;
32+
readonly ref?: string;
33+
readonly nullable?: boolean;
3734
readonly readOnly?: boolean;
38-
readonly required?: ReadonlyArray<string | undefined>;
39-
readonly title?: string;
40-
readonly type?: string;
41-
readonly uniqueItems?: boolean;
4235
readonly writeOnly?: boolean;
36+
readonly example?: any;
37+
readonly externalDocs?: ExternalDocumentation;
38+
readonly deprecated?: boolean;
4339
readonly xml?: XML;
44-
}
40+
readonly extensions?: Readonly<Record<string, any | undefined>>;
41+
readonly _enum?: ReadonlyArray<any | undefined>;
42+
readonly discriminator?: Discriminator;
43+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* This module is generated from io.swagger.v3.oas.models.media.XML.
3-
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4-
*/
2+
* This module is generated from io.swagger.v3.oas.models.media.XML.
3+
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4+
*/
55
export default interface XML {
6-
readonly attribute?: boolean;
7-
readonly extensions?: Readonly<Record<string, any | undefined>>;
86
readonly name?: string;
97
readonly namespace?: string;
108
readonly prefix?: string;
9+
readonly attribute?: boolean;
1110
readonly wrapped?: boolean;
12-
}
11+
readonly extensions?: Readonly<Record<string, any | undefined>>;
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* This module is generated from io.swagger.v3.oas.models.ExternalDocumentation.
3-
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4-
*/
2+
* This module is generated from io.swagger.v3.oas.models.ExternalDocumentation.
3+
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4+
*/
55
export default interface ExternalDocumentation {
66
readonly description?: string;
7-
readonly extensions?: Readonly<Record<string, any | undefined>>;
87
readonly url?: string;
9-
}
8+
readonly extensions?: Readonly<Record<string, any | undefined>>;
9+
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/**
2-
* This module is generated from com.fasterxml.jackson.core.Version.
3-
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4-
*/
5-
2+
* This module is generated from com.fasterxml.jackson.core.Version.
3+
* All changes to this file are overridden. Please consider to make changes in the corresponding Java file if necessary.
4+
*/
65
export default interface Version {
7-
readonly artifactId?: string;
8-
readonly groupId?: string;
96
readonly majorVersion: number;
107
readonly minorVersion: number;
118
readonly patchLevel: number;
9+
readonly groupId?: string;
10+
readonly artifactId?: string;
1211
readonly snapshotInfo?: string;
13-
}
12+
}

0 commit comments

Comments
 (0)