Skip to content

Commit 61b96d7

Browse files
committed
Added swagger-core 2.1.6 deterministic order support
1 parent 89c1fd0 commit 61b96d7

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import com.fasterxml.jackson.annotation.JsonView;
4646
import com.fasterxml.jackson.core.JsonProcessingException;
47+
import com.fasterxml.jackson.databind.MapperFeature;
4748
import com.fasterxml.jackson.databind.ObjectMapper;
4849
import com.fasterxml.jackson.databind.SerializationFeature;
4950
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
@@ -61,13 +62,16 @@
6162
import io.swagger.v3.oas.models.PathItem;
6263
import io.swagger.v3.oas.models.PathItem.HttpMethod;
6364
import io.swagger.v3.oas.models.Paths;
65+
import io.swagger.v3.oas.models.media.Schema;
6466
import io.swagger.v3.oas.models.media.StringSchema;
6567
import io.swagger.v3.oas.models.parameters.Parameter;
6668
import io.swagger.v3.oas.models.responses.ApiResponses;
6769
import org.apache.commons.lang3.ArrayUtils;
6870
import org.apache.commons.lang3.StringUtils;
6971
import org.slf4j.Logger;
7072
import org.slf4j.LoggerFactory;
73+
import org.springdoc.api.mixins.SortedOpenAPIMixin;
74+
import org.springdoc.api.mixins.SortedSchemaMixin;
7175
import org.springdoc.core.AbstractRequestService;
7276
import org.springdoc.core.ActuatorProvider;
7377
import org.springdoc.core.GenericParameterService;
@@ -1025,7 +1029,7 @@ protected String writeYamlValue(OpenAPI openAPI) throws JsonProcessingException
10251029
String result;
10261030
ObjectMapper objectMapper = Yaml.mapper();
10271031
if (springDocConfigProperties.isWriterWithOrderByKeys())
1028-
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
1032+
sortOutput(objectMapper);
10291033
YAMLFactory factory = (YAMLFactory) objectMapper.getFactory();
10301034
factory.configure(Feature.USE_NATIVE_TYPE_ID, false);
10311035
if (!springDocConfigProperties.isWriterWithDefaultPrettyPrinter())
@@ -1090,7 +1094,7 @@ protected String writeJsonValue(OpenAPI openAPI) throws JsonProcessingException
10901094
String result;
10911095
ObjectMapper objectMapper = Json.mapper();
10921096
if (springDocConfigProperties.isWriterWithOrderByKeys())
1093-
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
1097+
sortOutput(objectMapper);
10941098
if (!springDocConfigProperties.isWriterWithDefaultPrettyPrinter())
10951099
result = objectMapper.writeValueAsString(openAPI);
10961100
else
@@ -1158,4 +1162,11 @@ enum ConditionType {
11581162
*/
11591163
HEADERS
11601164
}
1165+
1166+
private void sortOutput(ObjectMapper objectMapper) {
1167+
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
1168+
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
1169+
objectMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
1170+
objectMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
1171+
}
11611172
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2020 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.api.mixins;
22+
23+
import java.util.Map;
24+
25+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
26+
import com.fasterxml.jackson.annotation.JsonAnySetter;
27+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
28+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
29+
import io.swagger.v3.core.jackson.PathsSerializer;
30+
import io.swagger.v3.oas.models.Paths;
31+
32+
/**
33+
* @author bnasslashen
34+
*/
35+
@JsonPropertyOrder(value = {"openapi", "info", "externalDocs", "servers", "security", "tags", "paths", "components"}, alphabetic = true)
36+
public abstract class SortedOpenAPIMixin {
37+
38+
@JsonAnyGetter
39+
@JsonPropertyOrder(alphabetic = true)
40+
public abstract Map<String, Object> getExtensions();
41+
42+
@JsonAnySetter
43+
public abstract void addExtension(String name, Object value);
44+
45+
@JsonSerialize(using = PathsSerializer.class)
46+
public abstract Paths getPaths();
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2020 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.api.mixins;
22+
23+
import java.util.Map;
24+
25+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
26+
import com.fasterxml.jackson.annotation.JsonAnySetter;
27+
import com.fasterxml.jackson.annotation.JsonIgnore;
28+
import com.fasterxml.jackson.annotation.JsonInclude;
29+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
30+
31+
/**
32+
* @author bnasslashen
33+
*/
34+
@JsonPropertyOrder(value = {"type", "format"}, alphabetic = true)
35+
public abstract class SortedSchemaMixin {
36+
37+
@JsonAnyGetter
38+
@JsonPropertyOrder(alphabetic = true)
39+
public abstract Map<String, Object> getExtensions();
40+
41+
@JsonAnySetter
42+
public abstract void addExtension(String name, Object value);
43+
44+
@JsonIgnore
45+
public abstract boolean getExampleSetFlag();
46+
47+
@JsonInclude(JsonInclude.Include.CUSTOM)
48+
public abstract Object getExample();
49+
50+
}

0 commit comments

Comments
 (0)