Search in sources :

Example 1 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testResponseRemoteRefs.

private void testResponseRemoteRefs(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.path("/fun", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref(remoteRef))))))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200");
    final Schema ref = response.getContent().get("*/*").getSchema();
    assertEquals(ref.get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 2 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSimpleRemoteModelProperty.

private void testSimpleRemoteModelProperty(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.components(new Components().addSchemas("Sample", new Schema().addProperties("remoteRef", new Schema().$ref(remoteRef))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
    assertTrue(prop.get$ref() != null);
    assertEquals(prop.get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : Components(io.swagger.v3.oas.models.Components) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 3 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testDeserializeByteString.

@Test
public void testDeserializeByteString() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 0.0.0\n" + "  title: My Title\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      description: a test\n" + "      responses:\n" + "        '200':\n" + "          description: Successful response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: object\n" + "                properties:\n" + "                  bytes:\n" + "                    $ref: '#/components/schemas/ByteString'\n" + "components:\n" + "  schemas:\n" + "    ByteString:\n" + "      type: string\n" + "      format: byte\n" + "      default: W.T.F?\n" + "      enum:\n" + "        - VGhlIHdvcmxk\n" + "        - aXMgYWxs\n" + "        - dGhhdCBpcw==\n" + "        - dGhlIGNhc2U=\n" + "        - W.T.F?\n" + "";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
    Schema byteModel = resolved.getComponents().getSchemas().get("ByteString");
    assertTrue(byteModel instanceof ByteArraySchema);
    List<byte[]> byteValues = byteModel.getEnum();
    assertEquals(byteValues.size(), 4);
    assertEquals(new String(byteValues.get(0)), "The world");
    assertEquals(new String(byteValues.get(1)), "is all");
    assertEquals(new String(byteValues.get(2)), "that is");
    assertEquals(new String(byteValues.get(3)), "the case");
    assertEquals(byteModel.getDefault(), null);
    assertEquals(result.getMessages(), Arrays.asList("attribute components.schemas.ByteString.enum=`W.T.F?` is not of type `byte`", "attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`"));
}
Also used : ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 4 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIV3Parser method resolve.

private SwaggerParseResult resolve(SwaggerParseResult result, List<AuthorizationValue> auth, ParseOptions options, String location) {
    try {
        if (options != null) {
            if (options.isResolve() || options.isResolveFully()) {
                OpenAPIResolver resolver = new OpenAPIResolver(result.getOpenAPI(), emptyListIfNull(auth), location, null, options);
                resolver.resolve(result);
                if (options.isResolveFully()) {
                    new ResolverFully(options.isResolveCombinators()).resolveFully(result.getOpenAPI());
                }
            }
            if (options.isFlatten()) {
                final InlineModelResolver inlineModelResolver = new InlineModelResolver(options.isFlattenComposedSchemas(), options.isCamelCaseFlattenNaming(), options.isSkipMatches());
                if (result.getOpenAPI() != null) {
                    inlineModelResolver.flatten(result.getOpenAPI());
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Exception while resolving:", e);
        // TODO verify if this change makes sense (adding resolve messages instead of replacing)
        result.getMessages().add(e.getMessage());
    // result.setMessages(Collections.singletonList(e.getMessage()));
    }
    return result;
}
Also used : InlineModelResolver(io.swagger.v3.parser.util.InlineModelResolver) ReadContentException(io.swagger.v3.parser.exception.ReadContentException) EncodingNotSupportedException(io.swagger.v3.parser.exception.EncodingNotSupportedException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResolverFully(io.swagger.v3.parser.util.ResolverFully)

Example 5 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSettingsAddParametersToEachOperationDisabled.

@Test
public void testSettingsAddParametersToEachOperationDisabled() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: test spec\n" + "  version: '1.0'\n" + "paths:\n" + "  '/test/{id}':\n" + "    parameters:\n" + "      - name: id\n" + "        in: path\n" + "        required: true\n" + "        schema:\n" + "          type: string\n" + "    get:\n" + "      description: test get\n" + "      parameters:\n" + "        - name: page\n" + "          in: query\n" + "          schema:\n" + "            type: string\n" + "      responses:\n" + "        default:\n" + "          description: test response";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI swagger = result.getOpenAPI();
    final OpenAPI resolved = new OpenAPIResolver(swagger, null, null, new OpenAPIResolver.Settings().addParametersToEachOperation(false)).resolve();
    assertEquals(resolved.getPaths().get("/test/{id}").getParameters().size(), 1);
    PathParameter pp = (PathParameter) resolved.getPaths().get("/test/{id}").getParameters().get(0);
    assertEquals(pp.getName(), "id");
    assertEquals(resolved.getPaths().get("/test/{id}").getGet().getParameters().size(), 1);
    QueryParameter qp = (QueryParameter) resolved.getPaths().get("/test/{id}").getGet().getParameters().get(0);
    assertEquals(qp.getName(), "page");
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Aggregations

OpenAPIResolver (io.swagger.v3.parser.OpenAPIResolver)21 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Test (org.testng.annotations.Test)16 StringSchema (io.swagger.v3.oas.models.media.StringSchema)15 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)14 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)13 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)13 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)13 Schema (io.swagger.v3.oas.models.media.Schema)13 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)10 Operation (io.swagger.v3.oas.models.Operation)8 PathItem (io.swagger.v3.oas.models.PathItem)8 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)8 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)8 Components (io.swagger.v3.oas.models.Components)7 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)7 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5