io.swagger.v3.oas.models.media.Discriminator Java Examples
The following examples show how to use
io.swagger.v3.oas.models.media.Discriminator.
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: ComponentSchemaTransformer.java From spring-openapi with MIT License | 6 votes |
public Schema transformSimpleSchema(Class<?> clazz, Map<String, InheritanceInfo> inheritanceMap) { if (clazz.isEnum()) { return schemaGeneratorHelper.createEnumSchema(clazz.getEnumConstants()); } List<String> requiredFields = new ArrayList<>(); Schema<?> schema = new Schema<>(); schema.setType("object"); schema.setProperties(getClassProperties(clazz, requiredFields)); schemaGeneratorHelper.enrichWithTypeAnnotations(schema, clazz.getDeclaredAnnotations()); updateRequiredFields(schema, requiredFields); if (inheritanceMap.containsKey(clazz.getName())) { Discriminator discriminator = createDiscriminator(inheritanceMap.get(clazz.getName())); schema.setDiscriminator(discriminator); enrichWithDiscriminatorProperty(schema, discriminator); } if (clazz.getSuperclass() != null) { return traverseAndAddProperties(schema, inheritanceMap, clazz.getSuperclass(), clazz); } return schema; }
Example #2
Source File: JavaInheritanceTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test(description = "convert a composed model with discriminator") public void javaInheritanceWithDiscriminatorTest() { final Schema base = new Schema().name("Base"); Discriminator discriminator = new Discriminator().mapping("name", StringUtils.EMPTY); discriminator.setPropertyName("model_type"); base.setDiscriminator(discriminator); final Schema schema = new ComposedSchema() .addAllOfItem(new Schema().$ref("Base")); OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("Base", base); final JavaClientCodegen codegen = new JavaClientCodegen(); codegen.setOpenAPI(openAPI); final CodegenModel cm = codegen.fromModel("sample", schema); Assert.assertEquals(cm.name, "sample"); Assert.assertEquals(cm.classname, "Sample"); Assert.assertEquals(cm.parent, "Base"); Assert.assertEquals(cm.imports, Sets.newHashSet("Base")); }
Example #3
Source File: OpenAPIDeserializer.java From swagger-parser with Apache License 2.0 | 6 votes |
public Discriminator getDiscriminator (ObjectNode node, String location, ParseResult result){ Discriminator discriminator = new Discriminator(); String value = getString("propertyName",node,true,location,result); if (StringUtils.isNotBlank(value)){ discriminator.setPropertyName(value); } ObjectNode mappingNode = getObject("mapping", node,false, location, result); if(mappingNode != null){ Map<String, String> mapping = new LinkedHashMap<>(); Set<String> keys = getKeys(mappingNode); for(String key : keys) { mapping.put(key, mappingNode.get(key).asText()); } discriminator.setMapping(mapping); } return discriminator; }
Example #4
Source File: OpenApiClientGenerator.java From spring-openapi with MIT License | 5 votes |
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 #5
Source File: OpenApiClientGenerator.java From spring-openapi with MIT License | 5 votes |
private void parseDiscriminator(TypeSpec.Builder typeSpecBuilder, Discriminator discriminator, String targetPackage, boolean generateDiscriminatorProperty) { if (discriminator.getPropertyName() != null) { AnnotationSpec.Builder annotationSpecBuilder = AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "JsonTypeInfo.Id.NAME") .addMember("include", "JsonTypeInfo.As.PROPERTY") .addMember("property", "$S", discriminator.getPropertyName()); if (generateDiscriminatorProperty) { annotationSpecBuilder.addMember("visible", "true"); } typeSpecBuilder.addAnnotation(annotationSpecBuilder.build()); } if (discriminator.getMapping() != null) { List<AnnotationSpec> annotationSpecs = discriminator.getMapping().entrySet().stream() .map(discriminatorMappingEntry -> AnnotationSpec.builder(Type.class) .addMember("value", "$T.class", ClassName.get(targetPackage, discriminatorMappingEntry.getValue())) .addMember("name", "$S", discriminatorMappingEntry.getKey()) .build()) .collect(Collectors.toList()); AnnotationSpec jsonSubTypesAnnotation = AnnotationSpec.builder(JsonSubTypes.class) .addMember("value", wrapAnnotationsIntoArray(annotationSpecs)) .build(); typeSpecBuilder.addAnnotation(jsonSubTypesAnnotation); } }
Example #6
Source File: ComponentSchemaTransformer.java From spring-openapi with MIT License | 5 votes |
private Discriminator createDiscriminator(InheritanceInfo inheritanceInfo) { Map<String, String> discriminatorTypeMapping = inheritanceInfo.getDiscriminatorClassMap().entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); Discriminator discriminator = new Discriminator(); discriminator.setPropertyName(inheritanceInfo.getDiscriminatorFieldName()); discriminator.setMapping(discriminatorTypeMapping); return discriminator; }
Example #7
Source File: ComponentSchemaTransformer.java From spring-openapi with MIT License | 5 votes |
private void enrichWithDiscriminatorProperty(Schema schema, Discriminator discriminator) { if (schema != null && !schema.getProperties().containsKey(discriminator.getPropertyName())) { List<String> discriminatorTypeRequiredProperty = new ArrayList<>(); discriminatorTypeRequiredProperty.add(discriminator.getPropertyName()); updateSchemaProperties(schema, discriminator.getPropertyName(), new StringSchema()); updateRequiredFields(schema, discriminatorTypeRequiredProperty); } }
Example #8
Source File: SchemaProcessor.java From swagger-parser with Apache License 2.0 | 5 votes |
private void changeDiscriminatorMapping(ComposedSchema composedSchema, String oldRef, String newRef) { Discriminator discriminator = composedSchema.getDiscriminator(); if (!oldRef.equals(newRef) && discriminator != null) { String oldName = RefUtils.computeDefinitionName(oldRef); String newName = RefUtils.computeDefinitionName(newRef); String mappingName = null; if (discriminator.getMapping() != null) { for (String name : discriminator.getMapping().keySet()) { if (oldRef.equals(discriminator.getMapping().get(name))) { mappingName = name; break; } } if (mappingName != null) { discriminator.getMapping().put(mappingName, newRef); } } if (mappingName == null && !oldName.equals(newName)) { if (discriminator.getMapping() == null) { discriminator.setMapping(new HashMap()); } discriminator.getMapping().put(oldName, newRef); } } }
Example #9
Source File: SchemaDiscriminatorChangeValidator.java From servicecomb-toolkit with Apache License 2.0 | 4 votes |
@Override protected Discriminator getProperty(Schema schema) { return schema.getDiscriminator(); }
Example #10
Source File: SchemaDiscriminatorChangeValidator.java From servicecomb-toolkit with Apache License 2.0 | 4 votes |
@Override protected boolean isAllowed(Discriminator leftProperty, Discriminator rightProperty) { return leftProperty.equals(rightProperty); }
Example #11
Source File: SchemaDiscriminatorChangeValidator.java From servicecomb-toolkit with Apache License 2.0 | 4 votes |
@Override protected String getMessage(Discriminator leftProperty, Discriminator rightProperty) { return DiffViolationMessages.NEW_NOT_EQ_OLD; }
Example #12
Source File: InheritedModelEndpointTest.java From flow with Apache License 2.0 | 4 votes |
public InheritedModelEndpointTest() { super(Arrays.asList(InheritedModelEndpoint.class, Discriminator.class, Schema.class, ArraySchema.class, ExternalDocumentation.class, XML.class, Version.class)); }
Example #13
Source File: ExternalRefProcessor.java From swagger-parser with Apache License 2.0 | 4 votes |
private void processDiscriminator(Discriminator d, String file) { if (d != null && d.getMapping() != null) { processDiscriminatorMapping(d.getMapping(), file); } }