blob: e6b5c99f76abbee3a09b5964a0d0395a3b7372e3 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.servicecomb.toolkit.oasv.web.api.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.toolkit.oasv.compliance.ComplianceCheckParser;
import org.apache.servicecomb.toolkit.oasv.validation.api.OasSpecValidator;
import org.apache.servicecomb.toolkit.oasv.validation.api.OasValidationContext;
import org.apache.servicecomb.toolkit.oasv.validation.api.OasViolation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
@RestController
@RequestMapping("/api/compliance")
public class ComplianceController {
@Autowired
private OasSpecValidator oasSpecValidator;
@PostMapping(consumes = MimeTypeUtils.TEXT_PLAIN_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
public Map<String, Object> validateOpenAPI(@RequestBody String yaml) {
ImportError importError = doValidate(yaml);
Map<String, Object> json = new HashMap<>();
json.put("acknowleged", true);
json.put("data", importError);
return json;
}
private ImportError doValidate(String yaml) {
ImportError importError = new ImportError();
importError.addParseErrors(ComplianceCheckParser.checkSyntax(yaml));
if (importError.isNotEmpty()) {
return importError;
}
SwaggerParseResult parseResult = ComplianceCheckParser.parseYaml(yaml);
if (CollectionUtils.isNotEmpty(parseResult.getMessages())) {
throw new RuntimeException(StringUtils.join(parseResult.getMessages(), ","));
}
OpenAPI openAPI = loadByYaml(yaml);
List<OasViolation> violations = oasSpecValidator.validate(createContext(openAPI), openAPI);
if (CollectionUtils.isNotEmpty(violations)) {
importError.addViolations(violations);
}
return importError;
}
private OpenAPI loadByYaml(String yaml) {
SwaggerParseResult parseResult = ComplianceCheckParser.parseYaml(yaml);
if (CollectionUtils.isNotEmpty(parseResult.getMessages())) {
throw new RuntimeException(StringUtils.join(parseResult.getMessages(), ","));
}
return parseResult.getOpenAPI();
}
private OasValidationContext createContext(OpenAPI openAPI) {
OasValidationContext oasValidationContext = new OasValidationContext(openAPI);
initContext(oasValidationContext);
return oasValidationContext;
}
private void initContext(OasValidationContext context) {
}
}