IT TIP

Swagger UI의 파일 문제에서 읽을 수 없음

itqueen 2020. 11. 20. 17:33
반응형

Swagger UI의 파일 문제에서 읽을 수 없음


내 응용 프로그램에 swagger UI를 통합했습니다.

swagger UI를 보려고하면 API 문서가 멋지게 표시되지만 얼마 후 버튼에 오류 아이콘이 표시됩니다.

오류 메시지는 다음과 같습니다.

[{ "level": "error", "message": " http : // MYIP / swagger / docs / v1 "} 파일에서 읽을 수 없습니다 . ]

원인이 무엇인지 잘 모르겠습니다. 새로 고치면 작동하고 몇 초 후에 오류가 표시됩니다.


" http : // MYIP / swagger / docs / v1 "에 공개적으로 액세스 할 수없는 것 같습니다.

기본적으로 swagger ui는 online.swagger.io라는 온라인 유효성 검사기를 사용합니다. swagger URL에 액세스 할 수없는 경우 해당 오류 메시지가 표시됩니다.

가능한 해결책:

  1. 유효성 검사 비활성화 :

    config.EnableSwagger().EnableSwaggerUi(c => c.DisableValidator());

  2. 사이트를 공개적으로 액세스 할 수 있도록 설정

  3. 유효성 검사기를 로컬로 호스팅합니다.

유효성 검사기는 https://github.com/swagger-api/validator-badge#running-locally 에서 얻을 수 있습니다.

또한 swaggerui에게 유효성 검사기의 위치를 ​​알려야합니다.

config.EnableSwagger().EnableSwaggerUi(c => c.SetValidatorUrl(<validator_url>));


허용되는 답변을 보완하기 위해 ... SwaggerConfig.cs에서 한 줄의 주석 처리를 제거했습니다. 나는 유효성 검사기를 비활성화하여 메인 스웨거 페이지의 빨간색 오류 만 제거하고 싶었습니다.

// By default, swagger-ui will validate specs against swagger.io's online validator and display the result
// in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the
// feature entirely.
//c.SetValidatorUrl("http://localhost/validator");
c.DisableValidator();

당신이 파일을 사용하는 경우 swagger-uiGitHub의의의 repo에, 당신은 당신의에서 스키마 유효성 검사를 사용하지 않도록 설정할 수 있습니다 index.html설정하여 파일 validatorUrlnull그것에 :

window.onload = function() {

  // Build a system
  const ui = SwaggerUIBundle({
    url: "/docs/open_api.json",
    dom_id: '#swagger-ui',

    validatorUrl : null,   # <----- Add this line

    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

L5-Swagger 와 함께 PHP Laravel 프레임 워크 를 사용하는 경우 주석을 제거하십시오.

'validatorUrl' => null,

구성 파일에서 /config/l5-swagger.php


설정 this.model.validatorUrl = null;dist/swagger-ui.js나를 위해 일했습니다 ..

// Default validator
if(window.location.protocol === 'https:') {
  //this.model.validatorUrl = 'https://online.swagger.io/validator';
  this.model.validatorUrl = null;
} else {
  //this.model.validatorUrl = 'http://online.swagger.io/validator';
  this.model.validatorUrl = null;
}

Swashbuckle.OData를 사용할 때 비슷한 문제가있는 anynoe에게 :

I was having issues to integrated Swagger with our OData endpoints (using ODataController for API and Swashbuckle.OData NuGet package). I had to write our own document filter for it and add it:

GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "OurSolution.API");
                    c.DocumentFilter<SwaggerDocumentFilter>();
                    //c.CustomProvider((defaultProvider) => new ODataSwaggerProvider(defaultProvider, c, GlobalConfiguration.Configuration));
                    c.IncludeXmlComments(GetXmlCommentsPath());
                    c.UseFullTypeNameInSchemaIds();
                    c.RootUrl(req => ConfigurationManager.AppSettings["AppUrl"]);
                })
            .EnableSwaggerUi(c =>
            {
                c.DisableValidator();
            });

Apparently in order to avoid validation error I had to comment out line which is setting ODataSwaggerProvider along with turning off validator as mentioned in posts above. This makes usefulness of Swashbuckle.OData questionable yet I didn't test whatever it works with vanilla Swashbuckle.

Note: I used approach described on GitHub page for Swashbuckle.OData but it was not working: showing no possible endpoints at all. Maybe somebody knows better solution.

참고URL : https://stackoverflow.com/questions/32188386/cant-read-from-file-issue-in-swagger-ui

반응형