} else if (schema instanceof ComposedSchema) { ComposedSchema composedSchema = (ComposedSchema) schema; if (composedSchema.getAllOf() != null) { for (Schema ref : composedSchema.getAllOf()) { addSchemaRef(ref, referencedDefinitions); if (composedSchema.getAnyOf() != null) { for (Schema ref : composedSchema.getAnyOf()) { addSchemaRef(ref, referencedDefinitions); if (composedSchema.getOneOf() != null) { for (Schema ref : composedSchema.getOneOf()) { addSchemaRef(ref, referencedDefinitions);
model = new ComposedSchema() .type("object") .name(name); } else { Schema refSchema = new Schema().$ref(allOfRef.getName()); if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) { composedSchema.addAllOfItem(refSchema); anyOfFiltered.forEach(c -> { Schema anyOfRef = context.resolve(new AnnotatedType().type(c).jsonViewAnnotation(annotatedType.getJsonViewAnnotation())); composedSchema.addAnyOfItem(new Schema().$ref(anyOfRef.getName())); if (oneOfRef != null) { if (StringUtils.isBlank(oneOfRef.getName())) { composedSchema.addOneOfItem(oneOfRef); } else { composedSchema.addOneOfItem(new Schema().$ref(oneOfRef.getName()));
@Test public void testComposedUberObject() throws Exception { final Schema uberModel = context.resolve(new AnnotatedType(UberObject.class)); assertNotNull(uberModel); // make sure child points at parent assertTrue(uberModel instanceof ComposedSchema); ComposedSchema cm = (ComposedSchema) uberModel; assertEquals(cm.getAnyOf().size(), 2); assertEquals(cm.getAnyOf().get(0).get$ref(), "#/components/schemas/UserObject"); // parent properties are filtered out of subclass when parent doesn't define subtypes assertNotNull(cm.getProperties()); assertEquals(cm.getProperties().size(), 3); final Schema interfaceModel = context.getDefinedModels().get("UserObject"); assertNotNull(interfaceModel); assertUserObjectPropertiesValid(interfaceModel.getProperties()); }
@Test public void testComposedChildBean() throws Exception { final Schema subModel = context.resolve(new AnnotatedType(Sub2Bean.class)); assertNotNull(subModel); // make sure child points at parent assertTrue(subModel instanceof ComposedSchema); ComposedSchema cm = (ComposedSchema) subModel; assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/BaseBean2"); // make sure parent properties are filtered out of subclass assertSub1PropertiesValid(cm.getProperties()); final Schema baseModel = context.getDefinedModels().get("BaseBean2"); assertNotNull(baseModel); assertBase2PropertiesValid(baseModel.getProperties()); }
boolean adjacent = false; if (aggregateCombinators) { Schema model = SchemaTypeUtil.createSchema(composedSchema.getType(), composedSchema.getFormat()); Set<String> requiredProperties = new HashSet<>(); Set<Object> examples = new HashSet<>(); if ((composedSchema.getAllOf() != null && composedSchema.getAnyOf() != null && composedSchema.getOneOf() != null)|| (composedSchema.getAllOf() != null && composedSchema.getAnyOf() != null) || (composedSchema.getAllOf() != null && composedSchema.getOneOf() != null)|| (composedSchema.getOneOf() != null && composedSchema.getAnyOf() != null)){ if (composedSchema.getAllOf() != null) { for (Schema innerModel : composedSchema.getAllOf()) { Schema resolved = resolveSchema(innerModel); Map<String, Schema> properties = resolved.getProperties(); if (composedSchema.getExtensions() != null) { Map<String, Object> extensions = composedSchema.getExtensions(); for (String key : extensions.keySet()) { model.addExtension(key, composedSchema.getExtensions().get(key)); } if (composedSchema.getOneOf() != null) { if(adjacent == false) { Schema resolved; List<Schema> list = new ArrayList<>(); for (Schema innerModel : composedSchema.getOneOf()) { resolved = resolveSchema(innerModel); list.add(resolved);
ComposedSchema composedSchema = new ComposedSchema(); composedSchema.addAllOfItem(referenceSchema); composedSchema.setAdditionalProperties(schema.getAdditionalProperties()); composedSchema.setDefault(schema.getDefault()); composedSchema.setDeprecated(schema.getDeprecated()); composedSchema.setDescription(schema.getDescription()); composedSchema.setDiscriminator(schema.getDiscriminator()); composedSchema.setEnum(schema.getEnum()); composedSchema.setExample(schema.getExample()); composedSchema.setExclusiveMaximum(schema.getExclusiveMaximum()); composedSchema.setExclusiveMinimum(schema.getExclusiveMinimum()); composedSchema.setExtensions(schema.getExtensions()); composedSchema.setExternalDocs(schema.getExternalDocs()); composedSchema.setFormat(schema.getFormat()); composedSchema.setMaximum(schema.getMaximum()); composedSchema.setMaxItems(schema.getMaxItems()); composedSchema.setMaxLength(schema.getMaxLength()); composedSchema.setMaxProperties(schema.getMaxProperties()); composedSchema.setMinimum(schema.getMinimum()); composedSchema.setMinItems(schema.getMinItems()); composedSchema.setMinLength(schema.getMinLength()); composedSchema.setMinProperties(schema.getMinProperties()); composedSchema.setMultipleOf(schema.getMultipleOf()); composedSchema.setName(schema.getName()); composedSchema.setNot(schema.getNot()); composedSchema.setNullable(schema.getNullable()); composedSchema.setPattern(schema.getPattern()); composedSchema.setProperties(schema.getProperties());
} else if (v2Model instanceof ComposedModel) { ComposedModel composedModel = (ComposedModel) v2Model; ComposedSchema composed = new ComposedSchema(); composed.setDescription(composedModel.getDescription()); composed.setExample(composedModel.getExample()); if (composedModel.getExternalDocs() != null) { composed.setExternalDocs(convert(composedModel.getExternalDocs())); composed.setTitle(composedModel.getTitle()); composed.setExtensions(convert(composedModel.getVendorExtensions())); composed.setAllOf(composedModel.getAllOf().stream().map(this::convert).collect(Collectors.toList())); result = composed; } else {
@Test(description = "it should deserialize the composition test") public void testCompositionTest() throws IOException { final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/compositionTest-3.0.json"); final Object deserialized = m.readValue(json, OpenAPI.class); assertTrue(deserialized instanceof OpenAPI); OpenAPI openAPI = (OpenAPI) deserialized; Schema lizardSchema = openAPI.getComponents().getSchemas().get("Lizard"); assertTrue(lizardSchema instanceof ComposedSchema); assertEquals(((ComposedSchema) lizardSchema).getAllOf().size(), 2); Schema petSchema = openAPI.getComponents().getSchemas().get("Pet"); assertEquals(petSchema.getDiscriminator().getPropertyName(), "pet_type"); assertEquals(petSchema.getDiscriminator().getMapping().get("cachorro"), "#/components/schemas/Dog"); }
schema.allOf().length > 0 || schema.anyOf().length > 0) { schemaObject = new ComposedSchema(); } else { schemaObject = new Schema(); for (Class<?> schemaImplementation : schemaImplementations) { Schema oneOfSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation); ((ComposedSchema) schemaObject).addOneOfItem(oneOfSchemaObject); for (Class<?> schemaImplementation : schemaImplementations) { Schema anyOfSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation); ((ComposedSchema) schemaObject).addAnyOfItem(anyOfSchemaObject); for (Class<?> schemaImplementation : schemaImplementations) { Schema allOfSchemaObject = resolveSchemaFromType(schemaImplementation, components, jsonViewAnnotation); ((ComposedSchema) schemaObject).addAllOfItem(allOfSchemaObject);
composedSchema = (ComposedSchema) new ComposedSchema() .title(subtypeModel.getTitle()) .name(subtypeModel.getName()) .deprecated(subtypeModel.getDeprecated()) .extensions(subtypeModel.getExtensions()); composedSchema.setEnum(subtypeModel.getEnum()); } else { composedSchema = (ComposedSchema) subtypeModel; if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) { composedSchema.addAllOfItem(refSchema); if (StringUtils.isNotBlank(composedSchema.getName())) { context.defineModel(composedSchema.getName(), composedSchema, new AnnotatedType().type(currentType), null);
Assert.assertNull(properties.get("childName")); Assert.assertTrue(model instanceof ComposedSchema); Assert.assertTrue(((ComposedSchema)model).getOneOf().size() == 2); Assert.assertNotNull(properties.get("childName")); Assert.assertTrue(model instanceof ComposedSchema); Assert.assertTrue(((ComposedSchema)model).getAllOf().size() == 1); Assert.assertNull(properties.get("childName")); Assert.assertTrue(model instanceof ComposedSchema); Assert.assertTrue(((ComposedSchema)model).getOneOf().size() == 2);
private Map<String, String> getMapping(ComposedSchema composedSchema) { Map<String, String> reverseMapping = new LinkedHashMap<>(); for (Schema schema : composedSchema.getOneOf()) { String ref = schema.get$ref(); if (ref == null) { throw new IllegalArgumentException("invalid oneOf schema"); } String schemaName = refPointer.getRefName(ref); if (schemaName == null) { throw new IllegalArgumentException("invalid schema: " + ref); } reverseMapping.put(ref, schemaName); } if (composedSchema.getDiscriminator().getMapping() != null) { for (String ref : composedSchema.getDiscriminator().getMapping().keySet()) { reverseMapping.put(composedSchema.getDiscriminator().getMapping().get(ref), ref); } } return reverseMapping .entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); } }
@Test(description = "read composed schem refs #2616") public void readArrayComposedSchema_ticket2616() { Map<String, Schema> schemas = ModelConverters.getInstance().readAll(TestObject2616.class); Schema model = schemas.get("testObject"); Assert.assertNotNull(model); Map<String, Schema> properties = model.getProperties(); Assert.assertNotNull(properties.get("objects")); Assert.assertTrue(properties.get("objects") instanceof ArraySchema); model = schemas.get("AbstractObject"); Assert.assertNotNull(model); Assert.assertTrue(model instanceof ComposedSchema); Assert.assertTrue(((ComposedSchema)model).getOneOf().size() == 2); model = schemas.get("AObject"); Assert.assertNotNull(model); model = schemas.get("BObject"); Assert.assertNotNull(model); model = schemas.get("objects"); Assert.assertNull(model); }
protected static Schema resolveComposedSchema(Components components, Schema schema) { if (schema instanceof ComposedSchema) { ComposedSchema composedSchema = (ComposedSchema) schema; List<Schema> allOfSchemaList = composedSchema.getAllOf(); if (allOfSchemaList != null) { for (Schema allOfSchema : allOfSchemaList) { allOfSchema = refPointer.resolveRef(components, allOfSchema, allOfSchema.get$ref()); allOfSchema = resolveComposedSchema(components, allOfSchema); schema = addSchema(schema, allOfSchema); } composedSchema.setAllOf(null); } } return schema; }
/** * Recursively look for a discriminator in the interface tree */ private boolean isDiscriminatorInInterfaceTree(ComposedSchema composedSchema, Map<String, Schema> allSchema) { if (composedSchema == null || allSchema == null || allSchema.isEmpty()) { return false; } if (composedSchema.getDiscriminator() != null) { return true; } final List<Schema> interfaces = getInterfaces(composedSchema); if(interfaces == null) { return false; } for (Schema interfaceSchema : interfaces) { if (interfaceSchema.getDiscriminator() != null) { return true; } } return false; }
boolean adjacent = false; if (aggregateCombinators) { Schema model = SchemaTypeUtil.createSchema(composedSchema.getType(), composedSchema.getFormat()); Set<String> requiredProperties = new HashSet<>(); Set<Object> examples = new HashSet<>(); if ((composedSchema.getAllOf() != null && composedSchema.getAnyOf() != null && composedSchema.getOneOf() != null)|| (composedSchema.getAllOf() != null && composedSchema.getAnyOf() != null) || (composedSchema.getAllOf() != null && composedSchema.getOneOf() != null)|| (composedSchema.getOneOf() != null && composedSchema.getAnyOf() != null)){ if (composedSchema.getAllOf() != null) { for (Schema innerModel : composedSchema.getAllOf()) { Schema resolved = resolveSchema(innerModel); Map<String, Schema> properties = resolved.getProperties(); if (composedSchema.getExtensions() != null) { Map<String, Object> extensions = composedSchema.getExtensions(); for (String key : extensions.keySet()) { model.addExtension(key, composedSchema.getExtensions().get(key)); } if (composedSchema.getOneOf() != null) { if(adjacent == false) { Schema resolved; List<Schema> list = new ArrayList<>(); for (Schema innerModel : composedSchema.getOneOf()) { resolved = resolveSchema(innerModel); list.add(resolved);
} else if (v2Model instanceof ComposedModel) { ComposedModel composedModel = (ComposedModel) v2Model; ComposedSchema composed = new ComposedSchema(); composed.setDescription(composedModel.getDescription()); composed.setExample(composedModel.getExample()); if (composedModel.getExternalDocs() != null) { composed.setExternalDocs(convert(composedModel.getExternalDocs())); composed.setTitle(composedModel.getTitle()); composed.setExtensions(convert(composedModel.getVendorExtensions())); composed.setAllOf(composedModel.getAllOf().stream().map(this::convert).collect(Collectors.toList())); result = composed; } else {
protected String getParentName(ComposedSchema composedSchema, Map<String, Schema> allSchemas) { if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) { for (Schema schema : composedSchema.getAllOf()) { String ref = schema.get$ref(); if (!StringUtils.isBlank(ref)) { return ModelUtils.getSimpleRef(ref); } } } return null; }
ComposedSchema composedSchema = new ComposedSchema(); if (n.isObject()) { schema = getSchema((ObjectNode) n, location, result); composedSchema.addAllOfItem(schema); if (n.isObject()) { schema = getSchema((ObjectNode) n, location, result); composedSchema.addAnyOfItem(schema); if (n.isObject()) { schema = getSchema((ObjectNode) n, location, result); composedSchema.addOneOfItem(schema);
composedSchema = (ComposedSchema) new ComposedSchema() .title(subtypeModel.getTitle()) .name(subtypeModel.getName()) .deprecated(subtypeModel.getDeprecated()) .extensions(subtypeModel.getExtensions()); composedSchema.setEnum(subtypeModel.getEnum()); } else { composedSchema = (ComposedSchema) subtypeModel; if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) { composedSchema.addAllOfItem(refSchema); if (StringUtils.isNotBlank(composedSchema.getName())) { context.defineModel(composedSchema.getName(), composedSchema, new AnnotatedType().type(currentType), null);