Java Code Examples for io.swagger.v3.oas.models.media.Schema#getType()

The following examples show how to use io.swagger.v3.oas.models.media.Schema#getType() . 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: HaskellHttpClientCodegen.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Override
public String toInstantiationType(Schema p) {
    if (ModelUtils.isMapSchema(p)) {
        Schema additionalProperties2 = getAdditionalProperties(p);
        String type = additionalProperties2.getType();
        if (null == type) {
            LOGGER.error("No Type defined for Additional Schema " + additionalProperties2 + "\n" //
                    + "\tIn Schema: " + p);
        }
        String inner = getSchemaType(additionalProperties2);
        return "(Map.Map Text " + inner + ")";
    } else if (ModelUtils.isArraySchema(p)) {
        ArraySchema ap = (ArraySchema) p;
        return getSchemaType(ap.getItems());
    } else {
        return null;
    }
}
 
Example 2
Source File: OpenApiSchemaValidations.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Validate the OAS document uses supported values for the 'type' attribute.
 * <p>
 * The type must be one of the following values: null, boolean, object, array, number, string, integer.
 * 
 * @param schema An input schema, regardless of the type of schema
 * @return {@link ValidationRule.Pass} if the check succeeds, otherwise {@link ValidationRule.Fail}
 */
private static ValidationRule.Result checkInvalidType(SchemaWrapper schemaWrapper) {
    Schema schema = schemaWrapper.getSchema();
    ValidationRule.Result result = ValidationRule.Pass.empty();
    if (schema.getType() != null && !validTypes.contains(schema.getType())) {
        result = new ValidationRule.Fail();
        String name = schema.getName();
        if (name == null) {
            name = schema.getTitle();
        }
        result.setDetails(String.format(Locale.ROOT,
            "Schema '%s' uses the '%s' type, which is not a valid type.",
            name, schema.getType()));
        return result;
    }
    return result;
}
 
Example 3
Source File: ParameterTransformer.java    From swagger-brake with Apache License 2.0 6 votes vote down vote up
@Override
public RequestParameter transform(Parameter from) {
    RequestParameterInType inType = requestParameterInTypeResolver.resolve(from.getIn());
    String name = from.getName();
    boolean required = BooleanUtils.toBoolean(from.getRequired());
    Schema swSchema = from.getSchema();
    if (swSchema != null) {
        // Validation for detecting when a request parameter is used with an actual schema object
        // even though its forbidden by the spec
        // https://github.com/redskap/swagger-brake/issues/28
        if (swSchema.getType() == null) {
            throw new IllegalStateException("schema does not have any type");
        }
        return new RequestParameter(inType, name, required, schemaTransformer.transform(swSchema));
    }
    return new RequestParameter(inType, name, required);
}
 
Example 4
Source File: MarkdownRender.java    From openapi-diff with Apache License 2.0 5 votes vote down vote up
protected String type(Schema schema) {
  String result = "object";
  if (schema instanceof ArraySchema) {
    result = "array";
  } else if (schema.getType() != null) {
    result = schema.getType();
  }
  return result;
}
 
Example 5
Source File: OpenApi3Utils.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public static Map<String, ObjectField> resolveAllOfArrays(List<Schema> allOfSchemas) {
  Map<String, ObjectField> properties = new HashMap<>();
  for (Schema schema : allOfSchemas) {
    if (schema.getType() != null && !schema.getType().equals("object"))
      throw new SpecFeatureNotSupportedException("allOf only allows inner object types");
    for (Map.Entry<String, ? extends Schema> entry : ((Map<String, Schema>) schema.getProperties()).entrySet()) {
      properties.put(entry.getKey(), new OpenApi3Utils.ObjectField(entry.getValue(), entry.getKey(), schema));
    }
  }
  return properties;
}
 
Example 6
Source File: InlineModelResolver.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
/**
 * This function fix models that are string (mostly enum). Before this fix, the example
 * would look something like that in the doc: "\"example from def\""
 * @param m Model implementation
 */
private void fixStringModel(Schema m) {
    if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) {
        String example = m.getExample().toString();
        if (example.substring(0, 1).equals("\"") &&
                example.substring(example.length() - 1).equals("\"")) {
            m.setExample(example.substring(1, example.length() - 1));
        }
    }
}
 
Example 7
Source File: ExampleBuilder.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
public static Example alreadyProcessedRefExample(String name, Map<String, Schema> definitions, Map<String, Example> processedModels) {
    if (processedModels.get(name) != null) {
        return processedModels.get(name);
    }
    Schema model = definitions.get(name);
    if (model == null) {
        return null;
    }
    Example output = null;

    // look at type
    if (model.getType() != null) {
        if ("object".equals(model.getType())) {
            return new ObjectExample();
        } else if ("string".equals(model.getType())) {
            return new StringExample("");
        } else if ("integer".equals(model.getType())) {
            return new IntegerExample(0);
        } else if ("long".equals(model.getType())) {
            return new LongExample(0);
        } else if ("float".equals(model.getType())) {
            return new FloatExample(0);
        } else if ("double".equals(model.getType())) {
            return new DoubleExample(0);
        }
    }

    return output;
}
 
