Java Code Examples for io.swagger.v3.oas.models.parameters.Parameter#setName()

The following examples show how to use io.swagger.v3.oas.models.parameters.Parameter#setName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenApiParameterValidationsTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "apacheNginxRecommendationExpectations", description = "disable apache nginx via turning off recommendations")
public void testApacheNginxWithDisabledRecommendations(String in, String key, boolean matches) {
    RuleConfiguration config = new RuleConfiguration();
    config.setEnableRecommendations(false);
    OpenApiParameterValidations validator = new OpenApiParameterValidations(config);

    Parameter parameter = new Parameter();
    parameter.setIn(in);
    parameter.setName(key);

    ValidationResult result = validator.validate(new ParameterWrapper(null, parameter));
    Assert.assertNotNull(result.getWarnings());

    List<Invalid> warnings = result.getWarnings().stream()
            .filter(invalid -> ValidationConstants.ApacheNginxUnderscoreFailureMessage.equals(invalid.getMessage()))
            .collect(Collectors.toList());

    Assert.assertNotNull(warnings);
    Assert.assertEquals(warnings.size(), 0, "Expected recommendations to be disabled completely.");
}
 
Example 2
Source File: OpenApiParameterValidationsTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "apacheNginxRecommendationExpectations", description = "disable apache nginx via turning off recommendations")
public void testApacheNginxWithDisabledRule(String in, String key, boolean matches) {
    RuleConfiguration config = new RuleConfiguration();
    config.setEnableApacheNginxUnderscoreRecommendation(false);
    OpenApiParameterValidations validator = new OpenApiParameterValidations(config);

    Parameter parameter = new Parameter();
    parameter.setIn(in);
    parameter.setName(key);

    ValidationResult result = validator.validate(new ParameterWrapper(null, parameter));
    Assert.assertNotNull(result.getWarnings());

    List<Invalid> warnings = result.getWarnings().stream()
            .filter(invalid -> ValidationConstants.ApacheNginxUnderscoreFailureMessage.equals(invalid.getMessage()))
            .collect(Collectors.toList());

    Assert.assertNotNull(warnings);
    Assert.assertEquals(warnings.size(), 0, "Expected rule to be disabled.");
}
 
Example 3
Source File: OpenApiParameterValidationsTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "apacheNginxRecommendationExpectations", description = "default apache nginx recommendation")
public void testDefaultRecommendationApacheNginx(String in, String key, boolean matches) {
    RuleConfiguration config = new RuleConfiguration();
    config.setEnableRecommendations(true);
    OpenApiParameterValidations validator = new OpenApiParameterValidations(config);

    Parameter parameter = new Parameter();
    parameter.setIn(in);
    parameter.setName(key);

    ValidationResult result = validator.validate(new ParameterWrapper(null, parameter));
    Assert.assertNotNull(result.getWarnings());

    List<Invalid> warnings = result.getWarnings().stream()
            .filter(invalid -> ValidationConstants.ApacheNginxUnderscoreFailureMessage.equals(invalid.getMessage()))
            .collect(Collectors.toList());

    Assert.assertNotNull(warnings);
    if (matches) {
        Assert.assertEquals(warnings.size(), 1, "Expected " + key + " to match recommendation.");
    } else {
        Assert.assertEquals(warnings.size(), 0, "Expected " + key + " not to match recommendation.");
    }
}
 
Example 4
Source File: OASMergeUtil.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
public static Parameter mergeParameter(Parameter thisParameter, Parameter thatParameter) {
  if (thatParameter.getName() != null) {
    thisParameter.setName(thatParameter.getName());
  }
  if (thatParameter.getIn() != null) {
    thisParameter.setIn(thatParameter.getIn());
  }
  if (thatParameter.getDescription() != null) {
    thisParameter.setDescription(thatParameter.getDescription());
  }
  if (thatParameter.getRequired() != null) {
    thisParameter.setRequired(thatParameter.getRequired());
  }
  if (thatParameter.getDeprecated() != null) {
    thisParameter.setDeprecated(thatParameter.getDeprecated());
  }
  if (thatParameter.getAllowEmptyValue() != null) {
    thisParameter.setAllowEmptyValue(thatParameter.getAllowEmptyValue());
  }
  if (thatParameter.get$ref() != null) {
    thisParameter.set$ref(thatParameter.get$ref());
  }
  return thisParameter;
}
 
Example 5
Source File: GenericParameterBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Build parameter from doc parameter.
 *
 * @param parameterDoc the parameter doc
 * @param components the components
 * @param jsonView the json view
 * @return the parameter
 */
