Fix NullPointerException in getUnusedSchemas(OpenAPI) (#10)

fix #9
This commit is contained in:
Jérémie Bresson 2018-05-13 09:22:01 +02:00 committed by William Cheng
parent 23ad9f3937
commit 9fa9b115e3
3 changed files with 51 additions and 7 deletions

View File

@ -50,7 +50,6 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -89,14 +88,9 @@ public class ModelUtils {
public static List<String> getUnusedSchemas(OpenAPI openAPI) {
List<String> unusedSchemas = new ArrayList<String>();
// no model defined
if (openAPI.getComponents().getSchemas() == null) {
openAPI.getComponents().setSchemas(new HashMap<String, Schema>());
}
// operations
Map<String, PathItem> paths = openAPI.getPaths();
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
Map<String, Schema> schemas = getSchemas(openAPI);
if (paths != null) {
for (String pathname : paths.keySet()) {

View File

@ -0,0 +1,37 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.utils;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
public class ModelUtilsTest {
@Test
public void testEnsureNoDuplicateProduces() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/ping.yaml", null, new ParseOptions()).getOpenAPI();
List<String> unusedSchemas = ModelUtils.getUnusedSchemas(openAPI);
Assert.assertEquals(unusedSchemas.size(), 0);
}
}

View File

@ -0,0 +1,13 @@
openapi: 3.0.1
info:
title: ping test
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/ping:
get:
operationId: pingGet
responses:
'201':
description: OK