swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

Valid YAML Alias/Anchor features not supported fy Jackson #9156

Open MercurieVV opened 5 years ago

MercurieVV commented 5 years ago
Description

YAML have so called Aliases/Anchors &/*. If I use them in swagger 3 project I got exception, cause Alias/Anchor feature was skipped. Its not directly Swagger issue, its seems Jackson. But you could switch YAML parsing library to any other. Btw. I didnt had this issue in Swagger 2. Heres Jackson issue: https://github.com/FasterXML/jackson-dataformats-text/issues/98

Swagger-codegen version

3.0.4

Swagger declaration file content or url
paths:
  /marketplaces/{marketplaceId}/claims:
    get:
      tags:
        - 'claims'
      summary: get Claims list
      operationId: getClaims
      parameters:
        - &marketplaceId
          name: marketplaceId
          in: path
          description: 'marketplaceId: darty, amazon-fr, amazon-de, amazon-com'
          required: true
          schema:
            type: string
  /marketplaces/{marketplaceId}/claims/{claimId}/messages:
    post:
      tags:
        - 'claims'
      summary: post Claim message
      operationId: postClaimMessage
      parameters:
        - *marketplaceId
        - name: claimId
          in: path
          required: true
          schema:
            type: string
Command line used for generation

It doesnt metter. It generic YAML processing issue. generate something

Steps to reproduce

1) generate any code from my yaml 2) You will get java.util.NoSuchElementException: No value present

Related issues/PRs
Suggest a fix/enhancement

Use another YAML parsing lib. From Jackson bug comments I see snakeyaml process this without problem

pkral3 commented 2 years ago

The Aliases/Anchors &/* can be expanded before:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;

            OpenAPI openAPI = new OpenAPIV3Parser().read("myApi.yaml");
            String swaggerJson = Json.mapper().writeValueAsString(openAPI);
            JsonNode jsonNodeTree = new ObjectMapper().readTree(swaggerJson);
            String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
            PrintWriter pw = new PrintWriter("myApi_expand.yaml");
            pw.println(jsonAsYaml);
            pw.close();

        <dependency>
          <groupId>io.swagger.parser.v3</groupId>
          <artifactId>swagger-parser</artifactId>
          <version>2.0.32</version>
        </dependency>