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

The following examples show how to use io.swagger.v3.oas.models.parameters.Parameter#getContent() . 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: ReflectionUtils.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
public JavaType getTypeFromParameter(Parameter parameter, Map<String, Schema> definitions) {
    if (parameter.getSchema() != null) {
        JavaType parameterType =  getTypeFromProperty(parameter.getSchema().getType(),parameter.getSchema().getFormat(), parameter.getSchema(), definitions);
        if (parameterType != null){
            return parameterType;
        }
    }

    else if (parameter.getContent() != null) {
        Map<String,MediaType> content   = parameter.getContent();
        for (String mediaType : content.keySet()){
            if (content.get(mediaType).getSchema() != null) {
                Schema model = content.get(mediaType).getSchema();
                return getTypeFromModel("", model, definitions);
            }
        }
    }
    return null;
}
 
Example 2
Source File: PathsProcessor.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
protected void updateLocalRefs(Parameter param, String pathRef) {
    if (param.get$ref() != null){
        if(isLocalRef(param.get$ref())) {
            param.set$ref(computeLocalRef(param.get$ref(), pathRef));
        }
    }
    if(param.getSchema() != null) {
        updateLocalRefs(param.getSchema(), pathRef);
    }
    if(param.getContent() != null) {
        Map<String, MediaType> content = param.getContent();
        for (String key: content.keySet()) {
            MediaType mediaType = content.get(key);
            if (mediaType.getSchema() != null) {
                updateLocalRefs(mediaType.getSchema(), pathRef);
            }
        }
    }

}
 
Example 3
Source File: OpenAPI3RequestValidationHandlerImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private void handleContent(Parameter parameter) {
  Content contents = parameter.getContent();
  ParameterLocation location = resolveLocation(parameter.getIn());
  List<MediaType> jsonsContents = OpenApi3Utils.extractTypesFromMediaTypesMap(contents, Utils::isJsonContentType);
  if (jsonsContents.size() == 1) {
    this.addRule(ParameterValidationRuleImpl.ParameterValidationRuleFactory
      .createValidationRuleWithCustomTypeValidator(parameter.getName(), JsonTypeValidator.JsonTypeValidatorFactory
        .createJsonTypeValidator(OpenApi3Utils.generateSanitizedJsonSchemaNode(jsonsContents.get(0).getSchema(), this.spec)),
        !OpenApi3Utils.isRequiredParam(parameter), OpenApi3Utils.resolveAllowEmptyValue(parameter), location), location);
  } else if (contents.size() > 1 && !jsonsContents.isEmpty()) {
    // Mount anyOf
    List<ParameterTypeValidator> validators =
      jsonsContents.stream().map(e -> JsonTypeValidator.JsonTypeValidatorFactory
        .createJsonTypeValidator(OpenApi3Utils.generateSanitizedJsonSchemaNode(e.getSchema(), this.spec))).collect(Collectors.toList());
    validators.add(CONTENT_TYPE_VALIDATOR);
    AnyOfTypeValidator validator = new AnyOfTypeValidator(validators);
    this.addRule(ParameterValidationRuleImpl.ParameterValidationRuleFactory
      .createValidationRuleWithCustomTypeValidator(parameter.getName(), validator, !OpenApi3Utils.isRequiredParam(parameter), OpenApi3Utils.resolveAllowEmptyValue(parameter)
        , location), location);
  } else {
    this.addRule(ParameterValidationRuleImpl.ParameterValidationRuleFactory
      .createValidationRuleWithCustomTypeValidator(parameter.getName(), CONTENT_TYPE_VALIDATOR, !OpenApi3Utils.isRequiredParam(parameter), 
              OpenApi3Utils.resolveAllowEmptyValue(parameter), location), location);
  }
}
 
Example 4
Source File: ParameterProcessor.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
public void processParameter(Parameter parameter) {
    String $ref = parameter.get$ref();
    if($ref != null){
        RefFormat refFormat = computeRefFormat(parameter.get$ref());
        if (isAnExternalRefFormat(refFormat)){
            final String newRef = externalRefProcessor.processRefToExternalParameter($ref, refFormat);
            if (newRef != null) {
                parameter.set$ref(newRef);
            }
        }
    }
    if (parameter.getSchema() != null){
        schemaProcessor.processSchema(parameter.getSchema());
    }
    if (parameter.getExamples() != null){
        Map <String, Example> examples = parameter.getExamples();
        for(String exampleName: examples.keySet()){
            final Example example = examples.get(exampleName);
            exampleProcessor.processExample(example);
        }
    }
    Schema schema = null;
    if(parameter.getContent() != null) {
        Map<String,MediaType> content = parameter.getContent();
        for( String mediaName : content.keySet()) {
            MediaType mediaType = content.get(mediaName);
            if(mediaType.getSchema()!= null) {
                schema = mediaType.getSchema();
                if (schema != null) {
                    schemaProcessor.processSchema(schema);
                }
            }
        }
    }
}
 