public Parameter buildParameterFromDoc(io.swagger.v3.oas.annotations.Parameter parameterDoc,
		Components components, JsonView jsonView) {
	Parameter parameter = new Parameter();
	if (StringUtils.isNotBlank(parameterDoc.description()))
		parameter.setDescription(propertyResolverUtils.resolve(parameterDoc.description()));
	if (StringUtils.isNotBlank(parameterDoc.name()))
		parameter.setName(propertyResolverUtils.resolve(parameterDoc.name()));
	if (StringUtils.isNotBlank(parameterDoc.in().toString()))
		parameter.setIn(parameterDoc.in().toString());
	if (StringUtils.isNotBlank(parameterDoc.example())) {
		try {
			parameter.setExample(Json.mapper().readTree(parameterDoc.example()));
		}
		catch (IOException e) {
			parameter.setExample(parameterDoc.example());
		}
	}
	if (parameterDoc.deprecated())
		parameter.setDeprecated(parameterDoc.deprecated());
	if (parameterDoc.required())
		parameter.setRequired(parameterDoc.required());
	if (parameterDoc.allowEmptyValue())
		parameter.setAllowEmptyValue(parameterDoc.allowEmptyValue());
	if (parameterDoc.allowReserved())
		parameter.setAllowReserved(parameterDoc.allowReserved());

	setSchema(parameterDoc, components, jsonView, parameter);
	setExamples(parameterDoc, parameter);
	setExtensions(parameterDoc, parameter);
	setParameterStyle(parameter, parameterDoc);
	setParameterExplode(parameter, parameterDoc);

	return parameter;
}
 
Example 6
Source File: PythonAbstractConnexionServerCodegen.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
    // XXX - Revert the original parameter (and path) names to make sure we have
    //       a consistent REST interface across other server/client languages:
    //
    // XXX - Reverts `x-python-connexion-openapi-name` back to the original (query/path) parameter name.
    //       We do not want to have our REST API itself being converted to pythonic params.
    //       This would be incompatible with other server implementations.
    OpenAPI openAPI = (OpenAPI) objs.get("openAPI");
    Map<String, PathItem> paths = openAPI.getPaths();
    if (paths != null) {
        List<String> pathnames = new ArrayList(paths.keySet());
        for (String pythonPathname : pathnames) {
            PathItem path = paths.get(pythonPathname);

            // Fix path parameters back to original casing
            Map<String, Object> pathExtensions = path.getExtensions();
            if (pathExtensions != null) {
                // Get and remove the (temporary) vendor extension
                String openapiPathname = (String) pathExtensions.remove("x-python-connexion-openapi-name");
                if (openapiPathname != null && !openapiPathname.equals(pythonPathname)) {
                    LOGGER.info("Path '" + pythonPathname + "' is not consistant with the original OpenAPI definition. It will be replaced back by '" + openapiPathname + "'");
                    paths.remove(pythonPathname);
                    paths.put(openapiPathname, path);
                }
            }

            Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
            if (operationMap != null) {
                for (HttpMethod method : operationMap.keySet()) {
                    Operation operation = operationMap.get(method);
                    if (operation.getParameters() != null) {
                        for (Parameter parameter : operation.getParameters()) {
                            Map<String, Object> parameterExtensions = parameter.getExtensions();
                            if (parameterExtensions != null) {
                                // Get and remove the (temporary) vendor extension
                                String swaggerParameterName = (String) parameterExtensions.remove("x-python-connexion-openapi-name");
                                if (swaggerParameterName != null) {
                                    String pythonParameterName = parameter.getName();
                                    if (!swaggerParameterName.equals(pythonParameterName)) {
                                        LOGGER.info("Reverting name of parameter '" + pythonParameterName + "' of operation '" + operation.getOperationId() + "' back to '" + swaggerParameterName + "'");
                                        parameter.setName(swaggerParameterName);
                                    } else {
                                        LOGGER.debug("Name of parameter '" + pythonParameterName + "' of operation '" + operation.getOperationId() + "' was unchanged.");
                                    }
                                } else {
                                    LOGGER.debug("x-python-connexion-openapi-name was not set on parameter '" + parameter.getName() + "' of operation '" + operation.getOperationId() + "'");
                                }
                            }
                        }
                    }
                }
            }
        }

        // Sort path names after variable name fix
        List<String> recoveredPathnames = new ArrayList(paths.keySet());
        Collections.sort(recoveredPathnames);
        for (String pathname : recoveredPathnames) {
            PathItem pathItem = paths.remove(pathname);
            paths.put(pathname, pathItem);
        }
    }

    generateJSONSpecFile(objs);
    generateYAMLSpecFile(objs);

    for (Map<String, Object> operations : getOperations(objs)) {
        @SuppressWarnings("unchecked")
        List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");

        List<Map<String, Object>> opsByPathList = sortOperationsByPath(ops);
        operations.put("operationsByPath", opsByPathList);
    }
    return super.postProcessSupportingFileData(objs);
}
 
Example 7
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 3 votes vote down vote up
@Test
public void resolveInlineParameter() throws Exception {
    OpenAPI openAPI = new OpenAPI();


    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("street", new StringSchema());

    Schema schema = new Schema();
    schema.addProperties("address", objectSchema);
    schema.addProperties("name", new StringSchema());

    Parameter parameter = new Parameter();
    parameter.setName("name");
    parameter.setSchema(schema);

    List parameters = new ArrayList();
    parameters.add(parameter);


    Operation operation = new Operation();
    operation.setParameters(parameters);

    PathItem pathItem = new PathItem();
    pathItem.setGet(operation);
    openAPI.path("/hello",pathItem);

    new InlineModelResolver().flatten(openAPI);

    Operation getOperation = openAPI.getPaths().get("/hello").getGet();
    Parameter param = getOperation.getParameters().get(0);
    assertTrue(param.getSchema().get$ref() != null);



    Schema bodySchema = openAPI.getComponents().getSchemas().get("name");
    assertTrue(bodySchema instanceof Schema);

    assertNotNull(bodySchema.getProperties().get("address"));
}