io.swagger.models.RefModel Java Examples
The following examples show how to use
io.swagger.models.RefModel.
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 |
private Model traverseAndAddProperties(ModelImpl schema, GenerationContext generationContext, Class<?> superclass, Class<?> clazz) { if (!isInPackagesToBeScanned(superclass, generationContext)) { // adding properties from parent classes is present due to swagger ui bug, after using different ui // this becomes relevant only for third party packages getClassProperties(superclass, generationContext).forEach(schema::addProperty); if (superclass.getSuperclass() != null && !"java.lang".equals(superclass.getSuperclass().getPackage().getName())) { return traverseAndAddProperties(schema, generationContext, superclass.getSuperclass(), superclass); } return schema; } else { RefModel parentClassSchema = new RefModel(); parentClassSchema.set$ref(CommonConstants.COMPONENT_REF_PREFIX + superclass.getSimpleName()); ComposedModel composedSchema = new ComposedModel(); composedSchema.setAllOf(Arrays.asList(parentClassSchema, schema)); InheritanceInfo inheritanceInfo = generationContext.getInheritanceMap().get(superclass.getName()); if (inheritanceInfo != null) { String discriminatorName = inheritanceInfo.getDiscriminatorClassMap().get(clazz.getName()); composedSchema.setVendorExtension("x-discriminator-value", discriminatorName); composedSchema.setVendorExtension("x-ms-discriminator-value", discriminatorName); } return composedSchema; } }
Example #2
Source File: TypeBuilder.java From api-compiler with Apache License 2.0 | 6 votes |
/** Returns the {@link TypeInfo} for the referenced model. */ private TypeInfo getRefModelTypeInfo(Service.Builder serviceBuilder, RefModel refModel) { if (!refModel.get$ref().startsWith(TYPE_DEFINITIONS_PREFIX)) { diagCollector.addDiag( Diag.error( SimpleLocation.TOPLEVEL, "Type definition reference path '%s' must begin with '%s'", refModel.get$ref(), TYPE_DEFINITIONS_PREFIX)); return null; } String refModelName = refModel.get$ref().substring(TYPE_DEFINITIONS_PREFIX.length()); TypeInfo resultTypeInfo = addTypeFromModel( serviceBuilder, refModelName, swagger.getDefinitions() == null ? null : swagger.getDefinitions().get(refModelName)); return resultTypeInfo; }
Example #3
Source File: PatchOperationGenerator.java From yang2swagger with Eclipse Public License 1.0 | 6 votes |
@Override public Operation execute(DataSchemaNode node) { final Operation patch = defaultOperation(); final RefModel definition = new RefModel(getDefinitionId(node)); patch.summary("patches " + getName(node)); String description = node.getDescription() == null ? "patches " + getName(node) : node.getDescription(); patch.description(description); patch.parameter(new BodyParameter() .name(getName(node) + ".body-param") .schema(definition) .description(getName(node) + " to be added or updated")); patch.response(200, new Response() .schema(new RefProperty(getDefinitionId(node))) .description(getName(node))); patch.response(204, new Response().description("Operation successful")); return patch; }
Example #4
Source File: PutOperationGenerator.java From yang2swagger with Eclipse Public License 1.0 | 6 votes |
@Override public Operation execute(DataSchemaNode node) { final Operation put = defaultOperation(); final RefModel definition = new RefModel(getDefinitionId(node)); put.summary("creates or updates " + getName(node)); String description = node.getDescription() == null ? "creates or updates " + getName(node) : node.getDescription(); put.description(description); put.parameter(new BodyParameter() .name(getName(node) + ".body-param") .schema(definition) .description(getName(node) + " to be added or updated")); put.response(201, new Response().description("Object created")); put.response(204, new Response().description("Object modified")); return put; }
Example #5
Source File: PostOperationGenerator.java From yang2swagger with Eclipse Public License 1.0 | 6 votes |
@Override public Operation execute(DataSchemaNode node) { final Operation post = dropLastSegmentParameters ? listOperation() : defaultOperation(); final RefModel definition = new RefModel(getDefinitionId(node)); post.summary("creates " + getName(node)); String description = node.getDescription() == null ? "creates " + getName(node) : node.getDescription(); post.description(description); post.parameter(new BodyParameter() .name(getName(node) + ".body-param") .schema(definition) .description(getName(node) + " to be added to list")); post.response(201, new Response().description("Object created")); post.response(409, new Response().description("Object already exists")); return post; }
Example #6
Source File: ReplaceEmptyWithParent.java From yang2swagger with Eclipse Public License 1.0 | 6 votes |
@Override protected Map<String, String> prepareForReplacement(Swagger swagger) { return swagger.getDefinitions().entrySet() .stream().filter(e -> { Model model = e.getValue(); if (model instanceof ComposedModel) { List<Model> allOf = ((ComposedModel) model).getAllOf(); return allOf.size() == 1 && allOf.get(0) instanceof RefModel; } return false; }).map(e -> { RefModel ref = (RefModel) ((ComposedModel) e.getValue()).getAllOf().get(0); return new Tuple<>(e.getKey(), ref.getSimpleRef()); }).collect(Collectors.toMap(Tuple::first, Tuple::second)); }
Example #7
Source File: ConsumerDrivenValidator.java From assertj-swagger with Apache License 2.0 | 6 votes |
private void validateModel(Model actualDefinition, Model expectedDefinition, String message) { if (isAssertionEnabled(SwaggerAssertionType.MODELS)) { if (expectedDefinition instanceof ModelImpl) { // TODO Validate ModelImpl softAssertions.assertThat(actualDefinition).as(message).isExactlyInstanceOf(ModelImpl.class); } else if (expectedDefinition instanceof RefModel) { // TODO Validate RefModel softAssertions.assertThat(actualDefinition).as(message).isExactlyInstanceOf(RefModel.class); } else if (expectedDefinition instanceof ArrayModel) { ArrayModel arrayModel = (ArrayModel) expectedDefinition; // TODO Validate ArrayModel softAssertions.assertThat(actualDefinition).as(message).isExactlyInstanceOf(ArrayModel.class); } else { // TODO Validate all model types softAssertions.assertThat(actualDefinition).isExactlyInstanceOf(expectedDefinition.getClass()); } } }
Example #8
Source File: DocumentationDrivenValidator.java From assertj-swagger with Apache License 2.0 | 6 votes |
private void validateModel(Model actualDefinition, Model expectedDefinition, String message) { if (isAssertionEnabled(SwaggerAssertionType.MODELS)) { if (expectedDefinition instanceof ModelImpl) { // TODO Validate ModelImpl softAssertions.assertThat(actualDefinition).as(message).isExactlyInstanceOf(ModelImpl.class); } else if (expectedDefinition instanceof RefModel) { // TODO Validate RefModel softAssertions.assertThat(actualDefinition).as(message).isExactlyInstanceOf(RefModel.class); } else if (expectedDefinition instanceof ArrayModel) { ArrayModel arrayModel = (ArrayModel) expectedDefinition; // TODO Validate ArrayModel softAssertions.assertThat(actualDefinition).as(message).isExactlyInstanceOf(ArrayModel.class); } else if (expectedDefinition instanceof ComposedModel) { ComposedModel composedModel = (ComposedModel) expectedDefinition; softAssertions.assertThat(actualDefinition).as(message).isInstanceOfAny(ComposedModel.class, ModelImpl.class); } else { // TODO Validate all model types softAssertions.assertThat(actualDefinition).isExactlyInstanceOf(expectedDefinition.getClass()); } } }
Example #9
Source File: SwaggerUtils.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
public static Map<String, Property> getBodyProperties(Swagger swagger, Parameter parameter) { if (!(parameter instanceof BodyParameter)) { return null; } Model model = ((BodyParameter) parameter).getSchema(); if (model instanceof RefModel) { model = swagger.getDefinitions().get(((RefModel) model).getSimpleRef()); } if (model instanceof ModelImpl) { return model.getProperties(); } return null; }
Example #10
Source File: SwaggerToWordExample.java From poi-tl with Apache License 2.0 | 6 votes |
private List<TextRenderData> fomartSchemaModel(Model schemaModel) { List<TextRenderData> schema = new ArrayList<>(); if (null == schemaModel) return schema; // if array if (schemaModel instanceof ArrayModel) { Property items = ((ArrayModel) schemaModel).getItems(); schema.add(new TextRenderData("<")); schema.addAll(formatProperty(items)); schema.add(new TextRenderData(">")); schema.add(new TextRenderData(((ArrayModel) schemaModel).getType())); } else // if ref if (schemaModel instanceof RefModel) { String ref = ((RefModel) schemaModel).get$ref().substring("#/definitions/".length()); schema.add(new HyperLinkTextRenderData(ref, "anchor:" + ref)); } else if (schemaModel instanceof ModelImpl) { schema.add(new TextRenderData(((ModelImpl) schemaModel).getType())); } // ComposedModel return schema; }
Example #11
Source File: OAS2Parser.java From carbon-apimgt with Apache License 2.0 | 6 votes |
/** * Creates a json string using the swagger object. * * @param swaggerObj swagger object * @return json string using the swagger object * @throws APIManagementException error while creating swagger json */ private String getSwaggerJsonString(Swagger swaggerObj) throws APIManagementException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.enable(SerializationFeature.INDENT_OUTPUT); //this is to ignore "originalRef" in schema objects mapper.addMixIn(RefModel.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefProperty.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefPath.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefParameter.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefResponse.class, IgnoreOriginalRefMixin.class); //this is to ignore "responseSchema" in response schema objects mapper.addMixIn(Response.class, ResponseSchemaMixin.class); try { return new String(mapper.writeValueAsBytes(swaggerObj)); } catch (JsonProcessingException e) { throw new APIManagementException("Error while generating Swagger json from model", e); } }
Example #12
Source File: ConverterMgr.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
private static void initConverters() { // inner converters for (Class<? extends Property> propertyCls : PROPERTY_MAP.keySet()) { addInnerConverter(propertyCls); } converterMap.put(RefProperty.class, new RefPropertyConverter()); converterMap.put(ArrayProperty.class, new ArrayPropertyConverter()); converterMap.put(MapProperty.class, new MapPropertyConverter()); converterMap.put(StringProperty.class, new StringPropertyConverter()); converterMap.put(ObjectProperty.class, new ObjectPropertyConverter()); converterMap.put(ModelImpl.class, new ModelImplConverter()); converterMap.put(RefModel.class, new RefModelConverter()); converterMap.put(ArrayModel.class, new ArrayModelConverter()); }
Example #13
Source File: ReaderTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Test public void testRead() { Map<Class<?>, Object> interfaceMapRef = new HashMap<>(); interfaceMapRef.put(TestSwaggerService.class, new TestSwaggerServiceImpl()); Swagger swagger = new Swagger(); swagger.setBasePath("/rest/"); Reader.read(swagger, interfaceMapRef, ""); Assert.assertEquals("2.0", swagger.getSwagger()); Assert.assertEquals("/rest/", swagger.getBasePath()); Map<String, Path> paths = swagger.getPaths(); Assert.assertEquals(TestSwaggerService.class.getMethods().length, paths.size()); Assert.assertTrue(paths.containsKey(COMPLEX)); List<Parameter> parameters = paths.get(COMPLEX).getPost().getParameters(); Assert.assertTrue(CommonUtils.isNotEmpty(parameters)); Parameter parameter = parameters.get(0); Assert.assertTrue(parameter instanceof BodyParameter); Model schema = ((BodyParameter) parameter).getSchema(); Assert.assertTrue(schema instanceof RefModel); String ref = ((RefModel) schema).get$ref(); Assert.assertEquals("#/definitions/ComplexPojo", ref); }
Example #14
Source File: OASParserUtil.java From carbon-apimgt with Apache License 2.0 | 6 votes |
/** * Creates a json string using the swagger object. * * @param swaggerObj swagger object * @return json string using the swagger object * @throws APIManagementException error while creating swagger json */ public static String getSwaggerJsonString(Swagger swaggerObj) throws APIManagementException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.enable(SerializationFeature.INDENT_OUTPUT); //this is to ignore "originalRef" in schema objects mapper.addMixIn(RefModel.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefProperty.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefPath.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefParameter.class, IgnoreOriginalRefMixin.class); mapper.addMixIn(RefResponse.class, IgnoreOriginalRefMixin.class); //this is to ignore "responseSchema" in response schema objects mapper.addMixIn(Response.class, ResponseSchemaMixin.class); try { return new String(mapper.writeValueAsBytes(swaggerObj)); } catch (JsonProcessingException e) { throw new APIManagementException("Error while generating Swagger json from model", e); } }
Example #15
Source File: DefaultCodegen.java From TypeScript-Microservices with MIT License | 6 votes |
protected void addProperties(Map<String, Property> properties, List<String> required, Model model, Map<String, Model> allDefinitions) { if (model instanceof ModelImpl) { ModelImpl mi = (ModelImpl) model; if (mi.getProperties() != null) { properties.putAll(mi.getProperties()); } if (mi.getRequired() != null) { required.addAll(mi.getRequired()); } } else if (model instanceof RefModel) { String interfaceRef = ((RefModel) model).getSimpleRef(); Model interfaceModel = allDefinitions.get(interfaceRef); addProperties(properties, required, interfaceModel, allDefinitions); } else if (model instanceof ComposedModel) { for (Model component :((ComposedModel) model).getAllOf()) { addProperties(properties, required, component, allDefinitions); } } }
Example #16
Source File: DefaultCodegen.java From TypeScript-Microservices with MIT License | 6 votes |
/** * Recursively look for a discriminator in the interface tree */ private boolean isDiscriminatorInInterfaceTree(ComposedModel model, Map<String, Model> allDefinitions) { if (model == null || allDefinitions == null) return false; Model child = model.getChild(); if (child instanceof ModelImpl && ((ModelImpl) child).getDiscriminator() != null) { return true; } for (RefModel _interface : model.getInterfaces()) { Model interfaceModel = allDefinitions.get(_interface.getSimpleRef()); if (interfaceModel instanceof ModelImpl && ((ModelImpl) interfaceModel).getDiscriminator() != null) { return true; } if (interfaceModel instanceof ComposedModel) { return isDiscriminatorInInterfaceTree((ComposedModel) interfaceModel, allDefinitions); } } return false; }
Example #17
Source File: SchemaObjectResolver.java From assertj-swagger with Apache License 2.0 | 5 votes |
private Map<String, Property> resolveProperties(Model definition, Swagger owningSchema, Set<String> seenRefs) { Map<String, Property> result; // if the definition does not contain any property, then the model will return null instead of an empty map final Map<String, Property> definitionProperties = definition.getProperties() != null ? definition.getProperties() : Collections.emptyMap(); if (definition instanceof RefModel) { // Don't navigate ref-def cycles infinitely final RefModel refDef = (RefModel) definition; if (seenRefs.contains(refDef.getSimpleRef())) { return Collections.emptyMap(); } else { seenRefs.add(refDef.getSimpleRef()); } result = resolveProperties(findDefinition(owningSchema.getDefinitions(), refDef), owningSchema, seenRefs); } else if (definition instanceof ComposedModel) { Map<String, Property> allProperties = new HashMap<>(); if (definitionProperties != null) { allProperties.putAll(definitionProperties); } for (final Model childDefinition : ((ComposedModel) definition).getAllOf()) { allProperties.putAll(resolveProperties(childDefinition, owningSchema, seenRefs)); } result = allProperties; } else { result = definitionProperties; } return result; }
Example #18
Source File: SundrioGenerator.java From sundrio with Apache License 2.0 | 5 votes |
public TypeDef createModel(String name, Model model, AbstractJavaCodegen config, Map<String, Model> allDefinitions) { String prefix = name.contains(DOT) ? name.substring(0, name.lastIndexOf(DOT)) : EMPTY; String packageName = prefix.isEmpty() ? config.modelPackage() : config.modelPackage() + DOT + prefix; String className = prefix.isEmpty() ? name : name.substring(name.lastIndexOf(DOT) + 1); ClassRef superClass = null; List<ClassRef> interfaces = new ArrayList<>(); List<Property> fields = new ArrayList<>(); List<Method> methods = new ArrayList<>(); if (model instanceof ComposedModel) { ComposedModel composed = (ComposedModel) model; // interfaces (intermediate models) if (composed.getInterfaces() != null) { for (RefModel _interface : composed.getInterfaces()) { Model interfaceModel = null; if (allDefinitions != null) { interfaceModel = allDefinitions.get(_interface.getSimpleRef()); } } return new TypeDefBuilder() .withKind(Kind.CLASS) .withPackageName(packageName) .withName(className) .withImplementsList(interfaces) .withExtendsList(superClass) .withProperties(fields) .withMethods(methods) .build(); } } return null; }
Example #19
Source File: ApiGatewaySdkSwaggerApiImporter.java From aws-apigateway-importer with Apache License 2.0 | 5 votes |
private Optional<String> getInputModel(BodyParameter p) { io.swagger.models.Model model = p.getSchema(); if (model instanceof RefModel) { String modelName = ((RefModel) model).getSimpleRef(); // assumption: complex ref? return Optional.of(modelName); } return Optional.empty(); }
Example #20
Source File: SequenceGenerator.java From carbon-apimgt with Apache License 2.0 | 5 votes |
private static void populateParametersFromOperation(Operation operation, Map<String, Model> definitions, Map<String, String> parameterJsonPathMapping, Map<String, String> queryParameters) { List<Parameter> parameters = operation.getParameters(); for (Parameter parameter : parameters) { String name = parameter.getName(); if (parameter instanceof BodyParameter) { Model schema = ((BodyParameter) parameter).getSchema(); if (schema instanceof RefModel) { String $ref = ((RefModel) schema).get$ref(); if (StringUtils.isNotBlank($ref)) { String defName = $ref.substring("#/definitions/".length()); Model model = definitions.get(defName); Example example = ExampleBuilder.fromModel(defName, model, definitions, new HashSet<String>()); String jsonExample = Json.pretty(example); try { org.json.JSONObject json = new org.json.JSONObject(jsonExample); SequenceUtils.listJson(json, parameterJsonPathMapping); } catch (JSONException e) { log.error("Error occurred while generating json mapping for the definition: " + defName, e); } } } } if (parameter instanceof QueryParameter) { String type = ((QueryParameter) parameter).getType(); queryParameters.put(name, type); } } }
Example #21
Source File: ModelDiff.java From swagger-diff with Apache License 2.0 | 5 votes |
private Model findReferenceModel(Model model, Map<String, Model> modelMap) { String modelName = null; if (model instanceof RefModel) { modelName = ((RefModel) model).getSimpleRef(); } else if (model instanceof ArrayModel) { Property arrayType = ((ArrayModel) model).getItems(); if (arrayType instanceof RefProperty) { modelName = ((RefProperty) arrayType).getSimpleRef(); } } return modelName == null ? null : modelMap.get(modelName); }
Example #22
Source File: PlantUMLCodegen.java From swagger2puml with Apache License 2.0 | 5 votes |
/** * * @param composedModel * @param modelsMap * @return */ private List<ClassMembers> getClassMembers(ComposedModel composedModel, Map<String, Model> modelsMap) { LOGGER.entering(LOGGER.getName(), "getClassMembers-ComposedModel"); List<ClassMembers> classMembers = new ArrayList<ClassMembers>(); Map<String, Property> childProperties = new HashMap<String, Property>(); if (null != composedModel.getChild()) { childProperties = composedModel.getChild().getProperties(); } List<Model> allOf = composedModel.getAllOf(); for (Model currentModel : allOf) { if (currentModel instanceof RefModel) { RefModel refModel = (RefModel) currentModel; childProperties.putAll(modelsMap.get(refModel.getSimpleRef()).getProperties()); classMembers = convertModelPropertiesToClassMembers(childProperties, modelsMap.get(refModel.getSimpleRef()), modelsMap); } } LOGGER.exiting(LOGGER.getName(), "getClassMembers-ComposedModel"); return classMembers; }
Example #23
Source File: SortComplexModels.java From yang2swagger with Eclipse Public License 1.0 | 5 votes |
private void sortModels(ComposedModel m) { m.getAllOf().sort((a,b) -> { if(a instanceof RefModel) { if(b instanceof RefModel) { return ((RefModel) a).getSimpleRef().compareTo(((RefModel) b).getSimpleRef()); } return -1; } return 1; }); }
Example #24
Source File: ModelAdapter.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
@Override public String getRefType() { if (model instanceof RefModel) { return ((RefModel) model).getSimpleRef(); } return null; }
Example #25
Source File: BodyParameterAdapter.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
@Override public String getRefType() { if (model instanceof RefModel) { return ((RefModel) model).getSimpleRef(); } return null; }
Example #26
Source File: TestApiImplicitParams.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
@Test public void testBody() { Swagger swagger = SwaggerGenerator.generate(ApiImplicitParamsAnnotation.class); Path path = swagger.getPaths().get("/testBody"); Operation operation = path.getOperations().get(0); BodyParameter parameter = (BodyParameter) operation.getParameters().get(0); Assert.assertEquals("body", parameter.getName()); Assert.assertEquals("User", ((RefModel) parameter.getSchema()).getSimpleRef()); }
Example #27
Source File: SwaggerUtils.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
public static ModelImpl getModelImpl(Swagger swagger, BodyParameter bodyParameter) { Model model = bodyParameter.getSchema(); if (model instanceof ModelImpl) { return (ModelImpl) model; } if (!(model instanceof RefModel)) { return null; } String simpleRef = ((RefModel) model).getSimpleRef(); Model targetModel = swagger.getDefinitions().get(simpleRef); return targetModel instanceof ModelImpl ? (ModelImpl) targetModel : null; }
Example #28
Source File: TestGenerate.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test public void testGenerateParameter() throws NoSuchMethodException { Method someMethod = TestGenerate.class.getMethod("someMethod", SomeParameter.class); Type type = someMethod.getGenericParameterTypes()[0]; Swagger swagger = new Swagger(); BodyParameter parameter = new BodyParameter(); Parameter parameter1 = ParameterProcessor.applyAnnotations(swagger, parameter, type, new ArrayList<Annotation>()); Assert.assertTrue(parameter1 instanceof BodyParameter); Model schema = ((BodyParameter) parameter1).getSchema(); Assert.assertTrue(schema instanceof RefModel); Assert.assertEquals("#/definitions/SomeParameter", ((RefModel) schema).get$ref()); }
Example #29
Source File: XmlExampleGenerator.java From TypeScript-Microservices with MIT License | 5 votes |
protected String toXml(Model model, int indent, Collection<String> path) { if (model instanceof RefModel) { RefModel ref = (RefModel) model; Model actualModel = examples.get(ref.getSimpleRef()); if (actualModel instanceof ModelImpl) { return modelImplToXml((ModelImpl) actualModel, indent, path); } } else if (model instanceof ModelImpl) { return modelImplToXml((ModelImpl) model, indent, path); } return null; }
Example #30
Source File: ModelDiff.java From swagger-diff with Apache License 2.0 | 4 votes |
private boolean isModelReference(Model model) { return model instanceof RefModel || model instanceof ArrayModel; }