Search in sources :

Example 1 with ParameterStyle

use of io.swagger.v3.oas.annotations.enums.ParameterStyle in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcher method createHttpRequest.

private HttpRequest createHttpRequest(OpenAPIDefinition openAPIDefinition, OpenAPI openAPI, String path, Pair<String, Operation> methodOperationPair) {
    HttpRequest httpRequest = new HttpRequest().withMethod(methodOperationPair.getKey()).withPath(path);
    Operation operation = methodOperationPair.getValue();
    // parameters
    if (operation.getParameters() != null) {
        for (Parameter parameter : operation.getParameters()) {
            Schema<?> schema = Optional.ofNullable(parameter.getSchema()).orElse(Optional.ofNullable(parameter.getContent()).flatMap(content -> content.values().stream().map(MediaType::getSchema).findFirst()).orElse(null));
            if (schema != null) {
                try {
                    NottableString name = (parameter.getRequired() != null && parameter.getRequired() ? string(parameter.getName()) : optional(parameter.getName())).withStyle(parameterStyle(parameter.getExplode(), parameter.getStyle()));
                    if (parameter.getAllowEmptyValue() != null && parameter.getAllowEmptyValue()) {
                        schema.nullable(true);
                    }
                    if (Boolean.TRUE.equals(parameter.getAllowReserved())) {
                        throw new IllegalArgumentException("allowReserved field is not supported on parameters, found on operation: \"" + methodOperationPair.getRight().getOperationId() + "\" method: \"" + methodOperationPair.getLeft() + "\" parameter: \"" + name + "\" in: \"" + parameter.getIn() + "\"");
                    }
                    switch(parameter.getIn()) {
                        case "path":
                            {
                                httpRequest.withPathParameter(name, schemaString(OBJECT_WRITER.writeValueAsString(schema)));
                                break;
                            }
                        case "query":
                            {
                                httpRequest.withQueryStringParameter(name, schemaString(OBJECT_WRITER.writeValueAsString(schema)));
                                break;
                            }
                        case "header":
                            {
                                httpRequest.withHeader(name, schemaString(OBJECT_WRITER.writeValueAsString(schema)));
                                break;
                            }
                        case "cookie":
                            {
                                httpRequest.withCookie(name, schemaString(OBJECT_WRITER.writeValueAsString(schema)));
                                break;
                            }
                        default:
                            mockServerLogger.logEvent(new LogEntry().setLogLevel(ERROR).setMessageFormat("unknown value for the parameter in property, expected \"query\", \"header\", \"path\" or \"cookie\" found \"" + parameter.getIn() + "\""));
                    }
                } catch (IOException exception) {
                    mockServerLogger.logEvent(new LogEntry().setLogLevel(ERROR).setMessageFormat("exception while creating adding parameter{}from the schema{}").setArguments(parameter, openAPIDefinition).setThrowable(exception));
                }
            }
        }
        // set matching key matching style to ensure all values are matched against any schema (not just one)
        if (httpRequest.getPathParameters() != null) {
            httpRequest.getPathParameters().withKeyMatchStyle(KeyMatchStyle.MATCHING_KEY);
        }
        if (httpRequest.getQueryStringParameters() != null) {
            httpRequest.getQueryStringParameters().withKeyMatchStyle(KeyMatchStyle.MATCHING_KEY);
        }
        if (httpRequest.getHeaders() != null) {
            httpRequest.getHeaders().withKeyMatchStyle(KeyMatchStyle.MATCHING_KEY);
        }
    }
    // security schemes
    Map<String, Set<String>> headerRequirements = new HashMap<>();
    Map<String, Set<String>> queryStringParameterRequirements = new HashMap<>();
    Map<String, Set<String>> cookieRequirements = new HashMap<>();
    buildSecurityValues(openAPI, headerRequirements, queryStringParameterRequirements, cookieRequirements, openAPI.getSecurity());
    buildSecurityValues(openAPI, headerRequirements, queryStringParameterRequirements, cookieRequirements, methodOperationPair.getRight().getSecurity());
    if (!headerRequirements.isEmpty()) {
        if (headerRequirements.keySet().size() > 1) {
            for (Map.Entry<String, Set<String>> headerMatchEntry : headerRequirements.entrySet()) {
                httpRequest.withHeader("?" + headerMatchEntry.getKey(), Joiner.on("|").join(headerMatchEntry.getValue()));
            }
            httpRequest.withHeader(Joiner.on("|").join(headerRequirements.keySet()), ".*");
        } else if (!queryStringParameterRequirements.isEmpty() || !cookieRequirements.isEmpty()) {
            httpRequest.withHeader("?" + Joiner.on("|").join(headerRequirements.keySet()), Joiner.on("|").join(headerRequirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList())));
        } else {
            httpRequest.withHeader(Joiner.on("|").join(headerRequirements.keySet()), Joiner.on("|").join(headerRequirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList())));
        }
    }
    if (!queryStringParameterRequirements.isEmpty()) {
        if (queryStringParameterRequirements.keySet().size() > 1) {
            for (Map.Entry<String, Set<String>> queryStringParameterMatchEntry : queryStringParameterRequirements.entrySet()) {
                httpRequest.withQueryStringParameter("?" + queryStringParameterMatchEntry.getKey(), Joiner.on("|").join(queryStringParameterMatchEntry.getValue()));
            }
            httpRequest.withQueryStringParameter(Joiner.on("|").join(queryStringParameterRequirements.keySet()), ".*");
        } else if (!headerRequirements.isEmpty() || !cookieRequirements.isEmpty()) {
            httpRequest.withQueryStringParameter("?" + Joiner.on("|").join(queryStringParameterRequirements.keySet()), Joiner.on("|").join(queryStringParameterRequirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList())));
        } else {
            httpRequest.withQueryStringParameter(Joiner.on("|").join(queryStringParameterRequirements.keySet()), Joiner.on("|").join(queryStringParameterRequirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList())));
        }
    }
    if (!cookieRequirements.isEmpty()) {
        if (cookieRequirements.keySet().size() > 1) {
            for (Map.Entry<String, Set<String>> cookieMatchEntry : cookieRequirements.entrySet()) {
                httpRequest.withCookie("?" + cookieMatchEntry.getKey(), Joiner.on("|").join(cookieMatchEntry.getValue()));
            }
            httpRequest.withCookie(Joiner.on("|").join(cookieRequirements.keySet()), ".*");
        } else if (!queryStringParameterRequirements.isEmpty() || !headerRequirements.isEmpty()) {
            httpRequest.withCookie("?" + Joiner.on("|").join(cookieRequirements.keySet()), Joiner.on("|").join(cookieRequirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList())));
        } else {
            httpRequest.withCookie(Joiner.on("|").join(cookieRequirements.keySet()), Joiner.on("|").join(cookieRequirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList())));
        }
    }
    return httpRequest;
}
Also used : Operation(io.swagger.v3.oas.models.Operation) IOException(java.io.IOException) NottableSchemaString.schemaString(org.mockserver.model.NottableSchemaString.schemaString) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) LogEntry(org.mockserver.log.model.LogEntry)

