swagger-parser icon indicating copy to clipboard operation
swagger-parser copied to clipboard

get path extension from swagger 2

Open klokane opened this issue 4 years ago • 10 comments

Hi, is there way how can i get extension form 'paths' level (while parse swagger 2)?

According to spec https://swagger.io/specification/v2/#pathsObject paths can contain

  • /{path} -> PathItemObject
  • ^x- -> Any

eg:

swagger: "2.0"
info:
  title: api name
  version: 1.0.0
paths:
  x-my-extension:
    type: HTTP_BACKEND
    url: https://example.com/mock
  /alerts/KS:
    get:
      description: description if operation
      responses:
        '200':
          description: Successful Response
          schema:
            type: object

Swagger.getPaths() returns Map<String, Path>, but i did not find way how to access extension.

Tried on 1.0.51

klokane avatar Sep 23 '20 12:09 klokane

No way: https://github.com/swagger-api/swagger-parser/blob/v1.0.51/modules/swagger-parser/src/main/java/io/swagger/parser/util/SwaggerDeserializer.java#L183

klokane avatar Sep 24 '20 11:09 klokane

hi @klokane should x-my-extension have the two dots like this: x-my-extension:?

gracekarina avatar Sep 24 '20 18:09 gracekarina

@gracekarina sure, you are right. I wrong copy example, sorry

klokane avatar Sep 24 '20 21:09 klokane

I edit original question and correct it.

klokane avatar Sep 24 '20 21:09 klokane

We are looking into it!

gracekarina avatar Sep 24 '20 21:09 gracekarina

BTW, maybe it can be enough to access it via SwaggerDeserializer::ParseResult::getUnsupported()

https://github.com/swagger-api/swagger-parser/blob/v1.0.51/modules/swagger-parser/src/main/java/io/swagger/parser/util/SwaggerDeserializer.java#L1673

But in current version (1.0.51) is SwaggerDeserializer::ParseResult protected and it is public unaccessible

klokane avatar Sep 25 '20 09:09 klokane

Hi, @klokane thanks for pointing this issue into our direction, We have looked into it and due it's an old version of the spec and the complexity of resolving it, we can't prioritize a fix at the moment. We do support paths.extension in OAS 3. Thanks.

gracekarina avatar Sep 26 '20 14:09 gracekarina

Okay. Thank for your time. Do you plan to fix it at least at conversion swagger to oas3

klokane avatar Sep 26 '20 14:09 klokane

Double checked, and it's the same complex fix.

gracekarina avatar Sep 27 '20 02:09 gracekarina

@klokane, in the meantime, you could access the unsupported in SwaggerDeserializer::ParseResult by extending deserializer, e.g. like:

public class MySwaggerDeserializer extends SwaggerDeserializer{
    @Override
    public MySwaggerDeserializationResult deserialize(JsonNode rootNode) {
        MySwaggerDeserializationResult result = new MySwaggerDeserializationResult();
        ParseResult rootParse = new ParseResult();
        Swagger swagger = parseRoot(rootNode, rootParse);
        result.unsupported = rootParse.getUnsupported();
        result.setSwagger(swagger);
        result.setMessages(rootParse.getMessages());
        return result;
    }
    static class MySwaggerDeserializationResult extends SwaggerDeserializationResult{
        public Map<Location, JsonNode> unsupported = new LinkedHashMap<>();
    }
}

gracekarina avatar Sep 29 '20 17:09 gracekarina