question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema has both $ref and attributes

See original GitHub issue

Describe the bug My config (api-docs.yaml.txt):

openapi: 3.0.1
paths:
  /api/users/self:
    get:
      tags:
      - users
      operationId: getMe
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                type: string
        default:
          $ref: '#/components/responses/default'
components:
  schemas:
    ErrorResponse:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
  responses:
    default:
      description: other error responses
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
      $ref: '#/components/responses/default'

I noticed that componenets.responses[0].$ref is included in the yaml which is not supposed to be there.

Here’s my java code (Spring Boot 2.4.0, Springdoc 1.5.0):

    public static final String JWT_SECURITY_SCHEME = "jwt";

    private final List<CommonResponse> commonResponses = new LinkedList<>();

    @Bean
    public OpenAPI openApi() {
        val components = new Components();
        components.addSecuritySchemes(JWT_SECURITY_SCHEME,
                new SecurityScheme()
                        .type(Type.HTTP)
                        .scheme("bearer")
                        .bearerFormat("JWT")
        );
        addCommonSecurityResponses(components);
        addDefaultErrorResponse(components);
        return new OpenAPI()
                .components(components)
                .addSecurityItem(new SecurityRequirement().addList(JWT_SECURITY_SCHEME));
    }

    private void addDefaultErrorResponse(Components components) {
        val schema = createSchema(components, ErrorResponse.class);
        val key = ApiResponses.DEFAULT;
        val response = new ApiResponse()
                .$ref(key)
                .description("other error responses")
                .content(new Content().addMediaType(
                        org.springframework.http.MediaType.APPLICATION_JSON_VALUE,
                        new MediaType().schema(schema)
                ));
        components.addResponses(key, response);
        commonResponses.add(new CommonResponse(key, response.get$ref()));
    }

    private Schema<?> createSchema(Components components, Class<?> clazz) {
        val type = new AnnotatedType(clazz).resolveAsRef(true);
        val context = new ModelConverterContextImpl(ModelConverters.getInstance().getConverters());
        val schema = context.resolve(type);
        context.getDefinedModels().forEach(components::addSchemas);
        return schema;
    }

    @Value
    private static class CommonResponse {
        String httpStatusCode;
        String reference;
    }

    private void addCommonSecurityResponses(Components components) {
        Stream.of(HttpStatus.UNAUTHORIZED, HttpStatus.FORBIDDEN).forEach(securityError -> {
                    val httpCodeStr = String.valueOf(securityError.value());
                    val response = new ApiResponse()
                            .$ref(httpCodeStr)
                            .description(securityError.getReasonPhrase())
                            .addHeaderObject(
                                    HttpHeaders.WWW_AUTHENTICATE,
                                    new Header().description("https://tools.ietf.org/html/rfc6750#section-3")
                            )
                            .content(new Content());
                    components.addResponses(httpCodeStr, response);
                    commonResponses.add(new CommonResponse(httpCodeStr, response.get$ref()));
                }
        );
    }

    

    /**
     * see https://swagger.io/docs/specification/describing-responses/#reuse
     */
    @Bean
    OperationCustomizer defaultResponsesOperationCustomizer() {
        return ((operation, handlerMethod) -> {
            commonResponses.forEach(commonResponse -> {
                val response = new ApiResponse().$ref(commonResponse.getReference());
                operation.getResponses().addApiResponse(commonResponse.getHttpStatusCode(), response);
            });
            return operation;
        });
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bnasslahsencommented, Nov 22, 2020

ok, i am ignoring this issue. Please make sure you respect the contribution guide , before opening any new issue.

0reactions
Sam-Kruglovcommented, Nov 22, 2020

@bnasslahsen as I also explained above, ignore that, even if my definition is as following, ErrorResponse is still getting removed

default:
      description: other error responses
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
Read more comments on GitHub >

github_iconTop Results From Across the Web

What does the ref attribute on an element in an XSD do?
Basically it references another element that is declared elsewhere, which may or may not be the same schema document. For instance, it could...
Read more >
ref | dbt Developer Hub
The {{ ref }} function returns a Relation object that has the same table , schema , and name attributes at the {{...
Read more >
Using $ref - Swagger
To reference a definition, use the $ref keyword: $ref: 'reference to definition'. For example, suppose you have the following schema object, which you...
Read more >
XML Schema Attributes - W3Schools
If an element has attributes, it is considered to be of a complex type. But the attribute itself is always ... XML Schema...
Read more >
XML Schema Part 0: Primer Second Edition - W3C
The fixed attribute is used in both attribute and element declarations to ensure that the attributes and elements are set to particular values....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found