Example 8
Source File: BodyGenerator.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
public String generate(Schema<?> schema) {
    if (schema == null) {
        return "";
    }

    boolean isArray = schema instanceof ArraySchema;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Generate body for object " + schema.getName());
    }

    if (isArray) {
        return generateFromArraySchema((ArraySchema) schema);
    }

    @SuppressWarnings("rawtypes")
    Map<String, Schema> properties = schema.getProperties();
    if (properties != null) {
        return generateFromObjectSchema(properties);
    } else if (schema.getAdditionalProperties() instanceof Schema) {
        return generate((Schema<?>) schema.getAdditionalProperties());
    }

    if (schema instanceof ComposedSchema) {
        return generateJsonPrimitiveValue(resolveComposedSchema((ComposedSchema) schema));
    }
    if (schema.getNot() != null) {
        resolveNotSchema(schema);
    }

    // primitive type, or schema without properties
    if (schema.getType() == null || schema.getType().equals("object")) {
        schema.setType("string");
    }

    return generateJsonPrimitiveValue(schema);
}
 
Example 9
Source File: OpenApiHelpers.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private static String getSchemaType(Schema<?> schema) {
    String type = schema.getType();
    if (StringUtils.isNotEmpty(type)) {
        return type;
    } else {
        return generateRefLink(schema.get$ref());
    }
}
 
Example 10
Source File: ConsoleRender.java    From openapi-diff with Apache License 2.0 5 votes vote down vote up
protected String type(Schema schema) {
  String result = "object";
  if (schema instanceof ArraySchema) {
    result = "array";
  } else if (schema.getType() != null) {
    result = schema.getType();
  }
  return result;
}
 
Example 11
Source File: HtmlRender.java    From openapi-diff with Apache License 2.0 5 votes vote down vote up
protected String type(Schema schema) {
  String result = "object";
  if (schema instanceof ArraySchema) {
    result = "array";
  } else if (schema.getType() != null) {
    result = schema.getType();
  }
  return result;
}
 
Example 12
Source File: ResourceInterfaceGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private ParameterSpec.Builder parseProperties(String parameterName, Schema parameterSchema) {
	if (parameterSchema.getType() != null) {
		return parseTypeBasedSchema(parameterName, parameterSchema);
	} else if (parameterSchema.get$ref() != null) {
		// simple no inheritance
		return createSimpleParameterSpec(null, getNameFromRef(parameterSchema.get$ref()), parameterName);
	} else if (parameterSchema instanceof ComposedSchema && CollectionUtils.isNotEmpty(((ComposedSchema) parameterSchema).getAllOf())) {
		return createSimpleParameterSpec(null, determineParentClassNameUsingOneOf(parameterSchema, parameterName, allComponents), parameterName);
	} else {
		throw new IllegalArgumentException("Incorrect schema. One of [type, $ref, discriminator+oneOf] has to be defined in property schema");
	}
}
 
Example 13
Source File: OpenApiClientGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private void parseProperties(TypeSpec.Builder typeSpecBuilder, Map<String, Schema> properties, String targetPackage, List<String> requiredFields,
							 Discriminator discriminator, boolean generateDiscriminatorProperty) {
	String discriminatorPropertyName = discriminator == null ? null : discriminator.getPropertyName();
	for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
		if (!generateDiscriminatorProperty && equalsIgnoreCase(propertyEntry.getKey(), discriminatorPropertyName)) {
			continue;
		}
		// type or ref or oneOf + (discriminator)
		Schema innerSchema = propertyEntry.getValue();
		FieldSpec.Builder fieldSpecBuilder;
		if (innerSchema.getType() != null) {
			fieldSpecBuilder = parseTypeBasedSchema(propertyEntry.getKey(), innerSchema, targetPackage, typeSpecBuilder);
		} else if (innerSchema.get$ref() != null) {
			// simple no inheritance
			fieldSpecBuilder = createSimpleFieldSpec(targetPackage, getNameFromRef(innerSchema.get$ref()), propertyEntry.getKey(), typeSpecBuilder);
		} else if (innerSchema instanceof ComposedSchema && CollectionUtils.isNotEmpty(((ComposedSchema) innerSchema).getAllOf())) {
			fieldSpecBuilder = createSimpleFieldSpec(targetPackage, determineParentClassNameUsingOneOf(innerSchema, propertyEntry.getKey(), allComponents), propertyEntry.getKey(), typeSpecBuilder);
		} else if (innerSchema.getDiscriminator() != null) {
			// complicated inheritance - identify target class
			fieldSpecBuilder = createSimpleFieldSpec(targetPackage, determineParentClassNameUsingDiscriminator(innerSchema, propertyEntry.getKey(), allComponents), propertyEntry.getKey(), typeSpecBuilder);
		} else {
			throw new IllegalArgumentException("Incorrect schema. One of [type, $ref, discriminator+oneOf] has to be defined in property schema");
		}
		if (requiredFields != null && requiredFields.contains(propertyEntry.getKey())) {
			fieldSpecBuilder.addAnnotation(NotNull.class);
		}
		if (fieldSpecBuilder != null) {
			typeSpecBuilder.addField(fieldSpecBuilder.build());
		}
	}
}
 
