Java Code Examples for io.swagger.v3.oas.models.media.Discriminator#getMapping()

The following examples show how to use io.swagger.v3.oas.models.media.Discriminator#getMapping() . 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: OpenApiClientGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
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 2
Source File: SchemaProcessor.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
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 3
Source File: ExternalRefProcessor.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
private void processDiscriminator(Discriminator d, String file) {
    if (d != null && d.getMapping() != null) {
        processDiscriminatorMapping(d.getMapping(), file);
    }
}