无法解析指针:/definitions/Error-ModelName指针、definitions、ModelName、Error

2023-09-08 09:21:35 作者:快乐停机了

I am new to Swagger.io and so also to Spring fox. The problem I am having is, that for some Reason one Object is not correctly referenced to its model.

The error in the UI:

C C error exception throw access violation reading location 0x.... c c Soar dream的博客 CSDN博客

The error is because it ends up like this in the JSON:

"schema": {
"$ref": "#/definitions/Error-ModelName{namespace='online.staffmanager.backend.auth.model.dto', name='UserChangeSet'}"
}

if I change it to:

"schema": {
"$ref": "#/definitions/UserChangeSet"
}

it does work. And I have no Idea why the anotations are mapping it like this.

My Annotations:

 @Operation(
            tags = "auth",
            summary = "Create a new User Account",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            content = @Content(schema = @Schema(implementation = TokenInfo.class))),
                    @ApiResponse(
                            responseCode = "201",
                            content = @Content(schema = @Schema(implementation = UserChangeSet.class)))
            }
    )

SpringFoxConfig:

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SpringFoxConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}

Note: I am using Springfox 3.0.0. Thanks in advance!

解决方案

you have to add one more configuration to the Bean.

Here is the configuration need to be added:

.additionalModels(
     typeResolver.resolve(TokenInfo.class),
     typeResolver.resolve(UserChangeSet.class)
 )

This is full code:

@Configuration
@Import(SpringDataRestConfiguration.class)
public class SwaggerUIConfig {

    @Bean
    public Docket api(TypeResolver typeResolver) {
        return new Docket(DocumentationType.SWAGGER_2)
            .additionalModels(
                    typeResolver.resolve(TokenInfo.class),
                    typeResolver.resolve(UserChangeSet.class)
             )
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.projectname.controllers"))
            .paths(PathSelectors.any())
            .build()
            .useDefaultResponseMessages(false);

     }
}

Note: @EnableSwagger2 annotation is recommended to remove in version 3.0. You can refer http://springfox.github.io/springfox/docs/current/#migrating-from-existing-2-x-version

I hope can help you.