Search in sources :

Example 1 with SwaggerResolver

use of io.swagger.parser.SwaggerResolver in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testDeserializeEnum.

@Test
public void testDeserializeEnum() {
    String yaml = "swagger: '2.0'\n" + "info:\n" + "  version: 0.0.0\n" + "  title: your title\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      description: a test\n" + "      responses:\n" + "        '200':\n" + "          description: Successful response\n" + "          schema:\n" + "            type: object\n" + "            properties:\n" + "              se:\n" + "                $ref: '#/definitions/StringEnum'\n" + "              ie:\n" + "                $ref: '#/definitions/IntegerEnum'\n" + "              ne:\n" + "                $ref: '#/definitions/NumberEnum'\n" + "definitions:\n" + "  StringEnum:\n" + "    type: string\n" + "    default: foo\n" + "    enum:\n" + "      - First\n" + "      - Second\n" + "  IntegerEnum:\n" + "    type: integer\n" + "    default: 1\n" + "    enum:\n" + "      - -1\n" + "      - 0\n" + "      - 1\n" + "  NumberEnum:\n" + "    type: number\n" + "    default: 3.14\n" + "    enum:\n" + "      - -1.151\n" + "      - 0.0\n" + "      - 1.6161\n" + "      - 3.14";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(yaml);
    final Swagger resolved = new SwaggerResolver(result.getSwagger(), null).resolve();
    Model stringModel = resolved.getDefinitions().get("StringEnum");
    assertTrue(stringModel instanceof ModelImpl);
    ModelImpl stringImpl = (ModelImpl) stringModel;
    List<String> stringValues = stringImpl.getEnum();
    assertEquals(2, stringValues.size());
    assertEquals("First", stringValues.get(0));
    assertEquals("Second", stringValues.get(1));
    Model integerModel = resolved.getDefinitions().get("IntegerEnum");
    assertTrue(integerModel instanceof ModelImpl);
    ModelImpl integerImpl = (ModelImpl) integerModel;
    List<String> integerValues = integerImpl.getEnum();
    assertEquals(3, integerValues.size());
    assertEquals("-1", integerValues.get(0));
    assertEquals("0", integerValues.get(1));
    assertEquals("1", integerValues.get(2));
    Model numberModel = resolved.getDefinitions().get("NumberEnum");
    assertTrue(numberModel instanceof ModelImpl);
    ModelImpl numberImpl = (ModelImpl) numberModel;
    List<String> numberValues = numberImpl.getEnum();
    assertEquals(4, numberValues.size());
    assertEquals("-1.151", numberValues.get(0));
    assertEquals("0.0", numberValues.get(1));
    assertEquals("1.6161", numberValues.get(2));
    assertEquals("3.14", numberValues.get(3));
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerResolver(io.swagger.parser.SwaggerResolver) Test(org.testng.annotations.Test)

Example 2 with SwaggerResolver

use of io.swagger.parser.SwaggerResolver in project swagger-parser by swagger-api.

the class SwaggerDeserializerTest method testDeserializeBinaryString.

@Test
public void testDeserializeBinaryString() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"info\": {\n" + "    \"title\": \"foo\"\n" + "  },\n" + "  \"paths\": {\n" + "    \"/test\": {\n" + "      \"post\": {\n" + "        \"parameters\": [\n" + "          {\n" + "            \"name\": \"AnyName\",\n" + "            \"in\": \"body\",\n" + "            \"schema\": {\n" + "              \"type\": \"string\",\n" + "              \"format\": \"binary\"\n" + "            }\n" + "          }\n" + "        ],\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"description\": \"ok\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(json);
    final Swagger resolved = new SwaggerResolver(result.getSwagger(), null).resolve();
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerResolver(io.swagger.parser.SwaggerResolver) Test(org.testng.annotations.Test)

Example 3 with SwaggerResolver

use of io.swagger.parser.SwaggerResolver in project swagger-parser by swagger-api.

the class SwaggerConverter method readContents.

@Override
public SwaggerParseResult readContents(String swaggerAsString, List<AuthorizationValue> auth, ParseOptions options) {
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerAsString, options == null ? true : options.isResolve());
    if (options != null) {
        if (options.isResolve()) {
            Swagger resolved = new SwaggerResolver(result.getSwagger(), convert(auth)).resolve();
            result.setSwagger(resolved);
        }
    }
    return readResult(result, auth, options);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) SwaggerResolver(io.swagger.parser.SwaggerResolver) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger)

Aggregations

SwaggerParser (io.swagger.parser.SwaggerParser)3 SwaggerResolver (io.swagger.parser.SwaggerResolver)3 Test (org.testng.annotations.Test)2 Swagger (io.swagger.models.Swagger)1 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)1