|
14 | 14 | import io.swagger.v3.oas.models.security.SecurityRequirement;
|
15 | 15 | import io.swagger.v3.oas.models.security.SecurityScheme;
|
16 | 16 | import io.swagger.v3.oas.models.tags.Tag;
|
| 17 | +import io.swagger.v3.parser.OpenAPIV3Parser; |
17 | 18 | import io.swagger.v3.parser.converter.SwaggerConverter;
|
| 19 | +import io.swagger.v3.parser.core.extensions.SwaggerParserExtension; |
18 | 20 | import io.swagger.v3.parser.core.models.AuthorizationValue;
|
19 | 21 | import io.swagger.v3.parser.core.models.ParseOptions;
|
20 | 22 | import io.swagger.v3.parser.core.models.SwaggerParseResult;
|
| 23 | + |
| 24 | +import org.testng.Assert; |
21 | 25 | import org.testng.annotations.Test;
|
22 | 26 |
|
23 | 27 | import java.io.IOException;
|
@@ -923,4 +927,42 @@ public void testConvertFormDataAsObjectSchema() throws Exception {
|
923 | 927 | assertEquals(companiesSchema.getClass(), ObjectSchema.class);
|
924 | 928 |
|
925 | 929 | }
|
| 930 | + |
| 931 | + /** |
| 932 | + * A clone (almost) of {@link OpenAPIV3Parser#readContents(String, List, ParseOptions)}. |
| 933 | + */ |
| 934 | + private OpenAPI read(String location, List<AuthorizationValue> auths, ParseOptions resolve, final List<SwaggerParserExtension> parserExtensions) { |
| 935 | + if (location == null) { |
| 936 | + return null; |
| 937 | + } |
| 938 | + SwaggerParseResult parsed; |
| 939 | + for (SwaggerParserExtension extension : parserExtensions) { |
| 940 | + parsed = extension.readLocation(location, auths, resolve); |
| 941 | + if (parsed.getMessages() != null) { |
| 942 | + for (String message : parsed.getMessages()) { |
| 943 | + // LOGGER.info("{}: {}", extension, message); |
| 944 | + } |
| 945 | + } |
| 946 | + final OpenAPI result = parsed.getOpenAPI(); |
| 947 | + if (result != null) { |
| 948 | + return result; |
| 949 | + } |
| 950 | + } |
| 951 | + return null; |
| 952 | + } |
| 953 | + |
| 954 | + /** |
| 955 | + * Tests an NPE when bother the old and new parser are on the classpath. The only way to test this is cloning the |
| 956 | + * {@link OpenAPIV3Parser#readContents(String, List, ParseOptions)} to see the NPE when we pass in the extentions. Instead of, for example, mocking the call |
| 957 | + * to {@link io.swagger.v3.parser.OpenAPIV3Parser.getExtensions()}. |
| 958 | + */ |
| 959 | + @Test |
| 960 | + public void testIssue2046() { |
| 961 | + ParseOptions options = new ParseOptions(); |
| 962 | + options.setResolve(true); |
| 963 | + // This should not throw an NPE: |
| 964 | + final OpenAPI openAPI = read("I/do/not/exist/on/the/file/system/I/really/do/not.yaml", null, options, Arrays.asList(new SwaggerConverter())); |
| 965 | + Assert.assertNull(openAPI); |
| 966 | + } |
| 967 | + |
926 | 968 | }
|
0 commit comments