Example 2 with ParameterStyle

use of io.swagger.v3.oas.annotations.enums.ParameterStyle in project springdoc-openapi by springdoc.

the class DataRestDelegatingMethodParameterCustomizer method getNewParameterAnnotationForField.

/**
 * Gets new parameter annotation for field.
 *
 * @param methodParameter the method parameter
 * @param pageableDefault the pageable default
 * @return the new parameter annotation for field
 */
private Optional<Annotation> getNewParameterAnnotationForField(MethodParameter methodParameter, PageableDefault pageableDefault) {
    String parameterName = methodParameter.getParameterName();
    Field field;
    Parameter parameterNew;
    try {
        field = methodParameter.getContainingClass().getDeclaredField(parameterName);
        Parameter parameter = field.getAnnotation(Parameter.class);
        parameterNew = new Parameter() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return parameter.annotationType();
            }

            @Override
            public String name() {
                return getName(parameterName, parameter.name());
            }

            @Override
            public ParameterIn in() {
                return parameter.in();
            }

            @Override
            public String description() {
                return parameter.description();
            }

            @Override
            public boolean required() {
                return parameter.required();
            }

            @Override
            public boolean deprecated() {
                return parameter.deprecated();
            }

            @Override
            public boolean allowEmptyValue() {
                return parameter.allowEmptyValue();
            }

            @Override
            public ParameterStyle style() {
                return parameter.style();
            }

            @Override
            public Explode explode() {
                return parameter.explode();
            }

            @Override
            public boolean allowReserved() {
                return parameter.allowReserved();
            }

            @Override
            public Schema schema() {
                return new Schema() {

                    @Override
                    public Class<? extends Annotation> annotationType() {
                        return parameter.schema().annotationType();
                    }

                    @Override
                    public Class<?> implementation() {
                        return parameter.schema().implementation();
                    }

                    @Override
                    public Class<?> not() {
                        return parameter.schema().not();
                    }

                    @Override
                    public Class<?>[] oneOf() {
                        return parameter.schema().oneOf();
                    }

                    @Override
                    public Class<?>[] anyOf() {
                        return parameter.schema().anyOf();
                    }

                    @Override
                    public Class<?>[] allOf() {
                        return parameter.schema().allOf();
                    }

                    @Override
                    public String name() {
                        return parameter.schema().name();
                    }

                    @Override
                    public String title() {
                        return parameter.schema().title();
                    }

                    @Override
                    public double multipleOf() {
                        return parameter.schema().multipleOf();
                    }

                    @Override
                    public String maximum() {
                        return parameter.schema().maximum();
                    }

                    @Override
                    public boolean exclusiveMaximum() {
                        return parameter.schema().exclusiveMaximum();
                    }

                    @Override
                    public String minimum() {
                        return parameter.schema().minimum();
                    }

                    @Override
                    public boolean exclusiveMinimum() {
                        return parameter.schema().exclusiveMaximum();
                    }

                    @Override
                    public int maxLength() {
                        return parameter.schema().maxLength();
                    }

                    @Override
                    public int minLength() {
                        return parameter.schema().minLength();
                    }

                    @Override
                    public String pattern() {
                        return parameter.schema().pattern();
                    }

                    @Override
                    public int maxProperties() {
                        return parameter.schema().maxProperties();
                    }

                    @Override
                    public int minProperties() {
                        return parameter.schema().minProperties();
                    }

                    @Override
                    public String[] requiredProperties() {
                        return parameter.schema().requiredProperties();
                    }

                    @Override
                    public boolean required() {
                        return parameter.schema().required();
                    }

                    @Override
                    public String description() {
                        return parameter.schema().description();
                    }

                    @Override
                    public String format() {
                        return parameter.schema().format();
                    }

                    @Override
                    public String ref() {
                        return parameter.schema().ref();
                    }

                    @Override
                    public boolean nullable() {
                        return parameter.schema().nullable();
                    }

                    @Override
                    public boolean readOnly() {
                        return AccessMode.READ_ONLY.equals(parameter.schema().accessMode());
                    }

                    @Override
                    public boolean writeOnly() {
                        return AccessMode.WRITE_ONLY.equals(parameter.schema().accessMode());
                    }

                    @Override
                    public AccessMode accessMode() {
                        return parameter.schema().accessMode();
                    }

                    @Override
                    public String example() {
                        return parameter.schema().example();
                    }

                    @Override
                    public ExternalDocumentation externalDocs() {
                        return parameter.schema().externalDocs();
                    }

                    @Override
                    public boolean deprecated() {
                        return parameter.schema().deprecated();
                    }

                    @Override
                    public String type() {
                        return parameter.schema().type();
                    }

                    @Override
                    public String[] allowableValues() {
                        return parameter.schema().allowableValues();
                    }

                    @Override
                    public String defaultValue() {
                        return getDefaultValue(parameterName, pageableDefault, parameter.schema().defaultValue());
                    }

                    @Override
                    public String discriminatorProperty() {
                        return parameter.schema().discriminatorProperty();
                    }

                    @Override
                    public DiscriminatorMapping[] discriminatorMapping() {
                        return parameter.schema().discriminatorMapping();
                    }

                    @Override
                    public boolean hidden() {
                        return parameter.schema().hidden();
                    }

                    @Override
                    public boolean enumAsRef() {
                        return parameter.schema().enumAsRef();
                    }

                    @Override
                    public Class<?>[] subTypes() {
                        return parameter.schema().subTypes();
                    }

                    @Override
                    public Extension[] extensions() {
                        return parameter.schema().extensions();
                    }
                };
            }

            @Override
            public ArraySchema array() {
                ArraySchema arraySchema = parameter.array();
                return new ArraySchema() {

                    @Override
                    public Class<? extends Annotation> annotationType() {
                        return arraySchema.annotationType();
                    }

                    @Override
                    public Schema schema() {
                        return arraySchema.schema();
                    }

                    @Override
                    public Schema arraySchema() {
                        Schema schema = arraySchema.arraySchema();
                        return new Schema() {

                            @Override
                            public Class<? extends Annotation> annotationType() {
                                return schema.annotationType();
                            }

                            @Override
                            public Class<?> implementation() {
                                return schema.implementation();
                            }

                            @Override
                            public Class<?> not() {
                                return schema.not();
                            }

                            @Override
                            public Class<?>[] oneOf() {
                                return schema.oneOf();
                            }

                            @Override
                            public Class<?>[] anyOf() {
                                return schema.anyOf();
                            }

                            @Override
                            public Class<?>[] allOf() {
                                return schema.allOf();
                            }

                            @Override
                            public String name() {
                                return schema.name();
                            }

                            @Override
                            public String title() {
                                return schema.title();
                            }

                            @Override
                            public double multipleOf() {
                                return schema.multipleOf();
                            }

                            @Override
                            public String maximum() {
                                return schema.maximum();
                            }

                            @Override
                            public boolean exclusiveMaximum() {
                                return schema.exclusiveMaximum();
                            }

                            @Override
                            public String minimum() {
                                return schema.minimum();
                            }

                            @Override
                            public boolean exclusiveMinimum() {
                                return schema.exclusiveMinimum();
                            }

                            @Override
                            public int maxLength() {
                                return schema.maxLength();
                            }

                            @Override
                            public int minLength() {
                                return schema.minLength();
                            }

                            @Override
                            public String pattern() {
                                return schema.pattern();
                            }

                            @Override
                            public int maxProperties() {
                                return schema.maxProperties();
                            }

                            @Override
                            public int minProperties() {
                                return schema.minProperties();
                            }

                            @Override
                            public String[] requiredProperties() {
                                return schema.requiredProperties();
                            }

                            @Override
                            public boolean required() {
                                return schema.required();
                            }

                            @Override
                            public String description() {
                                return schema.description();
                            }

                            @Override
                            public String format() {
                                return schema.format();
                            }

                            @Override
                            public String ref() {
                                return schema.ref();
                            }

                            @Override
                            public boolean nullable() {
                                return schema.nullable();
                            }

                            @Override
                            public boolean readOnly() {
                                return AccessMode.READ_ONLY.equals(schema.accessMode());
                            }

                            @Override
                            public boolean writeOnly() {
                                return AccessMode.WRITE_ONLY.equals(schema.accessMode());
                            }

                            @Override
                            public AccessMode accessMode() {
                                return schema.accessMode();
                            }

                            @Override
                            public String example() {
                                return schema.example();
                            }

                            @Override
                            public ExternalDocumentation externalDocs() {
                                return schema.externalDocs();
                            }

                            @Override
                            public boolean deprecated() {
                                return schema.deprecated();
                            }

                            @Override
                            public String type() {
                                return schema.type();
                            }

                            @Override
                            public String[] allowableValues() {
                                return schema.allowableValues();
                            }

                            @Override
                            public String defaultValue() {
                                return getArrayDefaultValue(parameterName, pageableDefault, schema.defaultValue());
                            }

                            @Override
                            public String discriminatorProperty() {
                                return schema.discriminatorProperty();
                            }

                            @Override
                            public DiscriminatorMapping[] discriminatorMapping() {
                                return schema.discriminatorMapping();
                            }

                            @Override
                            public boolean hidden() {
                                return schema.hidden();
                            }

                            @Override
                            public boolean enumAsRef() {
                                return schema.enumAsRef();
                            }

                            @Override
                            public Class<?>[] subTypes() {
                                return schema.subTypes();
                            }

                            @Override
                            public Extension[] extensions() {
                                return schema.extensions();
                            }
                        };
                    }

                    @Override
                    public int maxItems() {
                        return arraySchema.maxItems();
                    }

                    @Override
                    public int minItems() {
                        return arraySchema.minItems();
                    }

                    @Override
                    public boolean uniqueItems() {
                        return arraySchema.uniqueItems();
                    }

                    @Override
                    public Extension[] extensions() {
                        return arraySchema.extensions();
                    }
                };
            }

            @Override
            public Content[] content() {
                return parameter.content();
            }

            @Override
            public boolean hidden() {
                return parameter.hidden();
            }

            @Override
            public ExampleObject[] examples() {
                return parameter.examples();
            }

            @Override
            public String example() {
                return parameter.example();
            }

            @Override
            public Extension[] extensions() {
                return parameter.extensions();
            }

            @Override
            public String ref() {
                return parameter.ref();
            }
        };
        return Optional.of(parameterNew);
    } catch (NoSuchFieldException e) {
        LOGGER.warn(e.getMessage());
        return Optional.empty();
    }
}
Also used : ParameterStyle(io.swagger.v3.oas.annotations.enums.ParameterStyle) ParameterIn(io.swagger.v3.oas.annotations.enums.ParameterIn) Schema(io.swagger.v3.oas.annotations.media.Schema) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) Explode(io.swagger.v3.oas.annotations.enums.Explode) ExternalDocumentation(io.swagger.v3.oas.annotations.ExternalDocumentation) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) DelegatingMethodParameter(org.springdoc.core.DelegatingMethodParameter) MethodParameter(org.springframework.core.MethodParameter) Parameter(io.swagger.v3.oas.annotations.Parameter)

Aggregations

ExternalDocumentation (io.swagger.v3.oas.annotations.ExternalDocumentation)1 Parameter (io.swagger.v3.oas.annotations.Parameter)1 Explode (io.swagger.v3.oas.annotations.enums.Explode)1 ParameterIn (io.swagger.v3.oas.annotations.enums.ParameterIn)1 ParameterStyle (io.swagger.v3.oas.annotations.enums.ParameterStyle)1 ArraySchema (io.swagger.v3.oas.annotations.media.ArraySchema)1 Schema (io.swagger.v3.oas.annotations.media.Schema)1 Operation (io.swagger.v3.oas.models.Operation)1 MediaType (io.swagger.v3.oas.models.media.MediaType)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 LogEntry (org.mockserver.log.model.LogEntry)1 NottableSchemaString.schemaString (org.mockserver.model.NottableSchemaString.schemaString)1 DelegatingMethodParameter (org.springdoc.core.DelegatingMethodParameter)1 MethodParameter (org.springframework.core.MethodParameter)1