Example 14
Source File: TypeFormat.java    From servicecomb-toolkit with Apache License 2.0 4 votes vote down vote up
public TypeFormat(Schema schema) {
  this.type = schema.getType();
  this.format = schema.getFormat();
}
 
Example 15
Source File: OASMergeUtil.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
public static Schema mergeSchema(Schema thisSchema, Schema thatSchema) {
  if (thatSchema == null) {
    return thisSchema;
  }
  // Overwriting `implementation` is explicitly disallowed
  // Overwriting `not` is explicitly disallowed
  // Overwriting `oneOf` is explicitly disallowed
  // Overwriting `anyOf` is explicitly disallowed
  // Overwriting `allOf` is explicitly disallowed
  // Overwriting `name` is explicitly disallowed
  if (thatSchema.getTitle() != null) {
    thisSchema.setTitle(thatSchema.getTitle());
  }
  // Overwriting `multipleOf` is explicitly disallowed
  if (thatSchema.getMaximum() != null) {
    thisSchema.setMaximum(thatSchema.getMaximum());
  }
  if (thatSchema.getExclusiveMaximum() != null) {
    thisSchema.setExclusiveMaximum(thatSchema.getExclusiveMaximum());
  }

  if (thatSchema.getMinimum() != null) {
    thisSchema.setMinimum(thatSchema.getMinimum());
  }
  if (thatSchema.getExclusiveMinimum() != null) {
    thisSchema.setExclusiveMinimum(thatSchema.getExclusiveMinimum());
  }
  if (thatSchema.getMaxLength() != null) {
    thisSchema.setMaxLength(thatSchema.getMaxLength());
  }
  if (thatSchema.getMinLength() != null) {
    thisSchema.setMinLength(thatSchema.getMinLength());
  }
  if (thatSchema.getPattern() != null) {
    thisSchema.setPattern(thatSchema.getPattern());
  }
  if (thatSchema.getMaxProperties() != null) {
    thisSchema.setMaxProperties(thatSchema.getMaxProperties());
  }
  if (thatSchema.getMinProperties() != null) {
    thisSchema.setMinProperties(thatSchema.getMinProperties());
  }
  // RequiredProperties
  if (thatSchema.getRequired() != null) {
    thisSchema.setRequired(thatSchema.getRequired());
  }
  // Overwriting `name` is explicitly disallowed
  if (thatSchema.getDescription() != null) {
    thisSchema.setDescription(thatSchema.getDescription());
  }
  if (thatSchema.getFormat() != null) {
    thisSchema.setFormat(thatSchema.getFormat());
  }
  // Overwriting `ref` is explicitly disallowed
  if (thatSchema.getNullable() != null) {
    thisSchema.setNullable(thatSchema.getNullable());
  }
  // Overwriting `AccessMode` is explicitly disallowed
  if (thatSchema.getExample() != null) {
    thisSchema.setExample(thatSchema.getExample());
  }
  if (thatSchema.getExternalDocs() != null) {
    thisSchema.setExternalDocs(thatSchema.getExternalDocs());
  }
  if (thatSchema.getDeprecated() != null) {
    thisSchema.setDeprecated(thatSchema.getDeprecated());
  }
  if (thatSchema.getType() != null) {
    thisSchema.setType(thatSchema.getType());
  }
  if (thatSchema.getEnum() != null) {
    thisSchema.setEnum(thatSchema.getEnum());
  }
  if (thatSchema.getDefault() != null) {
    thisSchema.setDefault(thatSchema.getDefault());
  }
  // Overwriting `discriminator` is explicitly disallowed
  // Overwriting `hidden` is explicitly disallowed
  // Overwriting `subTypes` is explicitly disallowed
  if (thatSchema.getExtensions() != null) {
    thisSchema.setExtensions(thatSchema.getExtensions());
  }
  return thisSchema;
}
 
Example 16
Source File: InlineModelResolver.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
private boolean isObjectSchema(Schema schema) {
    return schema instanceof ObjectSchema
            || "object".equalsIgnoreCase(schema.getType())
            || (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty()
            || schema instanceof ComposedSchema);
}
 
Example 17
Source File: OpenApi3Utils.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public static boolean isSchemaArray(Schema schema) {
  if (schema != null && schema.getType() != null)
    return schema.getType().equals("array");
  else return false;
}