@Test public void testPrimitiveModel() throws Exception { String json = "{\n" + " \"type\": \"string\",\n" + " \"enum\": [\n" + " \"a\",\n" + " \"b\",\n" + " \"c\"\n" + " ]\n" + "}"; final Schema model = Json.mapper().readValue(json, Schema.class); assertNotNull(model.getEnum()); assertTrue(model.getEnum().size() == 3); }
@Test(description = "it should desserialize Long schema correctly") public void deserializeLongSchema() throws IOException { final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/oas3_2.yaml"); final OpenAPI swagger = Yaml.mapper().readValue(jsonString, OpenAPI.class); assertNotNull(swagger); Schema s = swagger.getPaths().get("/withIntegerEnum/{stage}").getGet().getParameters().get(0).getSchema(); assertEquals(s.getEnum().get(0), 2147483647); assertEquals(s.getEnum().get(1), 3147483647L); assertEquals(s.getEnum().get(2), 31474836475505055L); }
.extensions(subtypeModel.getExtensions()); composedSchema.setEnum(subtypeModel.getEnum()); } else { composedSchema = (ComposedSchema) subtypeModel;
/** * Determine all of the types in the model definitions that are aliases of * simple types. * @param allSchemas The complete set of model definitions. * @return A mapping from model name to type alias */ private static Map<String, String> getAllAliases(Map<String, Schema> allSchemas) { Map<String, String> aliases = new HashMap<>(); if (allSchemas == null || allSchemas.isEmpty()) { return aliases; } for (Map.Entry<String, Schema> entry : allSchemas.entrySet()) { String swaggerName = entry.getKey(); Schema schema = entry.getValue(); if (schema instanceof ArraySchema || schema instanceof MapSchema) { continue; } String schemaType = getTypeOfSchema(schema); if (schemaType != null && !schemaType.equals("object") && schema.getEnum() == null) { aliases.put(swaggerName, schemaType); } } return aliases; }
/** * Determine all of the types in the model definitions that are aliases of * simple types. * @param allSchemas The complete set of model definitions. * @return A mapping from model name to type alias */ private static Map<String, String> getAllAliases(Map<String, Schema> allSchemas) { Map<String, String> aliases = new HashMap<>(); if (allSchemas == null || allSchemas.isEmpty()) { return aliases; } for (Map.Entry<String, Schema> entry : allSchemas.entrySet()) { String swaggerName = entry.getKey(); Schema schema = entry.getValue(); if (schema instanceof ArraySchema || schema instanceof MapSchema) { continue; } String schemaType = getTypeOfSchema(schema); if (schemaType != null && !schemaType.equals("object") && schema.getEnum() == null) { aliases.put(swaggerName, schemaType); } } return aliases; }
/** * Determine all of the types in the model definitions (schemas) that are aliases of * simple types. * * @param schemas The complete set of model definitions (schemas). * @return A mapping from model name to type alias */ static Map<String, String> getAllAliases(Map<String, Schema> schemas) { if (schemas == null || schemas.isEmpty()) { return new HashMap<>(); } Map<String, String> aliases = new HashMap<>(); for (Map.Entry<String, Schema> entry : schemas.entrySet()) { String oasName = entry.getKey(); Schema schema = entry.getValue(); String schemaType = getPrimitiveType(schema); if (schemaType != null && !schemaType.equals("object") && !schemaType.equals("array") && schema.getEnum() == null && !ModelUtils.isMapSchema(schema)) { aliases.put(oasName, schemaType); } } return aliases; }
protected String schema(int deepness, Schema schema, DiffContext context) { StringBuilder sb = new StringBuilder(); sb.append(listItem(deepness, "Enum", schema.getEnum())); sb.append(properties(deepness, "Property", schema.getProperties(), true, context)); if (schema instanceof ComposedSchema) { sb.append(schema(deepness, (ComposedSchema) schema, context)); } else if (schema instanceof ArraySchema) { sb.append(items(deepness, resolve(((ArraySchema) schema).getItems()), context)); } return sb.toString(); }
private ValidationReport validateArrayParam(final Collection<String> values, final Parameter parameter) { final ValidationReport report = Stream.of( validateMaxItems(values, parameter), validateMinItems(values, parameter), validateUniqueItems(values, parameter) ).reduce(ValidationReport.empty(), ValidationReport::merge); if (parameter.getSchema().getEnum() != null && !parameter.getSchema().getEnum().isEmpty()) { final Set<String> enumValues = new HashSet<>(parameter.getSchema().getEnum()); return values.stream() .filter(v -> !enumValues.contains(v)) .map(v -> ValidationReport.singleton(messages.get("validation.request.parameter.enum.invalid", v, parameter.getName(), parameter.getSchema().getEnum()) )) .reduce(report, ValidationReport::merge); } return values.stream() .map(v -> schemaValidator.validate( v, ((ArraySchema) parameter.getSchema()).getItems(), "request.parameter") ) .reduce(report, ValidationReport::merge); }
if (schema.getDefault() != null) { String _default = schema.getDefault().toString(); if (schema.getEnum() == null) { return String.format("\"%s\"", _default); } else {
} else if (ModelUtils.isStringSchema(p)) { if (p.getEnum() != null) { return enumValuesToEnumTypeUnion(p.getEnum(), "string"); if (p.getEnum() != null) { return numericEnumValuesToEnumTypeUnion(new ArrayList<Number>(p.getEnum())); if (p.getEnum() != null) { return numericEnumValuesToEnumTypeUnion(new ArrayList<Number>(p.getEnum()));
@Override public String toDefaultValue(Schema p) { if (p.getEnum() != null && !p.getEnum().isEmpty()) { if (p.getDefault() != null) { return "." + escapeText((String) p.getDefault()); } } if (ModelUtils.isIntegerSchema(p) || ModelUtils.isNumberSchema(p) || ModelUtils.isBooleanSchema(p)) { if (p.getDefault() != null) { return p.getDefault().toString(); } } else if (ModelUtils.isStringSchema(p)) { if (p.getDefault() != null) { return "\"" + escapeText((String) p.getDefault()) + "\""; } } return null; }
if (schema.getDefault() != null) { String _default = schema.getDefault().toString(); if (schema.getEnum() == null) { return String.format("\"%s\"", escapeText(_default)); } else {
@Override public String getSchemaType(Schema p) { String openAPIType = super.getSchemaType(p); if (ModelUtils.isStringSchema(p)) { if (p.getEnum() != null) { return openAPIType; } } if (isLanguagePrimitive(openAPIType) || isLanguageGenericType(openAPIType)) { return openAPIType; } return addModelPrefix(openAPIType); }
String def = p.getDefault().toString(); if (def != null) { out = p.getEnum() == null ? String.format(Locale.ROOT, "'%s'", escapeText(def)) : def;
private Schema transformSchema(io.swagger.v3.oas.models.media.Schema swSchema) { String ref = swSchema.get$ref(); if (isNotBlank(ref) && SeenRefHolder.isNotSeen(ref)) { io.swagger.v3.oas.models.media.Schema resolvedSchema = getSchemaForRef(ref); SeenRefHolder.store(ref); Schema schema = internalTransform(resolvedSchema); SeenRefHolder.remove(ref); return schema; } Schema.Builder schemaBuilder = new Schema.Builder(swSchema.getType()); schemaBuilder.schemaAttributes(getSchemaAttributes(swSchema)); List<String> enumValues = swSchema.getEnum(); if (CollectionUtils.isNotEmpty(enumValues)) { schemaBuilder.enumValues(enumValues); } return schemaBuilder.build(); }
if (p.getDefault() != null) { String _default = (String) p.getDefault(); if (p.getEnum() == null) { return "\"" + _default + "\""; } else {
if (p.getDefault() != null) { String _default = (String) p.getDefault(); if (p.getEnum() == null) { return "\"" + escapeText(_default) + "\""; } else {
/** * Retrieves the type of a parameter, or otherwise null * * @param definitionDocumentResolver the definition document resolver * @return the type of the parameter, or otherwise null */ private Type getType(DocumentResolver definitionDocumentResolver) { Validate.notNull(parameter, "parameter must not be null!"); Type type; if (parameter.get$ref() != null) { String refName = RefUtils.computeSimpleRef(parameter.get$ref()); type = new RefType(definitionDocumentResolver.apply(refName), new ObjectType(refName, null)); } else { Schema serializableParameter = parameter.getSchema(); @SuppressWarnings("unchecked") List<String> enums = serializableParameter.getEnum(); if (CollectionUtils.isNotEmpty(enums)) { type = new EnumType(serializableParameter.getName(), enums); } else { type = new BasicType(serializableParameter.getType(), serializableParameter.getName(), serializableParameter.getFormat()); } if (serializableParameter.getType().equals("array")) { String collectionFormat = serializableParameter.getFormat(); type = new ArrayType(serializableParameter.getName(), new PropertyAdapter(serializableParameter).getType(definitionDocumentResolver), collectionFormat); } } return type; }
composedSchema.setDescription(schema.getDescription()); composedSchema.setDiscriminator(schema.getDiscriminator()); composedSchema.setEnum(schema.getEnum()); composedSchema.setExample(schema.getExample()); composedSchema.setExclusiveMaximum(schema.getExclusiveMaximum());