Example 5
Source File: OpenAPI3RequestValidationHandlerImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
private boolean checkSupportedAndNeedWorkaround(Parameter parameter) {
  boolean result = false;
  if (Boolean.TRUE.equals(parameter.getAllowReserved())) {
    throw new SpecFeatureNotSupportedException("allowReserved field not supported!");
  } else if (parameter.getContent() != null && parameter.getContent().size() != 0) {
    handleContent(parameter);
    result = true;
  } else /* From this moment only astonishing magic happens */ if (Boolean.TRUE.equals(parameter.getExplode())) {
    boolean isObject = OpenApi3Utils.isParameterObjectOrAllOfType(parameter);
    String style = OpenApi3Utils.resolveStyle(parameter);
    if (OpenApi3Utils.isParameterArrayType(parameter) && "matrix".equals(style)) {
      this.magicParameterExplodedMatrixArray(parameter);
      result = true;
    }
    if (isObject && ("form".equals(style) || "matrix".equals(style) || "label".equals(style))) {
      this.magicParameterExplodedObject(parameter);
      result = true;
    }
    if (isObject && "simple".equals(style)) {
      this.magicParameterExplodedStyleSimpleTypeObject(parameter);
      result = true;
    } else if ("deepObject".equals(style)) {
      this.magicParameterExplodedStyleDeepObjectTypeObject(parameter);
      result = true;
    } 
  }
  return result;
}
 
Example 6
Source File: ParameterContentValidator.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
@Override
protected Map<String, MediaType> getMapProperty(Parameter oasObject) {
  return oasObject.getContent();
}
 
Example 7
Source File: ParameterContentDiffValidator.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
@Override
protected Map<String, MediaType> getMapProperty(Parameter oasObject) {
  return oasObject.getContent();
}
 
Example 8
Source File: ExternalRefProcessor.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public String processRefToExternalParameter(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if(renamedRef != null) {
        return renamedRef;
    }

    final Parameter parameter = cache.loadRef($ref, refFormat, Parameter.class);

    if(parameter == null) {
        // stop!  There's a problem.  retain the original ref
        LOGGER.warn("unable to load model reference from `" + $ref + "`.  It may not be available " +
                "or the reference isn't a valid model schema");
        return $ref;
    }
    String newRef;

    if (openAPI.getComponents() == null) {
        openAPI.setComponents(new Components());
    }
    Map<String, Parameter> parameters = openAPI.getComponents().getParameters();

    if (parameters == null) {
        parameters = new LinkedHashMap<>();
    }

    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);

    Parameter existingParameters = parameters.get(possiblyConflictingDefinitionName);

    if (existingParameters != null) {
        LOGGER.debug("A model for " + existingParameters + " already exists");
        if(existingParameters.get$ref() != null) {
            // use the new model
            existingParameters = null;
        }
    }
    newRef = possiblyConflictingDefinitionName;
    cache.putRenamedRef($ref, newRef);

    if(existingParameters == null) {
        // don't overwrite existing model reference
        openAPI.getComponents().addParameters(newRef, parameter);
        cache.addReferencedKey(newRef);

        String file = $ref.split("#/")[0];
        if (parameter.get$ref() != null) {
            RefFormat format = computeRefFormat(parameter.get$ref());
            if (isAnExternalRefFormat(format)) {
                parameter.set$ref(processRefToExternalParameter(parameter.get$ref(), format));
            } else {
                processRefToExternalParameter(file + parameter.get$ref(), RefFormat.RELATIVE);
            }
        }
    }

    if(parameter != null) {
        if(parameter.getContent() != null){
            processRefContent(parameter.getContent(), $ref);
        }
        if(parameter.getSchema() != null){
            processRefSchemaObject(parameter.getSchema(), $ref);
        }
    }

    return newRef;
}