Search in sources :

Example 6 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OperationProcessor method processOperation.

public void processOperation(Operation operation) {
    final List<Parameter> processedOperationParameters = parameterProcessor.processParameters(operation.getParameters());
    if (processedOperationParameters != null) {
        operation.setParameters(processedOperationParameters);
    }
    final RequestBody requestBody = operation.getRequestBody();
    if (requestBody != null) {
        requestBodyProcessor.processRequestBody(requestBody);
    }
    final Map<String, ApiResponse> responses = operation.getResponses();
    if (responses != null) {
        for (String responseCode : responses.keySet()) {
            ApiResponse response = responses.get(responseCode);
            if (response != null) {
                // This part allows parser to put response schema inline without the resolveFully option set to true
                if (response.get$ref() != null) {
                    responseProcessor.processResponse(response);
                    RefFormat refFormat = computeRefFormat(response.get$ref());
                    ApiResponse resolvedResponse = cache.loadRef(response.get$ref(), refFormat, ApiResponse.class);
                    if (resolvedResponse != null) {
                        response = resolvedResponse;
                        responses.put(responseCode, resolvedResponse);
                    }
                }
                responseProcessor.processResponse(response);
            }
        }
    }
    final Map<String, Callback> callbacks = operation.getCallbacks();
    if (callbacks != null) {
        for (String name : callbacks.keySet()) {
            Callback callback = callbacks.get(name);
            if (callback != null) {
                if (callback.get$ref() != null) {
                    String $ref = callback.get$ref();
                    RefFormat refFormat = computeRefFormat($ref);
                    if (isAnExternalRefFormat(refFormat)) {
                        final String newRef = externalRefProcessor.processRefToExternalCallback($ref, refFormat);
                        if (newRef != null) {
                            callback.set$ref(newRef);
                        }
                    }
                }
                for (String callbackName : callback.keySet()) {
                    PathItem pathItem = callback.get(callbackName);
                    final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
                    for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
                        Operation op = operationMap.get(httpMethod);
                        processOperation(op);
                    }
                    List<Parameter> parameters = pathItem.getParameters();
                    if (parameters != null) {
                        for (Parameter parameter : parameters) {
                            parameterProcessor.processParameter(parameter);
                        }
                    }
                }
            }
        }
    }
}
Also used : RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Parameter(io.swagger.v3.oas.models.parameters.Parameter) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 7 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class PathsProcessor method updateRefs.

protected void updateRefs(RequestBody body, String pathRef) {
    if (body.get$ref() != null) {
        body.set$ref(computeRef(body.get$ref(), pathRef));
    }
    if (body.getContent() != null) {
        Map<String, MediaType> content = body.getContent();
        for (String key : content.keySet()) {
            MediaType mediaType = content.get(key);
            if (mediaType.getSchema() != null) {
                updateRefs(mediaType.getSchema(), pathRef);
            }
            Map<String, Example> examples = content.get(key).getExamples();
            if (examples != null) {
                for (Example example : examples.values()) {
                    updateRefs(example, pathRef);
                }
            }
        }
    } else if (body.get$ref() != null) {
    }
}
Also used : Example(io.swagger.v3.oas.models.examples.Example) MediaType(io.swagger.v3.oas.models.media.MediaType)

Example 8 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class ParameterProcessorTest method testProcessParameters_BodyParameter.

@Test
public void testProcessParameters_BodyParameter(@Injectable final Schema bodyParamSchema) throws Exception {
    expectedModelProcessorCreation();
    RequestBody bodyParameter = new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(bodyParamSchema)));
    expectModelProcessorInvoked(bodyParamSchema);
    new RequestBodyProcessor(cache, openAPI).processRequestBody(bodyParameter);
    new FullVerifications() {

        {
        }
    };
}
Also used : Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 9 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testSkipInlineMatchesFalse.

@Test
public void testSkipInlineMatchesFalse() {
    final OpenAPI openAPI = new OpenAPI();
    final InlineModelResolver inlineModelResolver = new InlineModelResolver();
    final Schema operationAlphaInAsset = new ObjectSchema();
    operationAlphaInAsset.setTitle("operationAlphaInAsset");
    operationAlphaInAsset.addProperties("id1", new IntegerSchema());
    operationAlphaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationAlphaIn = new ObjectSchema();
    operationAlphaIn.setTitle("operationAlphaIn");
    operationAlphaIn.addProperties("asset", operationAlphaInAsset);
    final Schema operationAlphaRequest = new ObjectSchema();
    operationAlphaRequest.setTitle("operationAlphaRequest");
    operationAlphaRequest.addProperties("in", operationAlphaIn);
    final Schema operationBetaInAsset = new ObjectSchema();
    operationBetaInAsset.setTitle("operationBetaInAsset");
    operationBetaInAsset.addProperties("id1", new IntegerSchema());
    operationBetaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationBetaIn = new ObjectSchema();
    operationBetaIn.setTitle("operationBetaIn");
    operationBetaIn.addProperties("asset", operationBetaInAsset);
    final Schema operationBetaRequest = new ObjectSchema();
    operationBetaRequest.setTitle("operationBetaRequest");
    operationBetaRequest.addProperties("in", operationBetaIn);
    openAPI.path("/operationAlpha", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationAlphaRequest))))));
    openAPI.path("/operationBeta", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationBetaRequest))))));
    inlineModelResolver.flatten(openAPI);
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertEquals(openAPI.getComponents().getSchemas().size(), 6);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 10 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testIssue386.

@Test
public void testIssue386() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  description: bleh\n" + "  version: 2.0.0\n" + "  title: Test\n" + "paths:\n" + "  /foo:\n" + "    post:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: object\n" + "              enum:\n" + "                - id: fun\n" + "              properties:\n" + "                id:\n" + "                  type: string\n" + "components:\n" + "  schemas:\n" + "    Fun:\n" + "      type: object\n" + "      properties:\n" + "        complex:\n" + "          enum:\n" + "            - id: 110\n" + "          type: object\n" + "          properties:\n" + "            id:\n" + "              type: string\n" + "    MyEnum:\n" + "      type: integer\n" + "      enum:\n" + "        - value: 3\n" + "          description: Value 1\n" + "        - value: 10\n" + "          description: Value 2";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
}
Also used : 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)

Aggregations

Test (org.testng.annotations.Test)67 OpenAPI (io.swagger.v3.oas.models.OpenAPI)62 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)59 Schema (io.swagger.v3.oas.models.media.Schema)46 Operation (io.swagger.v3.oas.annotations.Operation)41 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)40 MediaType (io.swagger.v3.oas.models.media.MediaType)36 StringSchema (io.swagger.v3.oas.models.media.StringSchema)35 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)32 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)32 Content (io.swagger.v3.oas.models.media.Content)31 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)28 Operation (io.swagger.v3.oas.models.Operation)27 PathItem (io.swagger.v3.oas.models.PathItem)23 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)21 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)14 Components (io.swagger.v3.oas.models.Components)13 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)12