Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary property support #171

Merged
merged 1 commit into from
Apr 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright © 2010-2013 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.rules;

import org.jsonschema2pojo.Schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JClassContainer;
import com.sun.codemodel.JType;

/**
* <p>
* Applies the "media" hyper schema rule.
* </p>
*
* @see <a href="http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.3">
* Section 4.3 media, JSON Hyper-Schema: Hypertext definitions for JSON Schema</a>
*
* @author Christian Trimble
* @since 0.4.2
*/
public class MediaRule implements Rule<JType, JType> {

public static final String BINARY_ENCODING = "binaryEncoding";

protected RuleFactory ruleFactory;

/**
* <p>
* Constructs a new media rule.
* </p>
*
* @param ruleFactory the rule factory that created this rule.
* @since 0.4.2
*/
protected MediaRule(RuleFactory ruleFactory) {
this.ruleFactory = ruleFactory;
}

/**
* <p>
* Applies this schema rule.
* </p>
*
* @param nodeName
* the name of the property.
* @param mediaNode
* the media node
* @param baseType
* the type with the media node. This must be java.lang.String.
* @param schema
* the schema containing the property.
* @return byte[] when a binary encoding is specified, baseType otherwise.
* @since 0.4.2
*/
@Override
public JType apply(String nodeName, JsonNode mediaNode, JType baseType, Schema schema) {
if (!mediaNode.has(BINARY_ENCODING)) {
return baseType;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case has been narrowed, to only pass through items without binaryEncoding.


return baseType.owner().ref(byte[].class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,14 @@ public NameHelper getNameHelper() {
return nameHelper;
}

/**
* Provides a rule instance that should be applied when a "media"
* declaration is found in the schema.
*
* @return a schema rule that can handle the "media" declaration.
*/
public Rule<JType, JType> getMediaRule() {
return new MediaRule(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public JType apply(String nodeName, JsonNode node, JClassContainer jClassContain

if (node.has("format")) {
type = ruleFactory.getFormatRule().apply(nodeName, node.get("format"), type, schema);
} else if(propertyTypeName.equals("string") && node.has("media")) {
type = ruleFactory.getMediaRule().apply(nodeName, node.get("media"), type, schema);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelittlejohn does this seem like the correct way for format and media to interact, when they are both present?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so. It'll minimize backwards-compatibility problems in the obscure case that someone is using both format and media. I think you could make an argument for either to win out in this case but I think you've taken the right path.


return type;
Expand Down
4 changes: 4 additions & 0 deletions jsonschema2pojo-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
<artifactId>jsonschema2pojo-ant</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Expand Down
Loading